/
a2jrg
executable file
·109 lines (81 loc) · 2.39 KB
/
a2jrg
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import Aria2JsonRpc
import argparse
import getpass
import os
import time
################################### Globals ####################################
ID = 'a2jrg'
################################### argparse ###################################
parser = argparse.ArgumentParser(
description='Queue downloads via the Aria2 RPC interface.'
)
parser.add_argument(
'uris', metavar='<URI>', nargs='*',
help='File URIs. These must all point to the same file.'
)
group = parser.add_argument_group('server options')
Aria2JsonRpc.add_server_arguments(group)
group = parser.add_argument_group('download options')
group.add_argument(
'-o', '--options', nargs=argparse.REMAINDER,
help='Aria2 options as key-value pairs separated by "=".'
)
group = parser.add_argument_group('commands')
group.add_argument(
'--pause-all', action='store_true',
help='Pause all downloads.'
)
group.add_argument(
'--unpause-all', action='store_true',
help='Unpause all downloads.'
)
group.add_argument(
'--purge', action='store_true',
help='Purge download results.'
)
group = parser.add_argument_group('other options')
group.add_argument(
'-w', '--wait', action='store_true',
help='Wait for the download to finish'
)
group.add_argument(
'-i', '--interval', type=int, default=1,
help='Status query interval while waiting, in seconds. Default: %(default)s.'
)
group.add_argument(
'--status', action='store_true',
help='Print global status report.'
)
################################### Actions ####################################
def queue_uris(a2jr, args):
options = dict()
if args.options:
for option in args.options:
key, value = option.split('=', 1)
options[key] = value
if 'dir' not in options:
options['dir'] = os.getcwd()
if args.wait:
a2jr.queue_uris(args.uris, options, interval=args.interval)
else:
a2jr.queue_uris(args.uris, options)
##################################### Main #####################################
def main(args=None):
pargs = parser.parse_args(args)
a2jr = Aria2JsonRpc.a2jr_from_args(ID, pargs)
if pargs.pause_all:
a2jr.pauseAll()
elif pargs.unpause_all:
a2jr.unpauseAll()
if pargs.status:
a2jr.print_status()
if pargs.uris:
queue_uris(a2jr, pargs)
if pargs.purge:
a2jr.purgeDownloadResult()
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, BrokenPipeError):
pass