app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from bs4 import BeautifulSoup
  2. import requests
  3. from typing import List
  4. from dataclasses import dataclass
  5. import os
  6. import re
  7. import pickle
  8. @dataclass
  9. class Torrent:
  10. link: str
  11. season_ep: str
  12. season: str
  13. cookies = {
  14. "cf_clereance": "ZN62DXsprSj9LxHcpoQERkOZ3hK4vmGD4lQArmdsing-1679950264-0-150",
  15. "pass": "aefe3fe3c855fdf4dc773f1372ae22a2",
  16. "uid": "1610296"
  17. }
  18. def search_series(serie_name: str) -> List[Torrent]:
  19. base_url = "https://iptorrents.com/"
  20. search_url = f"{base_url}t?q={serie_name}"
  21. response = requests.get(search_url, cookies=cookies)
  22. response.raise_for_status()
  23. expr = re.compile(".*((S[0-9]+)E[0-9]+).*(1080p).*")
  24. soup = BeautifulSoup(response.content, 'html.parser')
  25. results = soup.find_all('a', {'class': 'tTipWrap'})
  26. torrents = []
  27. for result in results:
  28. link = result['href']
  29. m = expr.search(link)
  30. if link.endswith('.torrent') and m is not None:
  31. torrents.append(Torrent(link, m.group(1), season=m.group(2)))
  32. return torrents
  33. def download_torrent(url: str, file_name: str) -> None:
  34. read = requests.get(url, cookies=cookies)
  35. with open(file_name, 'wb') as f:
  36. for chunk in read.iter_content(chunk_size=512):
  37. if chunk:
  38. f.write(chunk)
  39. #print(f"Downloaded {file_name} success")
  40. def main() -> None:
  41. if os.path.exists("downloaded.bin"):
  42. with open("downloaded.bin", "rb") as f:
  43. downloaded: List[Torrent] = pickle.load(f)
  44. else:
  45. downloaded = list()
  46. serie_name = input("Name serie: ")
  47. torrents = search_series(serie_name)
  48. season = ""
  49. if len(torrents) <= 0:
  50. print("torrent not found")
  51. return
  52. #choice = int(input("Qual serie? "))
  53. seasons = set()
  54. for to in torrents:
  55. seasons.add(to.season)
  56. for idx, s in enumerate(seasons):
  57. print(f"{idx + 1}, {s}")
  58. choice = int(input("What season? "))
  59. if 0 <= (choice -1 ) <= len(seasons):
  60. season = list(seasons)[choice -1]
  61. for torrent in torrents:
  62. if torrent.link.find(season):
  63. name = os.path.basename(torrent.link)
  64. got = list(filter(lambda x: (x.season_ep in torrent.season_ep), downloaded))
  65. if len(got) == 0:
  66. downloaded.append(torrent)
  67. print(f"Downloading {torrent.season} - {torrent.season_ep}")
  68. download_torrent(f"https://iptorrents.com/t/{torrent.link}", name)
  69. else:
  70. print("Escolha invalida")
  71. with open("downloaded.bin", 'wb') as f:
  72. pickle.dump(downloaded, f, protocol=pickle.HIGHEST_PROTOCOL)
  73. if __name__ == '__main__':
  74. main()