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("Connessione a Pyload rifiutata.")
40
41 print("Link da aggiungere manualmente:\n")
42 for x in links:
43 print(x)
44 print()
45 return []
46
47def main():
48 newspapers = get_newspaper(NEWSPAPER_PREFIX, 0) # 0 -> today
49 name = NEWSPAPER_PREFIX + datetime.today().strftime("%Y-%m-%d")
50 links = get_sorted_links(newspapers)
51 pids = handle_links(name, links)
52
53if __name__ == "__main__":
54 exit(main())