all repos — sunstroke @ dc852443b5738a07c869b612ca3ed6db9d3c3777

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        print(link, host)
29        if get_host(link) == host:
30            return link
31    return filter_links(links, hosts)
32        
33        
34def get_sorted_links(dictionary):
35    hosts = scroll_list(HOST_PREFERENCE)
36    return [ filter_links(links, hosts) for _, links in dictionary.items() ]
37
38def download_link(connection, name, link):
39    return connection.addPackage(name=name, links=[link])
40
41def handle_links(name, links):
42    try:
43        con = Pyload()
44        return [ download_link(con, name, link) for link in links ]
45    except URLError:
46        print("\nConnessione a Pyload rifiutata.")
47
48    print(len(links), "link da aggiungere manualmente:\n")
49    for link in links:
50        print(link)
51    print()
52    return []
53
54def main():
55    newspapers = get_newspaper(NEWSPAPER_PREFIX, 0) # 0 -> today
56    name = f"{NEWSPAPER_PREFIX} - {datetime.today().strftime('%Y-%m-%d')}"
57    links = get_sorted_links(newspapers)
58    pids = handle_links(name, links)
59    print(len(pids), "link aggiunti a Pyload.")
60    print("Premi INVIO per uscire.")
61    input()
62
63if __name__ == "__main__":
64    exit(main())