-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
492 lines (398 loc) · 17.6 KB
/
__init__.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Copyright (C) 2008-2012 Sun Ning <[email protected]>
# Copyright (C) 2012 Yu Shijun <[email protected]>
# Copyright (C) 2012 Liu Guyue <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# The developers of the Exaile media player hereby grant permission
# for non-GPL compatible GStreamer and Exaile plugins to be used and
# distributed together with GStreamer and Exaile. This permission is
# above and beyond the permissions granted by the GPL license by which
# Exaile is covered. If you modify this code, you may extend this
# exception to your version of the code, but you are not obligated to
# do so. If you do not wish to do so, delete this exception statement
# from your version.
from libdoubanfm import DoubanFM, DoubanTrack, DoubanLoginException
from doubanfm_mode import DoubanFMMode
from doubanfm_cover import DoubanFMCover
from doubanfm_dbus import DoubanFMDBusController
from captcha_dialog import CaptchaDialog
import dbfm_pref
import gtk
import time
import urllib
from string import Template
from xl import common, event, main, playlist, xdg, settings, trax, providers, player
from xl.radio import *
from xl.nls import gettext as _
from xl.trax import Track
from xlgui import guiutil
from xlgui.accelerators import Accelerator
from xlgui.widgets import menu, menuitems, dialogs, playlist, notebook
DOUBANFM = None
def enable(exaile):
if exaile.loading:
event.add_callback(_enable, "exaile_loaded")
else:
_enable(None, exaile, None)
def _enable(device, exaile, nothing):
global DOUBANFM
DOUBANFM = DoubanRadioPlugin(exaile)
def disable(exaile):
global DOUBANFM
DOUBANFM.destroy(exaile)
def get_preferences_pane():
return dbfm_pref
SHARE_TEMPLATE = {'kaixin001': "http://www.kaixin001.com/repaste/bshare.php?rurl=%s&rcontent=&rtitle=%s",
'renren': "http://www.connect.renren.com/share/sharer?title=%s&url=%s",
'sina': "http://v.t.sina.com.cn/share/share.php?appkey=3015934887&url=%s&title=%s&source=&sourceUrl=&content=utf-8&pic=%s",
'twitter': "http://twitter.com/share?text=%s&url=%s",
'fanfou': "http://fanfou.com/sharer?u=%s&t=%s&d=&s=bm",
'douban': "http://shuo.douban.com/!service/share?name=%s&href=%s&image=%s&text=&desc=(%s)&apikey=0ace3f74eb3bd5d8206abe5ec1b38188&target_type=rec&target_action=0&object_kind=3043&object_id=%s"}
class DoubanRadioPlugin(object):
@common.threaded
def __init__(self, exaile):
self.exaile = exaile
## mark if a track is skipped instead of end normally
self.skipped = False
self.pre_init()
def pre_init(self):
self.captcha_dialog = None
self.__create_pre_init_menu_item()
@common.threaded
def do_init(self, captcha_id=None, captcha_solution=None):
username = settings.get_option("plugin/douban_radio/username")
password = settings.get_option("plugin/douban_radio/password")
try:
self.doubanfm = DoubanFM(username, password, captcha_id, captcha_solution)
except DoubanLoginException as e:
if e.data['captcha_id'] is None:
self.exaile.gui.main.message.show_error(
_('Douban FM Error'),
_('Failed to login to douban.fm with your credential'))
return
else:
captcha_id = e.data['captcha_id']
self.show_captcha_dialog(captcha_id)
return
self.channels = self.doubanfm.channels
self.__create_menu_item__()
self.check_to_enable_dbus()
self.__register_events()
self.doubanfm_cover = DoubanFMCover()
providers.register('covers', self.doubanfm_cover)
self.doubanfm_mode = DoubanFMMode(self.exaile, self)
def show_captcha_dialog(self, captcha_id):
if self.captcha_dialog is None:
self.captcha_dialog = CaptchaDialog(self)
captcha_url = "http://www.douban.com/misc/captcha?id=%s&size=s" % captcha_id
self.captcha_dialog.set_captcha(captcha_id, captcha_url)
self.captcha_dialog.show()
@staticmethod
def __translate_channels():
d = {}
for k in DoubanFMChannels.keys():
d[_(k)] = DoubanFMChannels[k]
return d
def __register_events(self):
event.add_callback(self.check_to_load_more, 'playback_track_start')
event.add_callback(self.close_playlist, 'quit_application')
event.add_callback(self.play_feedback, 'playback_track_end')
if self.dbus_controller:
self.dbus_controller.register_events()
def __unregister_events(self):
event.remove_callback(self.check_to_load_more, 'playback_track_start')
event.remove_callback(self.close_playlist, 'quit_application')
event.remove_callback(self.play_feedback, 'playback_track_end')
if self.dbus_controller:
self.dbus_controller.unregister_events()
@common.threaded
def mark_as_skip(self, track):
self.skipped = True
playlist = self.get_current_playlist()
rest_sids = self.get_rest_sids(playlist)
## play next song
#self.exaile.gui.main.QUEUE.next()
sid = track.get_tag_raw('sid')[0]
aid = track.get_tag_raw('aid')[0]
songs = self.doubanfm.skip_song(sid, aid, history=self.get_history_sids(playlist))
player.QUEUE.next()
self.load_more_tracks(songs)
def load_more_tracks(self, songs):
#if self.get_tracks_remain() > 5:
# start = self.get_current_pos()+4
# end = len(playlist)-1
# playlist.remove_tracks(start, end)
if self.get_tracks_remain() < 5:
tracks = map(self.create_track_from_douban_song, songs)
playlist = self.get_current_playlist()
playlist.extend(tracks)
@common.threaded
def mark_as_like(self, track):
sid = track.get_tag_raw('sid')[0]
aid = track.get_tag_raw('aid')[0]
pt = player.PLAYER.get_time()
self.doubanfm.fav_song(sid, aid)
track.set_tag_raw('fav', '1')
@common.threaded
def mark_as_dislike(self, track):
sid = track.get_tag_raw('sid')[0]
aid = track.get_tag_raw('aid')[0]
track.set_tag_raw('fav', '0')
self.doubanfm.unfav_song(sid, aid)
@common.threaded
def mark_as_recycle(self, track):
self.skipped = True
playlist = self.get_current_playlist()
rest_sids = self.get_rest_sids(playlist)
## remove the track
self.remove_current_track()
## play next song
#self.exaile.gui.main.queue.next()
player.QUEUE.next()
sid = track.get_tag_raw('sid')[0]
aid = track.get_tag_raw('aid')[0]
songs = self.doubanfm.del_song(sid, aid, rest=rest_sids)
self.load_more_tracks(songs)
def get_rest_sids(self, playlist):
playlist = self.get_current_playlist()
#current_tracks = playlist.get_tracks()
current_tracks = self.get_tracks(playlist)
rest_tracks = current_tracks[playlist.get_current_position()+1:]
rest_sids = self.tracks_to_sids(rest_tracks)
return rest_sids
def share(self, target, track):
if target not in SHARE_TEMPLATE:
return None
templ = SHARE_TEMPLATE[target]
data = {}
data['title'] = track.get_tag_raw('title')[0]
data['artist'] = track.get_tag_raw('artist')[0]
data['sid'] = track.get_tag_raw('sid')[0]
data['ssid'] = track.get_tag_raw('ssid')[0]
data['picture'] = track.get_tag_raw('cover_url')[0]
track = DoubanTrack(**data)
if target == 'renren':
title = track.title + ", " + track.artist
p = templ % tuple(map(urllib.quote_plus, [title.encode('utf8'), track.get_uri()]))
return p
if target == 'kaixin001':
title = track.title + ", " + track.artist
p = templ % tuple(map(urllib.quote_plus, [track.get_uri(), title.encode('utf8')]))
return p
if target == 'sina':
title = track.title + ", " + track.artist
p = templ % tuple(map(urllib.quote_plus, [track.get_uri(), title.encode('utf8'), track.picture]))
return p
if target == 'twitter':
title = track.title + ", " + track.artist
p = templ % tuple(map(urllib.quote_plus, [title.encode('utf8'), track.get_uri()]))
return p
if target == 'fanfou':
title = track.title + ", " + track.artist
p = templ % tuple(map(urllib.quote_plus, [track.get_uri(), title.encode('utf8')]))
return p
if target == 'douban':
p = templ % tuple(map(urllib.quote_plus, [track.title.encode('utf8'),track.get_uri(), track.picture, "Exaile DoubanFM Plugin",track.sid]))
return p
def get_tracks_remain(self):
pl = self.get_current_playlist()
total = len(pl)
cursor = pl.get_current_position()
return total-cursor-1
def get_current_track(self):
pl = self.get_current_playlist()
if isinstance(pl, DoubanFMPlaylist):
#return pl.get_tracks()[pl.get_current_pos()]
return pl.get_current()
else:
return None
def remove_current_track(self):
pl = self.get_current_playlist()
del pl[pl.get_current_position()]
def tracks_to_sids(self, tracks):
return map(lambda t: t.get_tag_raw('sid')[0], tracks)
@common.threaded
def play_feedback(self, type, player, current_track):
if self.skipped:
self.skipped = False
return
track = current_track
if track.get_tag_raw('sid'):
sid = track.get_tag_raw('sid')[0]
aid = track.get_tag_raw('aid')[0]
if sid is not None and aid is not None:
self.doubanfm.played_song(sid, aid)
def get_current_playlist(self):
page_num = self.exaile.gui.main.playlist_notebook.get_current_page();
page = self.exaile.gui.main.playlist_notebook.get_nth_page(page_num);
return page.playlist
# return self.exaile.gui.main.get_selected_playlist().playlist
def close_playlist(self, type, exaile, data=None):
removed = 0
for i,page in enumerate(exaile.gui.main.playlist_notebook):
if isinstance(page.playlist, DoubanFMPlaylist):
page.tab.close()
removed += 1
def check_to_load_more(self, type, player, track):
playlist = self.get_current_playlist()
if isinstance(playlist, DoubanFMPlaylist):
## check if last one
## playlist.index(track), len(playlist.get_tracks())
if self.get_tracks_remain() <= 1:
self.load_more(playlist)
def get_tracks(self, playlist):
current_tracks = []
for i, x in enumerate(playlist):
current_tracks.append(x)
return current_tracks
def get_history_sids(self, playlist):
current_tracks = self.get_tracks(playlist)
sids = self.tracks_to_sids(current_tracks)
return sids
def load_more(self, playlist):
sids = self.get_history_sids(playlist)
current_sid = sids[playlist.get_current_position()]
retry = 0
while retry < 1:
try:
songs = self.doubanfm.played_list(current_sid, sids)
except:
retry += 1
continue
if len(songs) > 0:
tracks = map(self.create_track_from_douban_song, songs)
#playlist.add_tracks(tracks)
playlist.extend(tracks)
break
else:
retry += 1
def __create_menu_item__(self):
providers.unregister('menubar-file-menu',self.premenu)
self.menu=gtk.Menu()
for channel_name in self.channels.keys():
menuItem = gtk.MenuItem(_(channel_name))
menuItem.connect('activate', self.active_douban_radio, self.channels[channel_name])
self.menu.append(menuItem)
menuItem.show()
self.premenu=menu.simple_menu_item('Open Douban.fm',[],_('_Open Douban.fm'),
None, None,[],self.menu)
providers.register('menubar-file-menu',self.premenu)
self.modemenu=menu.simple_menu_item('DoubanFM Mode',[],_('_DoubanFM Mode'),
gtk.STOCK_FULLSCREEN,self.show_mode,
accelerator='<Control>D')
self.accelerator_mode = Accelerator('<Control>D',self.show_mode)
providers.register('menubar-view-menu',self.modemenu)
providers.register('mainwindow-accelerators', self.accelerator_mode)
def __create_pre_init_menu_item(self):
self.premenu=menu.simple_menu_item('Connect to Douban.fm',[],_('_Connect to Douban.fm'),
gtk.STOCK_ADD, lambda e,r,t,y:self.do_init(),
accelerator='<Control>C')
self.accelerator_pre = Accelerator('<Control>C',lambda e,r,t,y:self.do_init())
providers.register('menubar-file-menu',self.premenu)
providers.register('mainwindow-accelerators', self.accelerator_pre)
def create_track_from_douban_song(self, song):
track = Track(song.url)
track.set_tag_raw('sid', song.sid)
track.set_tag_raw('aid', song.aid)
track.set_tag_raw('ssid', song.ssid or '')
track.set_tag_raw('uri', song.url)
track.set_tag_raw('cover_url', song.picture)
track.set_tag_raw('title', song.title)
track.set_tag_raw('artist', song.artist)
track.set_tag_raw('album', song.albumtitle)
track.set_tag_raw('fav', str(song.like) or '0')
return track
def show_mode(self, *e):
self.doubanfm_mode.show()
def create_playlist(self, name, channel, initial_tracks=[]):
plist = DoubanFMPlaylist(name, channel, initial_tracks)
#plist.set_ordered_tracks(initial_tracks)
plist.set_repeat_mode("disabled")
# plist.set_random(False)
plist.set_dynamic_mode("disabled")
return plist
def get_current_channel(self):
return self.get_current_playlist().channel
def active_douban_radio(self, type, channel_id, auto=False):
self.doubanfm.channel = channel_id
try:
songs = self.doubanfm.new_playlist()
except:
dialog = gtk.MessageDialog(self.exaile.gui.main.window, 0,
gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
_('Failed to retrieve playlist, try again.'))
dialog.run()
dialog.destroy()
return
tracks = map(self.create_track_from_douban_song, songs)
channel_name = self.channel_id_to_name(channel_id)
plist = self.create_playlist(
'DoubanFM %s' % channel_name, channel_id, tracks)
self.exaile.gui.main.playlist_notebook.create_tab_from_playlist(plist)
# self.exaile.gui.main.add_playlist(plist)
if auto:
self._stop()
self._play()
def _stop(self):
player.PLAYER.stop()
def _play(self):
## ref xlgui.main.on_playpause_button_clicked
guimain = self.exaile.gui.main
pl = guimain.get_selected_playlist()
#guimain.queue.set_current_playlist(pl.playlist)
player.QUEUE.set_current_playlist(pl.playlist)
#if pl:
# track = pl.get_selected_track()
# if track:
# pl.playlist.set_current_pos((pl.playlist.index(track)))
# set to play the first song in playlist
pl.playlist.set_current_position(-1)
player.QUEUE.play()
def destroy(self, exaile):
try:
providers.unregister('covers', self.doubanfm_cover)
if self.menuItem :
self.get_menu('menubar-file-menu').remove(self.menuItem)
if self.modeMenuItem:
self.get_menu('menubar-view-menu').remove(self.modeMenuItem)
exaile.gui.main.remove_accel_group(self.accels)
self.__unregister_events()
self.doubanfm_mode.destroy()
if self.dbus_controller:
self.dbus_controller.on_exit()
self.dbus_controller.unregister_events()
self.dbus_controller.release_dbus()
except:
pass
def get_menu(self, menu_id):
return providers.get(menu_id)
def check_to_enable_dbus(self):
if settings.get_option('plugin/douban_radio/dbus_indicator'):
self.dbus_controller = DoubanFMDBusController(self)
self.dbus_controller.acquire_dbus()
self.dbus_controller.on_init()
else:
self.dbus_controller = None
def channel_id_to_name(self, channel_id):
for k,v in self.channels.items():
if v == channel_id:
return k
return None
class DoubanFMPlaylist(playlist.Playlist):
def __init__(self, name, channel, initTracks):
playlist.Playlist.__init__(self, name, initTracks)
self.channel = channel