From a9a8c128af7a7d366e448a918c07b7fbc8176f7b Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 25 May 2010 21:12:22 +0200 Subject: url_titles: Support spotify-links (non-http) and albums. --- modules/url_titles.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/url_titles.py b/modules/url_titles.py index 6fa48f2..11c0472 100644 --- a/modules/url_titles.py +++ b/modules/url_titles.py @@ -9,6 +9,7 @@ from PIL import ImageFile class Module: re_http = re.compile(r'(http://[^\ ]+)') + re_spotify = re.compile(r'spotify:(?:track|artist|album):[\w]{22}') re_title = re.compile(r']*?>(.*?)', re.S | re.I) metadata = spotimeta.Metadata(cache = {}) @@ -21,9 +22,12 @@ class Module: except: return 'Failed to fetch metadata from spotify.' if data['type'] == 'artist': - return 'Spotify: %s' % data['result']['name'] - else: - return 'Spotify: %s - %s' % (data['result']['artist']['name'], data['result']['name']) + return 'Spotify artist: %s' % data['result']['name'] + elif data['type'] == 'track': + return 'Spotify track: %s - %s' % (data['result']['artist']['name'], data['result']['artist']['name']) + elif data['type'] == 'album': + return u'Spotify album: %s (%d) by %s' % (data['result']['name'], data['result']['released'], + ', '.join([x['name'] for x in data['result']['artists']])) def get_titles(self, s): def parse_url(url): @@ -61,11 +65,9 @@ class Module: return s m = self.re_http.findall(s) - if not m: - return titles = [] for url in m: - if any([x in url for x in ('open.spotify.com', 'spotify:track:', 'spotify:artist:', 'spotify:album:')]): + if 'open.spotify.com' in url: titles.append(self.spotify(url).encode('utf8')) continue t = time.time() @@ -122,7 +124,12 @@ class Module: finally: del im u.close() - if len(titles) == 1: + spotify_links = self.re_spotify.findall(s) + for sp in spotify_links: + titles.append(self.spotify(sp).encode('utf8')) + if len(titles) == 0: + return + elif len(titles) == 1: s = format_text(titles[0]) else: s = '' -- cgit v1.2.3