main.py (view raw)
1
2from urllib.error import URLError
3from datetime import datetime
4from os import getenv
5from dotenv import load_dotenv
6load_dotenv()
7from Overpost import get_newspaper
8from MyPyload import Pyload
9
10NEWSPAPER_PREFIX = getenv("NEWSPAPER_PREFIX") or ""
11HOST_PREFERENCE = [ 'katfile.com', 'rapidgator.net', 'www.easybytez.com' ]
12
13def scroll_list(array, buffer=1000):
14 array_len = len(array)
15 i = 0
16 while i < buffer:
17 if i >= array_len:
18 i = 0
19 yield array[i]
20 i += 1
21
22def get_host(link):
23 return link.split("/")[2]
24
25def filter_links(links, hosts):
26 host = next(hosts)
27 for link in links:
28 if get_host(link) == host:
29 return link
30 return filter_links(links, hosts)
31
32def get_sorted_links(dictionary):
33 hosts = scroll_list(HOST_PREFERENCE)
34 return [ filter_links(links, hosts) for _, links in dictionary.items() ]
35
36def download_link(connection, name, link):
37 return connection.addPackage(name=name, links=[link])
38
39def handle_links(name, links):
40 try:
41 con = Pyload()
42 return [ download_link(con, name, link) for link in links ]
43 except URLError:
44 print("\nConnessione a Pyload rifiutata.")
45
46 print(len(links), "link da aggiungere manualmente:\n")
47 for link in links:
48 print(link)
49 print()
50 return []
51
52def main():
53 newspapers = get_newspaper(NEWSPAPER_PREFIX, 0) # 0 -> today
54 name = f"{NEWSPAPER_PREFIX} - {datetime.today().strftime('%Y-%m-%d')}"
55 links = get_sorted_links(newspapers)
56 pids = handle_links(name, links)
57 print(len(pids), "link aggiunti a Pyload.")
58 print("Premi INVIO per uscire.")
59 input()
60
61if __name__ == "__main__":
62 exit(main())