/usr/lib/rhythmbox/plugins/context/ArtistTab.py is in rhythmbox-plugins 3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
#
# Copyright (C) 2009 John Iacona
#
# 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.
#
# The Rhythmbox authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and Rhythmbox. This permission is above and beyond the permissions granted
# by the GPL license by which Rhythmbox 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.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
import re, os
import cgi
import urllib.request, urllib.parse
import xml.dom.minidom as dom
import json
from mako.template import Template
import rb
import LastFM
from gi.repository import WebKit
from gi.repository import GObject, Gtk
from gi.repository import RB
import gettext
gettext.install('rhythmbox', RB.locale_dir())
class ArtistTab (GObject.GObject):
__gsignals__ = {
'switch-tab' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
(GObject.TYPE_STRING,))
}
def __init__ (self, shell, buttons, ds, view):
GObject.GObject.__init__ (self)
self.shell = shell
self.sp = shell.props.shell_player
self.db = shell.props.db
self.buttons = buttons
self.button = Gtk.ToggleButton (label=_("Artist"))
self.datasource = ds
self.view = view
self.artist = None
self.active = False
self.button.show()
self.button.set_relief (Gtk.ReliefStyle.NONE)
self.button.set_focus_on_click(False)
self.button.connect ('clicked',
lambda button : self.emit('switch-tab', 'artist'))
buttons.pack_start (self.button, True, True, 0)
def activate (self):
print("activating Artist Tab")
self.button.set_active(True)
self.active = True
self.reload ()
def deactivate (self):
print("deactivating Artist Tab")
self.button.set_active(False)
self.active = False
def reload (self):
entry = self.sp.get_playing_entry ()
if entry is None:
print("Nothing playing")
return None
artist = entry.get_string (RB.RhythmDBPropType.ARTIST)
if self.active and self.artist != artist:
self.datasource.fetch_artist_data (artist)
self.view.loading (artist)
else:
self.view.load_view()
self.artist = artist
class ArtistView (GObject.GObject):
def __init__ (self, shell, plugin, webview, ds):
GObject.GObject.__init__ (self)
self.webview = webview
self.ds = ds
self.shell = shell
self.plugin = plugin
self.file = ""
plugindir = plugin.plugin_info.get_data_dir()
self.basepath = "file://" + urllib.request.pathname2url (plugindir)
self.load_tmpl ()
self.connect_signals ()
def load_view (self):
self.webview.load_string (self.file, 'text/html', 'utf-8', self.basepath)
def loading (self, current_artist):
self.loading_file = self.loading_template.render (
artist = current_artist,
info = _("Loading biography for %s") % current_artist,
song = "",
basepath = self.basepath)
self.webview.load_string (self.loading_file, 'text/html', 'utf-8', self.basepath)
def load_tmpl (self):
self.path = rb.find_plugin_file(self.plugin, 'tmpl/artist-tmpl.html')
self.loading_path = rb.find_plugin_file (self.plugin, 'tmpl/loading.html')
self.template = Template (filename = self.path)
self.loading_template = Template (filename = self.loading_path)
self.styles = self.basepath + '/tmpl/main.css'
def connect_signals (self):
self.air_id = self.ds.connect ('artist-info-ready', self.artist_info_ready)
def artist_info_ready (self, ds):
# Can only be called after the artist-info-ready signal has fired.
# If called any other time, the behavior is undefined
try:
info = ds.get_artist_info ()
small, med, big = info['images'] or (None, None, None)
summary, full_bio = info['bio'] or (None, None)
self.file = self.template.render (artist = ds.get_current_artist (),
error = ds.get_error (),
image = med,
fullbio = full_bio,
shortbio = summary,
datasource = LastFM.datasource_link (self.basepath),
stylesheet = self.styles )
self.load_view ()
except Exception as e:
print("Problem in info ready: %s" % e)
class ArtistDataSource (GObject.GObject):
__gsignals__ = {
'artist-info-ready' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
'artist-similar-ready' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
'artist-top-tracks-ready' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
'artist-top-albums-ready' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
}
def __init__ (self, info_cache, ranking_cache):
GObject.GObject.__init__ (self)
self.current_artist = None
self.error = None
self.artist = {
'info' : {
'data' : None,
'signal' : 'artist-info-ready',
'function' : 'getinfo',
'cache' : info_cache,
'parsed' : False,
},
# nothing uses this
#'similar' : {
# 'data' : None,
# 'signal' : 'artist-similar-ready',
# 'function' : 'getsimilar',
# 'cache' : info_cache,
# 'parsed' : False,
#},
# or this
#'top_albums' : {
# 'data' : None,
# 'signal' : 'artist-top-albums-ready',
# 'function' : 'gettopalbums',
# 'cache' : ranking_cache,
# 'parsed' : False,
#},
}
def fetch_artist_data (self, artist):
"""
Initiate the fetching of all artist data. Fetches artist info, similar
artists, artist top albums and top tracks. Downloads XML files from last.fm
and saves as parsed DOM documents in self.artist dictionary. Must be called
before any of the get_* methods.
"""
self.current_artist = artist
if LastFM.user_has_account() is False:
self.error = LastFM.NO_ACCOUNT_ERROR
self.emit ('artist-info-ready')
return
self.error = None
artist = urllib.parse.quote_plus(artist)
for key, value in self.artist.items():
cachekey = "lastfm:artist:%sjson:%s" % (value['function'], artist)
url = '%sartist.%s&artist=%s&api_key=%s&format=json' % (LastFM.URL_PREFIX,
value['function'], artist, LastFM.API_KEY)
print("fetching %s" % url)
value['cache'].fetch(cachekey, url, self.fetch_artist_data_cb, value)
def fetch_artist_data_cb (self, data, category):
if data is None:
print("no data fetched for artist %s" % category['function'])
return
try:
category['data'] = json.loads(data.decode('utf-8'))
category['parsed'] = False
self.emit (category['signal'])
except Exception as e:
print("Error parsing artist %s: %s" % (category['function'], e))
return False
def get_current_artist (self):
return self.current_artist
def get_error (self):
return self.error
def get_top_albums (self):
if not self.artist['top_albums']['parsed']:
albums = []
d = self.artist['top_albums']['data']
for album in d['topalbums'].get('album', []):
images = [img['#text'] for img in album.get('image', ())]
albums.append((album.get('name'), images[:3]))
self.artist['top_albums']['data'] = albums
self.artist['top_albums']['parsed'] = True
return self.artist['top_albums']['data']
def get_similar_artists (self):
"""
Returns a list of similar artists
"""
data = self.artist['similar']['data']
if data is None:
return None
if not self.artist['similar']['parsed']:
lst = []
for node in data['similarartists'].get('artist', []):
image = [img['#text'] for img in node.get('image', [])]
lst.append ((node.get('name'), node.get('match'), image[:1]))
data = lst
self.artist['similar']['parsed'] = True
self.artist['similar']['data'] = data
return data
def get_artist_images (self):
"""
Returns tuple of image url's for small, medium, and large images.
"""
data = self.artist['info']['data']
if data is None:
return None
images = [img['#text'] for img in data['artist'].get('image', ())]
return images[:3]
def get_artist_bio (self):
"""
Returns tuple of summary and full bio
"""
data = self.artist['info']['data']
if data is None:
return None
if not self.artist['info']['parsed']:
content = data['artist']['bio']['content']
summary = data['artist']['bio']['summary']
return summary, content
return self.artist['info']['data']['bio']
def get_artist_info (self):
"""
Returns the dictionary { 'images', 'bio' }
"""
if not self.artist['info']['parsed']:
images = self.get_artist_images()
bio = self.get_artist_bio()
self.artist['info']['data'] = { 'images' : images,
'bio' : bio }
self.artist['info']['parsed'] = True
return self.artist['info']['data']
|