-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
149 lines (121 loc) · 4.08 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
import os
import logging
import threading
import time
import random
import xl
from xl.nls import gettext as _
from xl import event, common
from scrap import FIPScrapper
FREQUENCY = 10 # seconds
_PLUGIN = None
TRACK_CHANGE_CALLBACKS = (
'playback_buffering',
'playback_player_resume',
'playback_current_changed',
'playback_track_start',
'playback_player_start',
'playback_track_end',
'player_loaded',
)
logger = logging.getLogger(__name__)
def enable(exaile):
if (exaile.loading):
event.add_callback(_enable, 'exaile_loaded')
else:
_enable(None, exaile, None)
def disable(exaile):
global _PLUGIN
_PLUGIN.stop()
def _enable(eventname, exaile, nothing):
global _PLUGIN
if not _PLUGIN:
_PLUGIN = WebRadioTitlePlugin(exaile)
_PLUGIN.exaile = exaile
for signal in TRACK_CHANGE_CALLBACKS:
xl.event.add_callback(_PLUGIN.change, signal)
class WebRadioTitlePlugin(object):
def __init__(self, exaile):
self.exaile = exaile
self.scrapper = None
self._stop = None
self._previous = {}
self._tmptag = False
def __del__(self):
self.stop()
def change(self, *args, **kwargs):
if self.exaile is None:
return
if not hasattr(self.exaile, 'player'):
logger.debug(_("Player not loaded, ignoring change call"))
return
track = self.exaile.player.current
if not track:
logger.debug(_("Player stopped, stop fetching titles"))
self.stop()
return
if self._tmptag: # Ignore event while tags set in updatetrack()
return
url = track.get_loc_for_io()
# Look for a web scrapper that knows this url
matchcls = None
for cls in [FIPScrapper]: # more coming !
if cls.match(url):
matchcls = cls
if matchcls:
self.run(matchcls)
else:
logger.debug(_("Current track does not match any webradio scrapper"))
self.stop()
def haschanged(self, tags):
return self._previous is None or \
any(tags.get(k) != self._previous.get(k) for k in tags)
@common.threaded
def run(self, scrappercls):
if self.scrapper and isinstance(self.scrapper, scrappercls):
# Already running with this scrapper
return
logger.info(_("Scrapping started"))
self.start()
self.scrapper = scrappercls()
while not self.stopped:
try:
d = self.scrapper.current()
# Update track if some field changed
if self.haschanged(d):
logger.debug(_("New track scrapped %s") % d)
self.updatetrack(d)
self._previous = d
time.sleep(FREQUENCY)
except Exception, e:
logger.exception(e)
self.scrapper = None
self._previous = None
logger.info(_("Scrapping stopped"))
def start(self):
self._stop = threading.Event()
def stop(self):
if self._stop:
self._stop.set()
@property
def stopped(self):
return self._stop.isSet()
def updatetrack(self, data):
track = self.exaile.player.current
if not track:
return
for tag in ['artist', 'title', 'album', 'author']:
value = data.get(tag)
if value:
track.set_tag_raw(tag, value)
if not data:
return
logger.debug(_("Simulate track change"))
track.set_tag_raw('__length', random.randint(180, 240)) # fake length
loc = track.get_tag_raw('__loc')
self._tmptag = True
fakepath = os.path.abspath(os.urandom(random.randint(16, 32)))
track.set_tag_raw('__loc', fakepath) # fake is_local() for audioscrobbler plugin
event.log_event('playback_track_start', self.exaile.player, track, async=False)
track.set_tag_raw('__loc', loc)
self._tmptag = False