-
Notifications
You must be signed in to change notification settings - Fork 11
/
youku_speed_up.py
executable file
·132 lines (108 loc) · 3.76 KB
/
youku_speed_up.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
#!/usr/bin/env python
# coding=utf-8
passport = '15000000000' #帐号
password = '000000' #密码
import requests
import json
import re
import hashlib
import logging
import os
import pickle
import sys
log_path = '' #日志路径
if len(log_path) > 0:
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=log_path, logging='a', level=logging.DEBUG)
else:
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
session = requests.session()
# session.headers = {
# 'Referer': 'http://vip.youku.com/vips/priSpeedup.html',
# 'User-Agent': 'Mozilla/5.0'
# }
actions = {
# 'getConfig': 'http://account.youku.com/getConfig.json',
'refreshFormToken': 'http://account.youku.com/refreshFormToken.json',
'login': 'http://account.youku.com/login/confirm.json',
# http://static.youku.com/paymentcenter/vip-pc/build/js/libs/config-build.js
'get_member_info': 'http://vip.youku.com/ajax/member/get_member_info.jsonp', # 获取用户基本信息
'speed_up': 'http://vip.youku.com/?c=ajax&a=ajax_do_speed_up', # 宽带加速
'switch_service_url': 'http://vip.youku.com/?c=ajax&a=ajax_speedup_service_switch', # 加速服务开关
'speed_status': 'http://vip.youku.com/ajax/speedup/get_status.jsonp', # 用户的宽带加速状态判断
}
def loads_jsonp(_jsonp):
return json.loads(re.match('.*?({.*}).*', _jsonp, re.S).group(1))
def login():
cookie_file_name = '%s.cookie'%passport
if os.path.isfile(cookie_file_name):
with open(cookie_file_name) as f:
session.cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
response = session.get(actions['speed_status'])
logging.debug('Response: %s'%response.text)
data = loads_jsonp(response.text)
if data['code'] == '20000':
return True
logging.info(data['msg'])
session.cookies = requests.utils.cookiejar_from_dict({})
response = session.get(actions['refreshFormToken'])
logging.debug('Response: %s'%response.text)
token = loads_jsonp(response.text)['data']['formtoken']
# 当前IP
response = requests.get('http://ip.cn', headers = {'User-Agent': 'curl/7.54.0'})
logging.info(response.text.strip())
m = hashlib.md5()
m.update(password)
md5_password = m.hexdigest()
params = {
'formtoken': token,
'passport': passport,
'password': md5_password,
'loginType': 'passport_pwd',
'UA': 'FFFF00000000016514CE',
'jsToken': '0',
}
response = session.get(actions['login'], params = params)
logging.debug('Response: %s'%response.text)
data = loads_jsonp(response.text)
if data['result'] == 'success':
logging.info(u'登录成功')
with open(cookie_file_name, 'w') as f:
pickle.dump(requests.utils.dict_from_cookiejar(session.cookies), f)
return True
else:
logging.info(data['errorMsg'])
return False
def get_status():
response = session.get(actions['speed_status'])
logging.debug('Response: %s'%response.text)
data = loads_jsonp(response.text)['result']
return data
def speed_up():
response = session.get(actions['speed_up'])
logging.debug('Response: %s'%response.text)
data = response.json()
logging.info(data['msg'])
if 'result' in data and 'operator_error_msg' in data['result']:
logging.info(data['result']['operator_error_msg'])
def switch():
response = session.get(actions['switch_service_url'])
logging.debug('Response: %s'%response.text)
data = response.json()
logging.info(data['msg'])
def main():
if login() == False:
return
status = get_status()
if status['speed_up_state'] == True:
logging.info(u'宽带加速已开启')
return
if status['speed_up_switch'] == 2:
logging.info(u'开启[加速服务开关]...')
switch()
logging.info(u'开启[宽带加速]...')
speed_up()
if __name__ == '__main__':
#不加下面2句好像在win环境下会报错
reload(sys)
sys.setdefaultencoding('utf-8')
main()