-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.py
75 lines (60 loc) · 2.36 KB
/
info.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
import logging
import urllib2
import json
import time
is_vip_url = "http://www.xiami.com/vip/role"
stat_url_temp = "http://www.xiami.com/count/playstat?type=%d&vip_role=%d&song_id=%s"
record_play_url_temp = "http://www.xiami.com/count/playrecord?object=%s&sid=%s&object_name=%s&ishq=%d&t=%d" # noqa
record_play_simple_url_temp = "http://www.xiami.com/count/playrecord?sid=%s&type=%s&ishq=%d" # noqa
logger = logging.getLogger('info')
STAT_BEGIN = 0
STAT_NEAREND = 2
STAT_END = 3
def update_state(state, config_node):
for child in config_node:
if child.tag == 'nick_name':
state['user_nick'] = child.text
elif child.tag == 'user_id':
state['user_id'] = child.text
elif child.tag == 'vip':
state['vip'] = child.text
elif child.tag == 'vip_role':
state['vip_role'] = child.text
def get_xiamitoken(state):
for cookie in state['cookiejar']:
if cookie.name == '_xiamitoken':
logger.debug('_xiamitoken = %s' % cookie.value)
return cookie.value
raise Exception("fail to find xiami token!")
def authenticated(state):
for cookie in state['cookiejar']:
if cookie.name == 'member_auth':
logger.debug('already authenticated')
return True
logger.debug('not authenticated')
return False
def is_vip(state):
if 'vip' in state:
return state['vip']
isvip_resp = urllib2.urlopen(is_vip_url).read()
isvip_parsed = json.loads(isvip_resp)
if 'status' not in isvip_parsed or isvip_parsed['status'] != 1:
if 'message' in isvip_parsed:
raise Exception(u"fail to check vip status: %s" % isvip_parsed['message'])
else:
raise Exception("fail to check vip status")
result = isvip_parsed['data']['vip'] == 1
state['vip'] = result
return result
def add_stat(state, pos, song_id):
stat_url = stat_url_temp % (pos, 1 if is_vip(state) else 0, song_id)
urllib2.urlopen(stat_url).read()
def record_play(state, song_id, object_name, is_hq, play_type):
hq_arg = 1 if is_hq else 0
if object_name:
record_play_url = record_play_url_temp % (
song_id, song_id, object_name, hq_arg, int(time.time() * 1000))
else:
record_play_url = record_play_simple_url_temp % (
song_id, play_type, hq_arg)
urllib2.urlopen(record_play_url).read()