feat(torrents): add Galaxy (TorrentGalaxy) provider with IMDb-first search and inline magnets
- new tg parser: /imdb-detail/<tt...> when an IMDb id is available (exact title, local seeder/size sort), keyword search as fallback; rows carry full magnets inline so Play works without a second fetch - wire tg into /api/torrents and /api/magnet, TG_HOST env (default https://torrentgalaxy.info) - Galaxy tab in the torrents modal; tabs now share equal space and hidden-scroll on overflow - fix tab drag&drop: reorder on the sanitized order so tabs added later (like Galaxy) can be dragged
This commit is contained in:
1 parent
fb645b75a5
commit
c41f1f098f
8 files changed
+184
-9
No files matched your search
@@ -0,0 +1,151 @@
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { load } from 'cheerio';
|
||||
import torrentTitle from 'parse-torrent-title';
|
||||
|
||||
import { TG_HOST } from '@/env';
|
||||
|
||||
import { Sort } from '../index';
|
||||
|
||||
const UA =
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
||||
|
||||
const CATEGORIES = { movie: 'Movies', tv: 'TV' } as const;
|
||||
|
||||
type ShowType = 'movie' | 'tv';
|
||||
|
||||
export class TorrentGalaxy {
|
||||
private client: AxiosInstance;
|
||||
|
||||
constructor() {
|
||||
this.client = axios.create({
|
||||
baseURL: TG_HOST || 'https://torrentgalaxy.info',
|
||||
headers: {
|
||||
'User-Agent': UA,
|
||||
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'en-US,en;q=0.9'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private buildSearchUrl(query: string, type?: ShowType, sort = Sort.seeds) {
|
||||
const parts = [`keywords:${encodeURIComponent(query)}`];
|
||||
|
||||
if (type && type in CATEGORIES) parts.push(`category:${CATEGORIES[type]}`);
|
||||
parts.push(`order:-${sort === Sort.seeds ? 'se' : 's'}`);
|
||||
|
||||
return `/get-posts/${parts.join(':')}/`;
|
||||
}
|
||||
|
||||
private parseRows(data: string) {
|
||||
const $ = load(data);
|
||||
|
||||
return $('div.tgxtablecell[data-href^="/post-detail/"]')
|
||||
.map((_, cell) => {
|
||||
const $cell = $(cell);
|
||||
const row = $cell.parent();
|
||||
const originalTitle = $cell.find('a[title]').first().attr('title') || $cell.find('a').first().text().trim();
|
||||
|
||||
const size = row
|
||||
.find('.badge.badge-secondary')
|
||||
.map((__, el) => $(el).text().replace(/\s+/g, ' ').trim())
|
||||
.get()
|
||||
.find((text) => /[GMK]B/.test(text));
|
||||
|
||||
const seeds = row
|
||||
.find('span[title="Seeders/Leechers"]')
|
||||
.first()
|
||||
.text()
|
||||
.match(/(\d+)\s*\/\s*(\d+)/);
|
||||
|
||||
const magnet = row.find('a[href^="magnet:"]').first().attr('href') || '';
|
||||
const hash = /urn:btih:([0-9a-fA-F]{40})/.exec(magnet)?.[1] ?? '';
|
||||
|
||||
const { title, resolution, source, codec, container, season, episode, year } = torrentTitle.parse(
|
||||
originalTitle || ''
|
||||
);
|
||||
|
||||
return {
|
||||
year: year?.toString(),
|
||||
source,
|
||||
codec,
|
||||
container,
|
||||
originalTitle,
|
||||
title,
|
||||
episodes: season && episode ? `S${season}:E${episode}` : undefined,
|
||||
id: $cell.attr('data-href') || originalTitle || '',
|
||||
size,
|
||||
seeders: parseInt(seeds?.[1] || '0', 10),
|
||||
quality: resolution,
|
||||
magnet,
|
||||
download: $cell.attr('data-href') || '',
|
||||
hash
|
||||
};
|
||||
})
|
||||
.get();
|
||||
}
|
||||
|
||||
private parseSize(size?: string) {
|
||||
const [value, unit] = (size || '').trim().split(/\s+/);
|
||||
const units: Record<string, number> = { B: 1, KB: 1024, MB: 1024 ** 2, GB: 1024 ** 3, TB: 1024 ** 4 };
|
||||
|
||||
return Number.parseFloat(value) * (units[unit] ?? 1) || 0;
|
||||
}
|
||||
|
||||
async search({
|
||||
query,
|
||||
sort = Sort.seeds,
|
||||
type,
|
||||
imdbID
|
||||
}: {
|
||||
query: string;
|
||||
sort?: Sort;
|
||||
type?: ShowType;
|
||||
imdbID?: string;
|
||||
}): Promise<Torrent[]> {
|
||||
if (imdbID && /^tt\d+$/.test(imdbID)) {
|
||||
try {
|
||||
// The IMDb page has no server-side sort, so sort the fetched rows locally.
|
||||
const { data } = await this.client.get(`/imdb-detail/${imdbID}/`, { responseType: 'text' });
|
||||
|
||||
const byImdb = this.parseRows(data as string);
|
||||
|
||||
if (byImdb.length) {
|
||||
return sort === Sort.size
|
||||
? byImdb.sort((a, b) => this.parseSize(b.size) - this.parseSize(a.size))
|
||||
: byImdb.sort((a, b) => b.seeders - a.seeders);
|
||||
}
|
||||
} catch (_) {
|
||||
// fall back to the keyword search below
|
||||
}
|
||||
}
|
||||
|
||||
const search = (searchQuery: string) =>
|
||||
this.client.get(this.buildSearchUrl(searchQuery, type, sort)).then(({ data }) => this.parseRows(data as string));
|
||||
|
||||
let torrents = await search(query);
|
||||
|
||||
if (!torrents.length) {
|
||||
// The query is scoped by the show's year to disambiguate sequels, but the TMDB
|
||||
// release year can differ from the year in release names, so fall back to the bare title.
|
||||
const fallback = query.replace(/\s\d{4}$/, '');
|
||||
|
||||
if (fallback !== query) torrents = await search(fallback);
|
||||
}
|
||||
|
||||
return torrents;
|
||||
}
|
||||
|
||||
async magnet(url: string) {
|
||||
const { data } = await this.client.get(url, { responseType: 'text' });
|
||||
|
||||
return (
|
||||
load(data as string)('a.dl-magnet')
|
||||
.first()
|
||||
.attr('href') || ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const parser = new TorrentGalaxy();
|
||||
|
||||
export default parser;
|
||||
Reference in new issue
Block a user