ソースを参照

feat: find torrent per season per episode

Douglas Andreani 1 年間 前
コミット
edb4480bca
1 ファイル変更59 行追加17 行削除
  1. 59 17
      app.py

+ 59 - 17
app.py

@@ -2,47 +2,89 @@ 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]:
 
-def search_series(serie_name: str) -> List[str]:
     base_url = "https://iptorrents.com/"
-    search_url = f"{base_url}search.php?cat=0search={serie_name}"
-    response = requests.get(search_url)
+    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': 'site-main'})
+    results = soup.find_all('a', {'class': 'tTipWrap'})
     torrents = []
+    
     for result in results:
         link = result['href']
-        if link.startswith('torrents'):
-            torrents.append(f"{base_url}{link}")
+        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:
-    response = requests.get(url)
+    read = requests.get(url, cookies=cookies)
     with open(file_name, 'wb') as f:
-        f.write(response.content)
-
+        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
-    for idx, torrent in enumerate(torrents):
-        print(f"{idx+1}. {torrent}")
-    choice = int(input("Qual serie? "))
-    if 0 <= choice < len(torrents):
-        torrent_url = torrents[choice]
-        torrent_name = f"{os.path.basename(torrent_url)}.torrent"
-        download_torrent(torrent_url, torrent_name)
-        print(f"downloaded torrent {torrent_name}")
+    #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()