1+ from ui .ui_print import *
2+ import urllib .parse
3+ import releases
4+
5+ name = "yts"
6+ session = requests .Session ()
7+
8+
9+ def setup (cls , new = False ):
10+ from scraper .services import setup
11+ setup (cls , new )
12+
13+
14+ def scrape (query , altquery ):
15+ mediatype = 'TV' if regex .search (r'(\bseries|S\d{2}|complete|season|seasons|nyaa|part\b)' , altquery , regex .I ) else 'Movies'
16+
17+ from scraper .services import active
18+ scraped_releases = []
19+
20+ match = regex .search (r'\btt(\d+)\b' , altquery )
21+ imdb_id = match .group (1 ) if match else None
22+ if imdb_id is None :
23+ match = regex .search (r'\btt(\d+)\b' , query )
24+ imdb_id = match .group (1 ) if match else None
25+ ui_print ("[yts] using IMDB ID: " + "tt" + imdb_id if imdb_id else "[yts] No IMDB ID found in query" , ui_settings .debug )
26+
27+ if 'yts' in active and mediatype == "Movies" :
28+
29+ search_term = "tt" + imdb_id if imdb_id is not None else urllib .parse .quote (query .replace ("." , " " ))
30+ headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' }
31+ api_url = f'https://yts.mx/api/v2/list_movies.json?query_term={ search_term } '
32+ response = None
33+ try :
34+ ui_print ("[yts] Sending GET request to YTS API URL: " + api_url , ui_settings .debug )
35+ response = session .get (api_url , headers = headers )
36+
37+ if response .status_code == 200 :
38+ movies = response .json ().get ('data' , {}).get ('movies' , [])
39+
40+ for movie in movies :
41+ title = movie .get ('title' ).replace ("'" , "" ).replace ("’" , "" ).replace (' - ' , '.' ).replace (': ' , '.' ).replace (' ' , '.' )
42+ # title_long = movie.get('title_long')
43+ year = movie .get ('year' )
44+ torrents = movie .get ('torrents' , [])
45+ ui_print (f"[yts] Found { len (torrents )} torrent(s)" , ui_settings .debug )
46+
47+ for torrent in torrents :
48+ quality = torrent .get ('quality' )
49+ torrent_type = torrent .get ('type' )
50+ video_codec = torrent .get ('video_codec' )
51+ bit_depth = torrent .get ('bit_depth' )
52+ audio_channels = torrent .get ('audio_channels' )
53+ torrent_hash = torrent .get ('hash' )
54+ download = 'magnet:?xt=urn:btih:' + torrent_hash
55+ size_bytes = int (torrent .get ('size_bytes' , 0 ))
56+ size = size_bytes / (1024 * 1024 * 1024 )
57+ seeders = int (torrent .get ('seeds' , 0 ))
58+
59+ release_title = f"{ title } .{ year } .{ quality } .{ torrent_type } .{ video_codec } .{ bit_depth } bit.AAC.{ audio_channels } "
60+
61+ scraped_releases += [
62+ releases .release ('[yts]' , 'torrent' , release_title , [], size , [download ], seeders = int (seeders ))]
63+
64+ ui_print (f"[yts] Scraped release: title={ release_title } , size={ size :.2f} GB, seeders={ seeders } " , ui_settings .debug )
65+ else :
66+ ui_print ("[yts] Failed to retrieve data from YTS API. Status code: " + str (response .status_code ))
67+
68+ except Exception as e :
69+ if hasattr (response , "status_code" ) and not str (response .status_code ).startswith ("2" ):
70+ ui_print ('yts error ' + str (response .status_code ) + ': yts is temporarily not reachable' )
71+ else :
72+ ui_print ('yts error: unknown error' )
73+ ui_print ('yts error: exception: ' + str (e ), ui_settings .debug )
74+
75+ return scraped_releases
0 commit comments