forked from leplatrem/exaile-webradio-title
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
127 lines (90 loc) · 3.23 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
import time
import random
from xl import player, event
from scrap import ScrapperThread, FIPScrapper, NovaScrapper, GrenouilleScrapper
import logging
logger = logging.getLogger(__name__)
_PLUGIN = None
PLAYBACK_START_CALLBACKS = (
'playback_player_start',
'playback_player_resume',
)
PLAYBACK_STOP_CALLBACKS = (
'playback_player_end',
'playback_player_pause',
)
def enable(exaile):
if exaile.loading:
event.add_callback(_enable, 'exaile_loaded')
else:
_enable(None, exaile, None)
def _enable(eventname, exaile, nothing):
global _PLUGIN
_PLUGIN = WebRadioTitlePlugin(exaile)
for signal in PLAYBACK_START_CALLBACKS:
event.add_callback(_PLUGIN.on_playback_start, signal)
for signal in PLAYBACK_STOP_CALLBACKS:
event.add_callback(_PLUGIN.on_playback_stop, signal)
def disable(exaile):
global _PLUGIN
for signal in PLAYBACK_START_CALLBACKS:
event.remove_callback(_PLUGIN.on_playback_start, signal)
for signal in PLAYBACK_STOP_CALLBACKS:
event.remove_callback(_PLUGIN.on_playback_stop, signal)
_PLUGIN.stop()
class WebRadioTitlePlugin(object):
def __init__(self, exaile):
logger.debug("")
self.scrapper = None
def __del__(self):
logger.debug("")
self.stop()
def on_playback_start(self, type, object, data):
logger.debug("Type: " + type)
# Stop
self.stop()
# Get track
track = player.PLAYER.current
if not track:
logger.debug("No track")
return
# Check if track is a webradio
if not track.get_type() == "http":
logger.debug("Track type is not http")
return
# Get URL
url = track.get_loc_for_io()
# Look for a web scrapper that knows this URL
for cls in [FIPScrapper, NovaScrapper, GrenouilleScrapper]:
if cls.match(url):
self.start(cls, track)
return
logger.debug("Current track does not match any webradio scrapper")
def on_playback_stop(self, type, object, data):
logger.debug("Type: " + type)
self.stop()
def start(self, scrappercls, track):
logger.debug("Start fetching titles")
self.scrapper = ScrapperThread(scrappercls, self, track)
self.scrapper.start()
def stop(self):
logger.debug("Stop fetching titles")
if self.scrapper:
self.scrapper.stop()
self.scrapper = None
def update_track(self, cause, track, infos):
logger.debug("cause: " + cause + ", track: " + str(track) + ", infos: " + str(infos))
# Ensure a track is defined
if not track:
logger.debug("No track")
return
# Set tags in track
for tag in ['artist', 'title', 'album', 'date']:
value = infos.get(tag)
if value is not None:
track.set_tag_raw(tag, value)
track.set_tag_raw('__length', random.randint(180, 240)) # Fake length
# Trigger a notification
if cause == 'updated':
logger.debug("Simulate track change")
event.log_event('playback_track_start', player.PLAYER, track)