from bs4 import BeautifulSoup import requests from typing import List from dataclasses import dataclass import os import re import pickle @dataclass class Torrent: link: str season_ep: str season: str cookies = { "cf_clereance": "ZN62DXsprSj9LxHcpoQERkOZ3hK4vmGD4lQArmdsing-1679950264-0-150", "pass": "aefe3fe3c855fdf4dc773f1372ae22a2", "uid": "1610296" } def search_series(serie_name: str) -> List[Torrent]: base_url = "https://iptorrents.com/" search_url = f"{base_url}t?q={serie_name}" response = requests.get(search_url, cookies=cookies) response.raise_for_status() expr = re.compile(".*((S[0-9]+)E[0-9]+).*(1080p).*") soup = BeautifulSoup(response.content, 'html.parser') results = soup.find_all('a', {'class': 'tTipWrap'}) torrents = [] for result in results: link = result['href'] m = expr.search(link) if link.endswith('.torrent') and m is not None: torrents.append(Torrent(link, m.group(1), season=m.group(2))) return torrents def download_torrent(url: str, file_name: str) -> None: read = requests.get(url, cookies=cookies) with open(file_name, 'wb') as f: for chunk in read.iter_content(chunk_size=512): if chunk: f.write(chunk) #print(f"Downloaded {file_name} success") def main() -> None: if os.path.exists("downloaded.bin"): with open("downloaded.bin", "rb") as f: downloaded: List[Torrent] = pickle.load(f) else: downloaded = list() serie_name = input("Name serie: ") torrents = search_series(serie_name) season = "" if len(torrents) <= 0: print("torrent not found") return #choice = int(input("Qual serie? ")) seasons = set() for to in torrents: seasons.add(to.season) for idx, s in enumerate(seasons): print(f"{idx + 1}, {s}") choice = int(input("What season? ")) if 0 <= (choice -1 ) <= len(seasons): season = list(seasons)[choice -1] for torrent in torrents: if torrent.link.find(season): name = os.path.basename(torrent.link) got = list(filter(lambda x: (x.season_ep in torrent.season_ep), downloaded)) if len(got) == 0: downloaded.append(torrent) print(f"Downloading {torrent.season} - {torrent.season_ep}") download_torrent(f"https://iptorrents.com/t/{torrent.link}", name) else: print("Escolha invalida") with open("downloaded.bin", 'wb') as f: pickle.dump(downloaded, f, protocol=pickle.HIGHEST_PROTOCOL) if __name__ == '__main__': main()