~guillaume86/exaile/context-dev

« back to all changes in this revision

Viewing changes to plugins/lastfmradio/__init__.py

  • Committer: guillaume86
  • Date: 2009-08-03 11:51:39 UTC
  • mfrom: (2141.1.148 trunk)
  • Revision ID: guillaume86-20090803115139-ipxtvutozyrsp2o3
Update the home/current song button image/tooltip according to the situation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Adam Olsen 
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 1, or (at your option)
6
 
# any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 
 
17
 
"""
18
 
    LastFM Proxy plugin.
19
 
 
20
 
    This plugin is a complete hack. 
21
 
 
22
 
    Props to Vidar Madsen for the lastfmproxy application, which this plugin
23
 
    uses directly
24
 
"""
25
 
 
26
 
from xl.nls import gettext as _
27
 
from xl import event, common, playlist, track, settings
28
 
from xl.radio import *
29
 
import os, os.path, logging
30
 
import lastfmmain
31
 
import config
32
 
import httpclient
33
 
logger = logging.getLogger(__name__)
34
 
 
35
 
def album_callback(*e):
36
 
    pass
37
 
 
38
 
@common.threaded
39
 
def run_proxy(config):
40
 
    count = 0
41
 
    while True:
42
 
        try:
43
 
            STATION.listenport = config.listenport
44
 
            PROXY.run(config.bind_address, config.listenport)
45
 
            return
46
 
        except Exception, e:
47
 
            if count >= 5:
48
 
                raise(e)
49
 
 
50
 
            if e.args[0] == 98:
51
 
                count += 1
52
 
                logger.warning("LastFM Proxy: Port %d in use, trying %d" %
53
 
                    (config.listenport, config.listenport + 1))
54
 
                config.listenport += 1
55
 
            else:
56
 
                raise(e)
57
 
 
58
 
def enable(exaile):
59
 
    if exaile.loading:
60
 
        event.add_callback(_enable, 'exaile_loaded')
61
 
    else:
62
 
        _enable(None, exaile, None)
63
 
 
64
 
STATION = None
65
 
PROXY = None
66
 
HTTP_CLIENT = None
67
 
BUTTONS = None
68
 
SHOW_COVER = None
69
 
def _enable(devicename, exaile, nothing):
70
 
    global STATION, PROXY, HTTP_CLIENT, BUTTONS, SHOW_COVER
71
 
 
72
 
    port = settings.get_option('plugin/lastfm/listenport', 1881)
73
 
    config.listenport = port
74
 
 
75
 
    config.username = settings.get_option('plugin/lastfm/user', '')
76
 
    config.password = settings.get_option('plugin/lastfm/password', '')
77
 
 
78
 
    if not STATION:
79
 
        STATION = LastFMRadioStation(config)
80
 
        exaile.radio.add_station(STATION)
81
 
        HTTP_CLIENT = httpclient.httpclient('__localhost', config.listenport)
82
 
 
83
 
    PROXY = lastfmmain.proxy(config.username, config.password)
84
 
    PROXY.np_image_func = album_callback
85
 
    basedir = os.path.dirname(os.path.realpath(__file__))
86
 
    PROXY.basedir = basedir
87
 
    run_proxy(config)
88
 
 
89
 
    event.add_callback(STATION.on_playback_start, 'playback_start',
90
 
        exaile.player)
91
 
 
92
 
    return True
93
 
 
94
 
def disable(exaile):
95
 
    global STATION, PROXY
96
 
 
97
 
    exaile.radio.remove_station(STATION)
98
 
    if PROXY:
99
 
        PROXY.np_image_func = None
100
 
        PROXY.quit = True
101
 
 
102
 
    PROXY = True
103
 
    if STATION:
104
 
        event.remove_callback(STATION.on_playback_start, 'playback_track_start',
105
 
            exaile.player)
106
 
    STATION = None
107
 
 
108
 
class LastFMRadioStation(RadioStation):
109
 
    """
110
 
        LastFM Radio
111
 
    """
112
 
    name = 'lastfm'
113
 
    def __init__(self, config):
114
 
        """
115
 
            Initializes the lastfm radio station
116
 
        """
117
 
        self.config = config
118
 
 
119
 
    def on_playback_start(self, type, player, track):
120
 
        station = track['lastfmstation']
121
 
        if station:
122
 
            self.command('/changestation/%s' % station)
123
 
 
124
 
    @common.threaded
125
 
    def command(self, command):
126
 
        logger.info("LastFM: running command %s" % command)
127
 
        HTTP_CLIENT.req(command)
128
 
 
129
 
    def _load_cache(self):
130
 
        pass
131
 
 
132
 
    def _save_cache(self):
133
 
        pass
134
 
 
135
 
    def get_lists(self, no_cache=False):
136
 
        """
137
 
            Returns the list of items for lastfm
138
 
        """
139
 
        stations = (
140
 
            (_('Personal'), 'lastfm://user/%s/personal' % self.config.username),
141
 
            (_('Recommended'), 'lastfm://user/%s/recommended/100' %
142
 
                self.config.username),
143
 
            (_('Neighbourhood'), 'lastfm://user/%s/neighbours' %
144
 
                self.config.username),
145
 
            (_('Loved Tracks'), 'lastfm://user/%s/loved' % self.config.username),
146
 
        )
147
 
 
148
 
        items = []
149
 
        for name, url in stations:
150
 
            item = RadioItem(name, station=self)
151
 
            item.get_playlist = lambda url=url, name=name: \
152
 
                self._get_playlist(url, name)
153
 
            items.append(item)
154
 
 
155
 
        return items
156
 
 
157
 
    def _get_playlist(self, url, name):
158
 
        self.listenport = config.listenport
159
 
        tr = track.Track()
160
 
        tr.set_loc('http://localhost:%d/lastfm.mp3' % self.listenport)
161
 
        tr['album'] = "LastFM: %s" % name
162
 
        tr['title'] = "LastFM: %s" % name
163
 
        tr['artist'] = _("LastFM Radio")
164
 
        tr['lastfmstation'] = url
165
 
        
166
 
        pl = playlist.Playlist(name=_("LastFM: %s") % name, kind='radio')
167
 
        pl.add_tracks([tr])
168
 
 
169
 
        return pl
170
 
 
171
 
    def get_menu(self, parent):
172
 
        return menu