-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAria2Manager.py
executable file
·153 lines (127 loc) · 4.83 KB
/
Aria2Manager.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
#
# <A PySide Gui for Aria2 Download Manager>
# Copyright (C) 2013 a.atalla <a.atalla [at] hacari [dot] org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import psutil
import subprocess
import xmlrpclib as rpc
HOME_DIR = os.environ['HOME']
CONFIG_DIR= HOME_DIR+'/.barq' # You can change the dir name as you want
class Aria2Manager:
def __init__(self):
self._PID = self.get_PID()
self.connection=rpc.ServerProxy('http://localhost:6800/rpc')
if self.isRunning():
print 'Aria2 is Running with PID = ',self._PID
def get_PID(self):
###
# return the system PID for 'aria2c' process
###
proc_list = psutil.get_process_list()
for proc in proc_list:
if proc.name == 'aria2c':
return proc.pid
return None
def isRunning(self):
if self.get_PID()==None:
return False
return True
def startAria2(self):
args = ['aria2c','-D','--enable-rpc','--continue=true']
if os.path.exists(CONFIG_DIR+'/session.ini'):
print CONFIG_DIR,'is exist'
args.append('--input-file='+CONFIG_DIR+'/session.ini')
else:
if not os.path.exists(CONFIG_DIR):
print 'create config folder'
os.mkdir(CONFIG_DIR)
args.append('--save-session='+CONFIG_DIR+'/session.ini')
args.append('--save-session-interval=30') # This option work with aria2 >= 1.17
args.append('--always-resume=true')
print (args)
subprocess.call(args)
self._PID = self.get_PID()
def stopAria2(self):
if self.isRunning():
print 'Aria2 will shutdown ===> ',self._PID
#self.connection.aria2.forceShutdown()
os.system("killall aria2c")
def addUris(self,uris,params):
'''
Start new download by adding new uri/uris with options
uris: is a list (one or more ) uri for the downloaded file
params: Dictionary of the download options such as (max-download-limit,max-connection-per-server,.....etc)
'''
self.connection.aria2.addUri(uris,params)
def getGlobalStatus(self):
return self.connection.aria2.getGlobalStat()
def getAllGids(self):
active = self.connection.aria2.tellActive(['gid'])
waiting = self.connection.aria2.tellWaiting(0,100,['gid'])
stopped = self.connection.aria2.tellStopped(0,100,['gid'])
allGids = [active,waiting,stopped] # This is a list of 3 lists
return allGids
def getOptions(self,gid):
return self.connection.aria2.getOption(gid)
def changeGlobalOption(self,options):
'''
option is of type dictionary for example: {'max-overall-download-limit':'20K'}
Avaliable globalOption in aria2:
download-result
log
log-level
max-concurrent-downloads
max-download-result
max-overall-download-limit
max-overall-upload-limit
save-cookies
save-session
server-stat-of
'''
self.connection.aria2.changeGlobalOption(options)
def changeOption(self,gid,options):
'''
available options to be changed
bt-max-peers
bt-request-peer-speed-limit
bt-remove-unselected-file
force-save
max-download-limit
max-upload-limit
'''
self.connection.aria2.changeOption(gid,options)
def pauseAllDownloads(self):
self.connection.aria2.forcePauseAll()
def unpauseAllDownloads(self):
self.connection.aria2.unpauseAll()
def pauseSingleDownload(self,gid):
self.connection.aria2.pause(gid)
def unpauseSingleDownload(self,gid):
self.connection.aria2.unpause(gid)
def removeSingleDownload(self,gid):
self.connection.aria2.remove(gid)
def cleanDownloadList(self):
self.connection.aria2.purgeDownloadResult()
def getDownloadDetail(self,gid,detail):
status = self.connection.aria2.tellStatus(gid)
detail_value = status.get(detail)
return detail_value
def getUrisDetails(self,gid):
status = self.connection.aria2.tellStatus(gid)
#p.pprint (status)
return status.get('files')[0].get('uris')