-
Notifications
You must be signed in to change notification settings - Fork 1
/
ycxunlei.py
149 lines (125 loc) · 5.01 KB
/
ycxunlei.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
# -*- coding: utf-8 -*-
import requests
import json
from six.moves.urllib.parse import quote
#python2 改变标准输出的默认编码
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#python3 改变标准输出的默认编码
#import io
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
class YCXunlei(object):
def __init__(self, cookie_str=r'this is a cookie'):
'''
YCXunlei constructor.
cookie_str: the cookie string after login yuancheng.xunlei.com
'''
self.cookie_str = cookie_str
url = 'http://homecloud.yuancheng.xunlei.com/listPeer?'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36','cookie': self.cookie_str}
req = requests.get(url, headers=headers)
result = req.json()
if result['rtn']==0:
self.login = True
self.devices = result['peerList']
else:
self.login = False
self.devices = None
def isLogin(self):
'''
check if the cookie string is valid
'''
return self.login
def getDevices(self):
'''
get remote devices
return: a list of the devices include name,pid,online....
'''
return self.devices
def getPid(self, name):
'''
get pid number of a device by device name
return None if no matched name found
'''
for device in self.devices:
if device['name'] == name:
return device['pid']
def getTasks(self,pid):
'''
get the xunlei tasks on device
pid: the pid of the device
'''
url = 'http://homecloud.yuancheng.xunlei.com/list?pid='+pid+'&type=0&pos=0&number=10&needUrl=1&v=2'
headers = {'cookie': self.cookie_str,'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
req = requests.get(url, headers=headers)
result = req.json()
if result['rtn']==0:
return result
else:
return None
def addTask(self, pid, links):
'''
add download task to yuancheng xunlei
pid: the remote device pid number
links: an array contain download urls
'''
url = 'http://homecloud.yuancheng.xunlei.com/createTask?pid='+pid+'&v=2&ct=0'
tasks = []
for link in links:
tasks.append({"url":link,"gcid":"","cid":""})
data = {"path":"C:/影视/","tasks":tasks}
# print(links)
# data = {"path":"C:/影视/","tasks":[{"url":links,"gcid":"","cid":""}]}
data = json.dumps(data)
data = quote(data)
data = 'json=' + data
data = data.encode('utf-8')
headers = {'cookie': self.cookie_str,"Content-Type": "application/x-www-form-urlencoded"}
req = requests.post(url, headers=headers, data = data)
result = req.json()
return result
def getFileInfo(self,pid,link):
'''
get file name and file size of a download url
pid:the remote device pid number
link: download url
'''
url = 'http://homecloud.yuancheng.xunlei.com/urlResolve?pid='+pid+'&v=2&ct=0'
data = {"url":link,}
data = json.dumps(data)
data = quote(data)
data = 'json=' + data
data = data.encode('utf-8')
headers = {'cookie': self.cookie_str,"Content-Type": "application/x-www-form-urlencoded"}
req = requests.post(url, headers=headers, data = data)
result = req.json()
if result['rtn']==0:
return result#['taskInfo']['name']
else:
return None
def delTask(self,pid,id):
'''
del task
pid:the remote device pid number
id: task id
'''
url = 'http://homecloud.yuancheng.xunlei.com/del?pid='+pid+'&tasks='+id+'_0&recycleTask=1&deleteFile=false&v=2&ct=0'
headers={'cookie': self.cookie_str, 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
req = request.get(url, headers=headers)
result = req.json()
return result
def getStatus(self,pid):
tasks = self.getTasks(pid)['tasks']
returncontent = ''
for task in tasks:
size = task['size']/1048576.0
unit = 'MB'
if size > 1024.0:
size = size/1024.0
unit = 'GB'
time = task['remainTime']
m, s = divmod(time, 60)
h, m = divmod(m, 60)
returncontent = returncontent + task['name']+' %.2f '%size + unit +'\n'+u'下载速度:%.2f KB/s\n'%(task['speed']/1024)+u'下载进度: %.2f%%\n'%(task['progress']/100.0)+u'剩余时间:%02d:%02d:%02d \n\n' % (h, m, s)
return returncontent