forked from nekocode/doubanfm-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fm.py
executable file
·355 lines (282 loc) · 10.4 KB
/
fm.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import subprocess
import requests
import urwid
import uuid
import random
import getpass
try:
input = raw_input
except NameError:
pass
class Song(object):
def __init__(self, song_json):
try:
self._parse(song_json)
except KeyError:
pass
def _parse(self, song_json):
self.sid = song_json['sid']
self.picture = song_json['picture']
self.artist = song_json['artist']
self.title = song_json['title']
if self.title.isupper():
self.title = self.title.title()
self.length_in_sec = song_json['length']
self.url = song_json['url']
@staticmethod
def parse(song_json):
return Song(song_json)
class Player(object):
def __init__(self):
self.is_playing = False
self.current_song = None
self.player_process = None
self.external_player = None
self._detect_external_players()
def _detect_external_players(self):
supported_external_players = [
["mpv", "--really-quiet"],
["mplayer", "-really-quiet"],
["mpg123", "-q"],
]
for external_player in supported_external_players:
proc = subprocess.Popen(
["which", external_player[0]],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
player_bin_path = proc.communicate()[0].strip()
if player_bin_path and os.path.exists(player_bin_path):
self.external_player = external_player
break
else:
print("No supported player(mpv/mplayer/mpg123) found. Exit.")
raise SystemExit()
def play(self, song):
if self.is_playing:
self.stop()
self.current_song = song
self.player_process = subprocess.Popen(
self.external_player + [self.current_song.url],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.is_playing = True
def stop(self):
self.is_playing = False
if self.player_process is None:
return
try:
self.player_process.terminate()
except:
pass
class DoubanFMApi:
API_HOST_URL = "https://api.douban.com"
TOKEN_HOST_URL = "https://www.douban.com"
APP_NAME = "radio_android"
VERSION = "642"
KEY = "02f7751a55066bcb08e65f4eff134361"
SECRET = "63cf04ebd7b0ff3b"
UUID = '408428bc' + str(uuid.uuid4()).replace('-', '')
REDIRECT_URI = 'http://douban.fm'
def __init__(self):
self.auth = None
def login(self, email, password):
rsp = requests.post('%s/service/auth2/token' % DoubanFMApi.TOKEN_HOST_URL, data={
'username': email,
'password': password,
'udid': DoubanFMApi.UUID,
'client_id': DoubanFMApi.KEY,
'client_secret': DoubanFMApi.SECRET,
'redirect_uri': DoubanFMApi.REDIRECT_URI,
'grant_type': 'password',
'apikey': DoubanFMApi.KEY,
}).json()
self.auth = "Bearer %s" % rsp['access_token']
def get_redheart_songs(self):
if self.auth is None:
return []
auth_header = {'Authorization': self.auth}
rsp = requests.get('%s/v2/fm/redheart/basic' % DoubanFMApi.API_HOST_URL, params={
'app_name': DoubanFMApi.APP_NAME,
'version': DoubanFMApi.VERSION,
}, headers=auth_header).json()
sids = ""
for sid in rsp['songs']:
if sid['playable'] is True:
sids += sid['sid'] + '|'
sids = sids[:-1]
rsp = requests.post('%s/v2/fm/songs' % DoubanFMApi.API_HOST_URL, data={
'sids': sids,
'kbps': '128',
'app_name': DoubanFMApi.APP_NAME,
'version': DoubanFMApi.VERSION,
'apikey': DoubanFMApi.KEY,
}, headers=auth_header).json()
return list(map(Song.parse, rsp))
class SongButton(urwid.Button):
def __init__(self, song, on_pressed_callback, index=0):
super(SongButton, self).__init__('', on_pressed_callback)
self.index = index
self.song = song
self.is_playing = False
self._text = urwid.SelectableIcon(
u'• %s - %s' % (song.title, song.artist),
cursor_position=0)
self._w = urwid.AttrMap(self._text, None, focus_map='reversed')
self.set_is_playing(self.is_playing)
# 设置按钮播放状态
def set_is_playing(self, is_playing):
self.is_playing = is_playing
if is_playing:
self._text.set_text(u'♫' + self._text.text[1:])
self._w.set_attr_map({None: 'playing'})
else:
self._text.set_text(u'•' + self._text.text[1:])
self._w.set_attr_map({'playing': None})
def mouse_event(self, size, event, button, x, y, focus):
# 屏蔽鼠标点击
pass
class SongListBox(urwid.ListBox):
def __init__(self, btns):
super(SongListBox, self).__init__(urwid.SimpleFocusListWalker(btns))
self._command_map['j'] = 'cursor down'
self._command_map['k'] = 'cursor up'
def keypress(self, size, key):
if key in ('up', 'down', 'page up', 'page down', 'enter', ' ', 'j', 'k'):
return super(SongListBox, self).keypress(size, key)
if key in ('q', 'Q', 'esc'):
# 发送退出信号
urwid.emit_signal(self, 'exit')
if key in ('s', 'S'):
# 停止播放
urwid.emit_signal(self, 'stop')
if key in ('left', 'right'):
# 下一首歌曲
urwid.emit_signal(self, 'next_song')
if key in ('m', 'M'):
# 切换模式
urwid.emit_signal(self, 'change_mode')
class UI:
LOOP_MODE = {
0: u'单曲循环',
1: u'全部循环',
2: u'随机播放',
}
def __init__(self):
self.player = Player()
self.btns = []
self.playing_btn = None
self.loop_mode = 0
self.next_song_alarm = None
self.main = None
self.index = 0
# 调色板
self.palette = [
('reversed', '', '', '', 'standout', ''),
('playing', '', '', '', 'bold, g7', '#d06'),
('title', '', '', '', 'bold, g7', '#d06'),
('loop_mode', '', '', '', 'bold, g7', '#d06'),
('red', '', '', '', 'bold, #d06', ''),
]
self._setup_ui()
def _setup_ui(self):
email = input('豆瓣账户 (Email地址): ')
password = getpass.getpass('豆瓣密码: ')
api = DoubanFMApi()
api.login(email, password)
songs = api.get_redheart_songs()
# 头部
self.title = urwid.Text('')
self._update_title()
divider = urwid.Divider()
header = urwid.Padding(urwid.Pile([divider, self.title, divider]), left=4, right=4)
# 歌曲列表
index = 0
for song in songs:
self.btns.append(SongButton(song, self._on_item_pressed, index))
index += 1
self.song_listbox = SongListBox(self.btns)
# 页面
self.main = urwid.Padding(
urwid.Frame(self.song_listbox, header=header, footer=divider),
left=4, right=4)
# 注册信号回调
urwid.register_signal(
SongListBox, ['exit', 'stop', 'next_song', 'change_mode'])
urwid.connect_signal(self.song_listbox, 'exit', self._on_exit)
urwid.connect_signal(self.song_listbox, 'stop', self.stop_song)
urwid.connect_signal(self.song_listbox, 'next_song', self.next_song)
urwid.connect_signal(self.song_listbox, 'change_mode', self.change_mode)
self.loop = urwid.MainLoop(self.main, palette=self.palette)
self.loop.screen.set_terminal_properties(colors=256)
def _update_title(self):
text = [
('title', u' ❤ 豆瓣 FM 红心歌曲 '),
('red', u' LOOP: '),
('loop_mode', u'%s' % UI.LOOP_MODE[self.loop_mode]),
]
if self.playing_btn is not None:
playing_song = self.playing_btn.song
text.extend([
('red', u'\n♫ %s - %s' % (playing_song.title, playing_song.artist)),
])
self.title.set_text(text)
def stop_song(self):
if self.playing_btn is not None:
self.playing_btn.set_is_playing(False)
self.index = self.playing_btn.index
self.playing_btn = None
self.player.stop()
self._update_title()
if self.next_song_alarm is not None:
self.loop.remove_alarm(self.next_song_alarm)
def next_song(self):
# 单曲循环
if self.loop_mode == 0:
# 处于未播放状态时
if self.playing_btn is not None:
self._on_item_pressed(self.playing_btn)
else:
self._on_item_pressed(self.btns[self.index])
# 全部循环
elif self.loop_mode == 1:
# 处于未播放状态时
if self.playing_btn is None:
index = self.index
else:
index = self.playing_btn.index + 1
if index >= len(self.btns):
index = 0
next_song_btn = self.btns[index]
self._on_item_pressed(next_song_btn)
# 随机播放
elif self.loop_mode == 2:
next_song_btn = self.btns[random.randint(0, len(self.btns) - 1)]
self._on_item_pressed(next_song_btn)
def change_mode(self):
if self.loop_mode < 2:
self.loop_mode += 1
else:
self.loop_mode = 0
self._update_title()
def _on_item_pressed(self, button):
if self.playing_btn is not None:
self.playing_btn.set_is_playing(False)
self.playing_btn = button
self.playing_btn.set_is_playing(True)
playing_song = self.playing_btn.song
self.player.play(playing_song)
self._update_title()
# 循环播放定时设置
if self.next_song_alarm is not None:
self.loop.remove_alarm(self.next_song_alarm)
self.next_song_alarm = self.loop.set_alarm_in(
playing_song.length_in_sec,
lambda loop, data: self.next_song(), None)
def _on_exit(self):
self.player.stop()
raise urwid.ExitMainLoop()
def run(self):
self.loop.run()
if __name__ == '__main__':
UI().run()