Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/qbittorrent/index.py

359 lines
8.6 KiB

6 years ago
# coding: utf-8
import time
import random
import os
import json
import re
import sys
sys.path.append(os.getcwd() + "/class/core")
import mw
6 years ago
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append('/usr/local/lib/python2.7/site-packages')
app_debug = False
if mw.isAppleSystem():
6 years ago
app_debug = True
def getPluginName():
return 'qbittorrent'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
6 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
6 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
6 years ago
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
def getSqlFile():
file = getPluginDir() + "/conf/qb.sql"
return file
def getRsyncShell():
file = getServerDir() + "/workers/rsync.sh"
return file
def getConf():
file = getServerDir() + "/qb.conf"
return file
def getRunLog():
file = getServerDir() + "/logs.pl"
return file
def contentReplace(content):
service_path = mw.getServerDir()
6 years ago
content = content.replace('{$SERVER_PATH}', service_path)
5 years ago
content = content.replace('{$WWW_PATH}', mw.getRootDir() + "/wwwroot")
6 years ago
return content
def initDreplace():
ddir = getServerDir() + '/workers'
if not os.path.exists(ddir):
sdir = getPluginDir() + '/workers'
mw.execShell('cp -rf ' + sdir + ' ' + getServerDir())
6 years ago
cfg = getServerDir() + '/qb.conf'
if not os.path.exists(cfg):
cfg_tpl = getPluginDir() + '/conf/qb.conf'
content = mw.readFile(cfg_tpl)
6 years ago
content = contentReplace(content)
mw.writeFile(cfg, content)
6 years ago
file_tpl = getInitDTpl()
service_path = os.path.dirname(os.getcwd())
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
# initd replace
if not os.path.exists(file_bin):
content = mw.readFile(file_tpl)
6 years ago
content = contentReplace(content)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
6 years ago
return file_bin
def status():
data = mw.execShell(
6 years ago
"ps -ef|grep qbittorrent_worker | grep -v grep | awk '{print $2}'")
if data[0] == '':
return 'stop'
return 'start'
def start():
cmd = "ps -ef | grep qbittorrent-nox |grep -v grep |awk '{print $2}'"
ret = mw.execShell(cmd)
6 years ago
if ret[0] == '':
mw.execShell('qbittorrent-nox -d')
6 years ago
file = initDreplace()
data = mw.execShell(file + ' start')
6 years ago
if data[1] == '':
return 'ok'
return data[1]
def stop():
file = initDreplace()
data = mw.execShell(file + ' stop')
6 years ago
# cmd = "ps -ef | grep qbittorrent-nox |grep -v grep |awk '{print $2}' | xargs kill"
# mw.execShell(cmd)
6 years ago
if data[1] == '':
return 'ok'
return data[1]
def restart():
file = initDreplace()
data = mw.execShell(file + ' restart')
6 years ago
if data[1] == '':
return 'ok'
return data[1]
def reload():
file = initDreplace()
data = mw.execShell(file + ' reload')
6 years ago
if data[1] == '':
return 'ok'
return data[1]
def initdStatus():
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
def initdInstall():
import shutil
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
mysql_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(mysql_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
6 years ago
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
initd_bin = getInitDFile()
os.remove(initd_bin)
mw.execShell('chkconfig --del ' + getPluginName())
6 years ago
return 'ok'
def matchData(reg, content):
tmp = re.search(reg, content).groups()
return tmp[0]
def getDbConfInfo():
cfg = getConf()
content = mw.readFile(cfg)
6 years ago
data = {}
data['DB_HOST'] = matchData("DB_HOST\s*=\s(.*)", content)
data['DB_USER'] = matchData("DB_USER\s*=\s(.*)", content)
data['DB_PORT'] = matchData("DB_PORT\s*=\s(.*)", content)
data['DB_PASS'] = matchData("DB_PASS\s*=\s(.*)", content)
data['DB_NAME'] = matchData("DB_NAME\s*=\s(.*)", content)
return data
def getQbConf():
cfg = getConf()
content = mw.readFile(cfg)
6 years ago
data = {}
data['QB_HOST'] = matchData("QB_HOST\s*=\s(.*)", content)
data['QB_PORT'] = matchData("QB_PORT\s*=\s(.*)", content)
data['QB_USER'] = matchData("QB_USER\s*=\s(.*)", content)
data['QB_PWD'] = matchData("QB_PWD\s*=\s(.*)", content)
return data
def pMysqlDb():
data = getDbConfInfo()
conn = mysql.mysql()
conn.setHost(data['DB_HOST'])
conn.setUser(data['DB_USER'])
conn.setPwd(data['DB_PASS'])
conn.setPort(int(data['DB_PORT']))
conn.setDb(data['DB_NAME'])
return conn
def pQbClient():
from qbittorrent import Client
info = getQbConf()
url = 'http://' + info['QB_HOST'] + ':' + info['QB_PORT'] + '/'
qb = Client(url)
qb.login(info['QB_USER'], info['QB_PWD'])
return qb
def getQbUrl():
info = getQbConf()
url = 'http://' + info['QB_HOST'] + ':' + info['QB_PORT'] + '/'
return mw.returnJson(True, 'ok', url)
6 years ago
def qbList():
args = getArgs()
# data = checkArgs(args, ['type'])
# if not data[0]:
# return data[1]
args_type = ''
if 'type' in args:
args_type = args['type']
f = ['downloading', 'completed']
tfilter = ''
if args_type in f:
tfilter = args['type']
try:
qb = pQbClient()
torrents = qb.torrents(filter=tfilter)
data = {}
data['type'] = tfilter
data['torrents'] = torrents
return mw.returnJson(True, 'ok', data)
6 years ago
except Exception as e:
return mw.returnJson(False, str(e))
6 years ago
def qbDel():
args = getArgs()
data = checkArgs(args, ['hash'])
if not data[0]:
return data[1]
qb = pQbClient()
data = qb.delete(args['hash'])
return mw.returnJson(True, '操作成功!', data)
6 years ago
def qbAdd():
args = getArgs()
data = checkArgs(args, ['hash'])
if not data[0]:
return data[1]
url = 'magnet:?xt=urn:btih:' + args['hash']
qb = pQbClient()
data = qb.download_from_link(url)
return mw.returnJson(True, '操作成功!', data)
6 years ago
def test():
qb = pQbClient()
# magnet_link = "magnet:?xt=urn:btih:57a0ec92a61c60585f1b7a206a75798aa69285a5"
# print qb.download_from_link(magnet_link)
torrents = qb.torrents(filter='downloading')
for torrent in torrents:
print mw.returnJson(False, torrent)
6 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print status()
elif func == 'start':
print start()
elif func == 'stop':
print stop()
elif func == 'restart':
print restart()
elif func == 'reload':
print reload()
elif func == 'initd_status':
print initdStatus()
elif func == 'initd_install':
print initdInstall()
elif func == 'initd_uninstall':
print initdUinstall()
elif func == 'get_sql':
print getSqlFile()
elif func == 'rsync_shell':
print getRsyncShell()
elif func == 'conf':
print getConf()
elif func == 'get_run_Log':
print getRunLog()
elif func == 'qb_list':
print qbList()
elif func == 'qb_del':
print qbDel()
elif func == 'qb_add':
print qbAdd()
elif func == 'qb_url':
print getQbUrl()
elif func == 'test':
print test()
else:
print 'error'