forked from skyline75489/pyfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
douban.py
executable file
·129 lines (109 loc) · 4.58 KB
/
douban.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
class Douban:
""" Douban class, provides some APIs.
"""
def __init__(self, email, password, user_id=None, expire=None, token=None, user_name=None):
self.login_url = 'http://www.douban.com/j/app/login'
self.channel_url = 'http://www.douban.com/j/app/radio/channels'
self.api_url = 'http://www.douban.com/j/app/radio/people'
self.app_name = 'radio_desktop_win'
self.version = '100'
self.typeMap = {
'new': 'n',
'playing': 'p',
'rate': 'r',
'unrate': 'u',
'end': 'e',
'bye': 'b',
'skip': 's',
}
self.email = email
self.password = password
self.user_id = user_id
self.expire = expire
self.token = token
self.user_name = user_name
self.channels = None
def _do_api_request(self, _type, sid=None, channel=None, kbps=64):
payload = {'app_name': self.app_name, 'version': self.version, 'user_id': self.user_id,
'expire': self.expire, 'token': self.token, 'sid': sid, 'h': '','channel': channel, 'type': _type}
r = requests.get(self.api_url, params=payload)
return r
def do_login(self):
payload = {'email': self.email, 'password': self.password, 'app_name': self.app_name, 'version': self.version}
r = requests.post(self.login_url, params=payload, headers={'Content-Type': 'application/x-www-form-urlencoded'})
if r.json()['r'] == 0:
self.user_name = r.json()['user_name']
self.user_id = r.json()['user_id']
self.expire = r.json()['expire']
self.token = r.json()['token']
return True, None
else:
return False, r.json()['err']
def _get_type(self, option):
return self.typeMap[option]
def get_channels(self):
""" Return a list of channels
"""
if self.channels is None:
r = requests.get(self.channel_url)
# Cache channels
self.channels = r.json()['channels']
return self.channels
return self.channels
def get_new_play_list(self, channel, kbps=64):
_type = self._get_type('new')
payload = {'app_name': self.app_name, 'version': self.version, 'user_id': self.user_id,
'expire': self.expire, 'token': self.token, 'sid': '', 'h': '','channel': channel, 'kbps': kbps, 'type': _type}
r = requests.get(self.api_url, params=payload)
if r.json()['r'] == 0:
songs = r.json()['song']
return songs
def get_playing_list(self, sid, channel, kbps=64):
_type = self._get_type('playing')
payload = {'app_name': self.app_name, 'version': self.version, 'user_id': self.user_id,
'expire': self.expire, 'token': self.token, 'sid': sid, 'h': '','channel': channel, 'kbps': kbps, 'type': _type}
r = requests.get(self.api_url, params=payload)
if r.json()['r'] == 0:
songs = r.json()['song']
return songs
def rate_song(self, sid, channel):
_type = self._get_type('rate')
r = self._do_api_request(sid=sid, channel=channel, _type=_type)
if r.json()['r'] == 0:
return True, None
else:
return False, r.json()['err']
def unrate_song(self, sid, channel):
_type = self._get_type('unrate')
r = self._do_api_request(sid=sid, channel=channel, _type=_type)
if r.json()['r'] == 0:
return True, None
else:
return False, r.json()['err']
def skip_song(self, sid, channel):
_type = self._get_type('skip')
r = self._do_api_request(sid=sid, channel=channel, _type=_type)
if r.json()['r'] == 0:
return True, None
else:
return False, r.json()['err']
def end_song(self, sid, channel):
_type = self._get_type('end')
r = self._do_api_request(sid=sid, channel=channel, _type=_type)
if r.json()['r'] == 0:
return True, None
else:
return False, r.json()['err']
def bye_song(self, sid, channel):
"""No longer play this song
"""
_type = self._get_type('bye')
r = self._do_api_request(sid=sid, channel=channel, _type=_type)
if r.json()['r'] == 0:
return True, None
else:
return False, r.json()['err']