-
Notifications
You must be signed in to change notification settings - Fork 3
/
__main__.py
74 lines (56 loc) · 1.73 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import queue
import sys
import threading
import dialogs
import downloader
import server
import sessionpool
import utils
# TODO: These are broken. Figure out a way to make them work.
EXIT_COMMANDS = ['bye', 'exit', 'kill', 'quit']
MESSAGE_FORMAT = (
'Size: %s MB\n'
'Time elapsed: %dm %ds\n'
'Average download speed: %.2f MB/s')
KB = 1024
MB = 1024*KB
ip_addresses = utils.get_ip_addresses()
session_pool = sessionpool.SessionPool(ip_addresses)
message_queue = queue.Queue()
def listen():
while True:
user_input = input()
if user_input.lower() in EXIT_COMMANDS:
message_queue.put((sys.exit,))
sys.exit()
threading.Thread(target=start, args=(user_input,)).start()
def start(url, path=None):
if path is None:
try:
url, path = url.split(' ', 1)
except ValueError:
path = ''
dtask = downloader.DownloadTask(session_pool, url)
message_queue.put((saveas, dtask, path))
def saveas(dtask, path):
if not path:
path = dialogs.saveas_dialog(initialfile=dtask.name)
if path:
dtask.path = path
threading.Thread(target=wait, args=(dtask,)).start()
def wait(dtask):
dtask.run()
message_queue.put((complete, dtask))
def complete(dtask):
mins, secs = divmod(dtask.time_elapsed, 60)
avg_speed = dtask.size/MB/dtask.time_elapsed
message = MESSAGE_FORMAT % (dtask.size//MB, mins, secs, avg_speed)
dialogs.DownloadCompleteDialog(dtask.url, dtask.path, message)
def main():
threading.Thread(target=server.run, args=(start,)).start()
threading.Thread(target=listen).start()
while True:
callback, *args = message_queue.get()
callback(*args)
if __name__ == '__main__':
main()