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