class TestPlaylist(unittest.TestCase): """ A test class for the Geo module. """ def setUp(self): self.api = Api(api_key, no_cache = True) self.playlist = self.api.get_playlist('lastfm://playlist/album/2287667') def tearDown(self): pass def testPlaylistUrl(self): self.assertEqual(self.playlist.url, 'lastfm://playlist/album/2287667')
class TestPlaylist(unittest.TestCase): """ A test class for the Geo module. """ def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache=True) self.playlist = self.api.get_playlist("lastfm://playlist/album/2287667") def tearDown(self): pass def testPlaylistUrl(self): self.assertEqual(self.playlist.url, "lastfm://playlist/album/2287667")
def __init__(self, config, no_cache=False): Worker.__init__(self) self._key = config.get('LastFM', 'key', None) self._lfm = Api(self._key, no_cache=no_cache) self._db = DB(config.get('LastFM', 'path', join(config.configdir(), 'echonest.db'))) self._db.execute( u"CREATE TABLE IF NOT EXISTS artist_lookup "\ "(artist text, timestamp integer, "\ "PRIMARY KEY(artist))") self._db.execute( u"CREATE TABLE IF NOT EXISTS track_lookup "\ "(artist text, title text, timestamp integer, "\ "PRIMARY KEY(artist, title))") self._db.commit()
def testTrackSearch(self): tracks = [('Plug In Baby', 'Muse'), ('Tell Me Baby', 'Red Hot Chili Peppers'), ('Combat Baby', 'Metric'), ('Hey Baby', 'No Doubt'), ('Cry Baby Cry', 'The Beatles'), ('Find My Baby', 'Moby'), ('Nobody Puts Baby in the Corner', 'Fall Out Boy'), ('Baby Fratelli', 'The Fratellis'), ("I Can't Quit You Baby", 'Led Zeppelin'), ('Baby Britain', 'Elliott Smith')] self.assertEqual([(t.name, t.artist.name) for t in list(api.search_track('baby')[:10])], tracks) apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache = True) data = { 'name': 'Lithium', 'mbid': '', 'url': 'http://www.last.fm/music/Evanescence/_/Lithium', 'duration': 223000, 'streamable': True, 'full_track': False, 'artist': api.get_artist('Evanescence'), 'album': api.get_album('The Open Door', 'Evanescence'), 'position': 4, 'image': {} } for k,v in data.items(): def testFunc(self):
def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache=True) self.playlist = self.api.get_playlist( 'lastfm://playlist/album/2287667')
def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache = True) self.tag = self.api.get_tag("rock").most_similar
(u'\u3075\u3057\u304e\u30c7\u30ab\u30eb\u30c8', u'\u76f8\u5bfe\u6027\u7406\u8ad6')] self.assertEqual( [(track.name, track.artist.name) for track in self.country.top_tracks[:10]], tracks) def testCountryTopTrack(self): top_track = self.country.top_track self.assertEqual((top_track.name, top_track.artist.name), ('Dream Fighter', 'Perfume')) def testCountryEvents(self): event_ids = [961510, 925636, 959392, 875466, 951038, 950520, 957543, 930614, 871240, 857063] self.assertEqual([e.id for e in self.country.events[:10]], event_ids) apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache = True) data = { 'city': "Tokyo", 'country': api.get_country("Japan"), 'street': '1-3-61, Koraku, Bunkyo-ku', 'postal_code': '112-8562' } for k,v in data.iteritems(): def testFunc(self): self.assertEqual(getattr(self.location, k), v) setattr(TestGeo, "testLocation%s" % k.replace('_', ' ').title().replace(' ', ''), testFunc) test_suite = unittest.TestLoader().loadTestsFromTestCase(TestGeo) if __name__ == '__main__':
class TestGroup(unittest.TestCase): """ A test class for the Group module. """ def setUp(self): self.api = Api(api_key, no_cache = True) self.group = self.api.get_group('Rock') def tearDown(self): pass def testGroupName(self): self.assertEqual(self.group.name, 'Rock') def testGroupMembers(self): members = ['Alaa10', 'ilovemusix', 'Bartuu', 'ABeyers', 'kaique1002', 'Thug4Life2Da', 'Fishuturek', 'JapanesBoy', 'rubenius90', '230688'] self.assertEqual([m.name for m in self.group.members[:10]], members) def testGroupWeeklyChartList(self): wcl = [('Sun, 24 Sep 2006', 'Sun, 01 Oct 2006'), ('Sun, 01 Oct 2006', 'Sun, 08 Oct 2006'), ('Sun, 08 Oct 2006', 'Sun, 15 Oct 2006'), ('Sun, 15 Oct 2006', 'Sun, 22 Oct 2006'), ('Sun, 22 Oct 2006', 'Sun, 29 Oct 2006'), ('Sun, 29 Oct 2006', 'Sun, 05 Nov 2006'), ('Sun, 05 Nov 2006', 'Sun, 12 Nov 2006'), ('Sun, 12 Nov 2006', 'Sun, 19 Nov 2006'), ('Sun, 19 Nov 2006', 'Sun, 26 Nov 2006'), ('Sun, 26 Nov 2006', 'Sun, 03 Dec 2006')] self.assertEqual( [ ( wc.start.date().strftime('%a, %d %b %Y'), wc.end.date().strftime('%a, %d %b %Y') ) for wc in self.group.weekly_chart_list[:10] ], wcl ) def testGroupGetWeeklyArtistChart(self): artists = ['Red Hot Chili Peppers', 'Led Zeppelin', 'Metallica', 'Pink Floyd', 'Queen', "Guns N' Roses", 'AC/DC', 'The Beatles', 'Nirvana', 'Muse'] wc = self.group.weekly_chart_list[0] self.assertEqual( [artist.name for artist in self.group.get_weekly_artist_chart(wc.start, wc.end).artists[:10]], artists) def testGroupGetWeeklyAlbumChart(self): albums = [('By the Way', 'Red Hot Chili Peppers', 48), ('Toxicity', 'System of a Down', 48), ('Stadium Arcadium', 'Red Hot Chili Peppers', 43), ('OK Computer', 'Radiohead', 41), ('Absolution', 'Muse', 39), ('Californication', 'Red Hot Chili Peppers', 38), ('Back in Black', 'AC/DC', 37), ('Black Holes And Revelations', 'Muse', 37), ('Appetite for Destruction', "Guns N' Roses", 35), ('Meteora', 'Linkin Park', 35)] wc = self.group.weekly_chart_list[0] self.assertEqual( [(album.name, album.artist.name, album.stats.playcount) for album in self.group.get_weekly_album_chart(wc.start, wc.end).albums[:10]], albums) def testGroupGetWeeklyTrackChart(self): tracks = [('Stairway to Heaven', 'Led Zeppelin', 36), ('Back in Black', 'AC/DC', 34), ('Enter Sandman', 'Metallica', 33), ('Dani California', 'Red Hot Chili Peppers', 33), ('Bohemian Rhapsody', 'Queen', 31), ('Welcome to the Jungle', "Guns N' Roses", 30), ('Supermassive Black Hole', 'Muse', 30), ('Wonderwall', 'Oasis', 29), ('Run to the Hills', 'Iron Maiden', 28), ('November Rain', "Guns N' Roses", 28)] wc = self.group.weekly_chart_list[0] self.assertEqual( [(track.name, track.artist.name, track.stats.playcount) for track in self.group.get_weekly_track_chart(wc.start, wc.end).tracks[:10]], tracks) def testGroupGetWeeklyTagChart(self): tags = [('classic rock', 156), ('rock', 154), ('alternative', 130), ('british', 112), ('alternative rock', 110), ('hard rock', 78), ('metal', 63), ('psychedelic', 61), ('thrash metal', 54), ('heavy metal', 47)] wc = self.group.weekly_chart_list[0] self.assertEqual( [(tag.name, tag.stats.count) for tag in self.group.get_weekly_tag_chart(wc.start, wc.end).tags[:10]], tags)
print >> stderr, "Usage: %s <last.fm API key> <last.fm username> <year>" % basename(argv[0]) print >> stderr, "Example: %s 91009080e1635307d49a60be883e616 jp 2012" % basename(argv[0]) else: API_KEY = argv[1] USERNAME = argv[2] YEAR = int(argv[3]) # get the start of the year to compare later on the dates # First of january, at 00:00:00 year_start = datetime(YEAR, 1, 1, 0, 0, 0) # dictionary to hold the album list year_albums = {} # connect and retrieve the user api = Api(API_KEY, no_cache=True) user = api.get_user(USERNAME) # now retrieve the favourite albums from last year top_albums = user.get_top_albums("12month") # loop through the albums for album in top_albums: # print "%s. %s (mbid: %s)" % (album.artist.name, album.name, album.mbid) # we will only consider the albums having a proper mbid (to simplify things) if album.mbid: full_album = api.get_album(mbid=album.mbid) if full_album.release_date and year_start < full_album.release_date: year_albums[full_album.name] = full_album # print out the albums for the year
def setUp(self): self.api = Api(api_key, no_cache=True) self.group = self.api.get_group('Rock')
class TestGroup(unittest.TestCase): """ A test class for the Group module. """ def setUp(self): self.api = Api(api_key, no_cache=True) self.group = self.api.get_group('Rock') def tearDown(self): pass def testGroupName(self): self.assertEqual(self.group.name, 'Rock') def testGroupMembers(self): members = [ 'Alaa10', 'ilovemusix', 'Bartuu', 'ABeyers', 'kaique1002', 'Thug4Life2Da', 'Fishuturek', 'JapanesBoy', 'rubenius90', '230688' ] self.assertEqual([m.name for m in self.group.members[:10]], members) def testGroupWeeklyChartList(self): wcl = [('Sun, 24 Sep 2006', 'Sun, 01 Oct 2006'), ('Sun, 01 Oct 2006', 'Sun, 08 Oct 2006'), ('Sun, 08 Oct 2006', 'Sun, 15 Oct 2006'), ('Sun, 15 Oct 2006', 'Sun, 22 Oct 2006'), ('Sun, 22 Oct 2006', 'Sun, 29 Oct 2006'), ('Sun, 29 Oct 2006', 'Sun, 05 Nov 2006'), ('Sun, 05 Nov 2006', 'Sun, 12 Nov 2006'), ('Sun, 12 Nov 2006', 'Sun, 19 Nov 2006'), ('Sun, 19 Nov 2006', 'Sun, 26 Nov 2006'), ('Sun, 26 Nov 2006', 'Sun, 03 Dec 2006')] self.assertEqual([(wc.start.date().strftime('%a, %d %b %Y'), wc.end.date().strftime('%a, %d %b %Y')) for wc in self.group.weekly_chart_list[:10]], wcl) def testGroupGetWeeklyArtistChart(self): artists = [ 'Red Hot Chili Peppers', 'Led Zeppelin', 'Metallica', 'Pink Floyd', 'Queen', "Guns N' Roses", 'AC/DC', 'The Beatles', 'Nirvana', 'Muse' ] wc = self.group.weekly_chart_list[0] self.assertEqual([ artist.name for artist in self.group.get_weekly_artist_chart( wc.start, wc.end).artists[:10] ], artists) def testGroupGetWeeklyAlbumChart(self): albums = [('By the Way', 'Red Hot Chili Peppers', 48), ('Toxicity', 'System of a Down', 48), ('Stadium Arcadium', 'Red Hot Chili Peppers', 43), ('OK Computer', 'Radiohead', 41), ('Absolution', 'Muse', 39), ('Californication', 'Red Hot Chili Peppers', 38), ('Back in Black', 'AC/DC', 37), ('Black Holes And Revelations', 'Muse', 37), ('Appetite for Destruction', "Guns N' Roses", 35), ('Meteora', 'Linkin Park', 35)] wc = self.group.weekly_chart_list[0] self.assertEqual( [(album.name, album.artist.name, album.stats.playcount) for album in self.group.get_weekly_album_chart( wc.start, wc.end).albums[:10]], albums) def testGroupGetWeeklyTrackChart(self): tracks = [('Stairway to Heaven', 'Led Zeppelin', 36), ('Back in Black', 'AC/DC', 34), ('Enter Sandman', 'Metallica', 33), ('Dani California', 'Red Hot Chili Peppers', 33), ('Bohemian Rhapsody', 'Queen', 31), ('Welcome to the Jungle', "Guns N' Roses", 30), ('Supermassive Black Hole', 'Muse', 30), ('Wonderwall', 'Oasis', 29), ('Run to the Hills', 'Iron Maiden', 28), ('November Rain', "Guns N' Roses", 28)] wc = self.group.weekly_chart_list[0] self.assertEqual( [(track.name, track.artist.name, track.stats.playcount) for track in self.group.get_weekly_track_chart( wc.start, wc.end).tracks[:10]], tracks) def testGroupGetWeeklyTagChart(self): tags = [('classic rock', 156), ('rock', 154), ('alternative', 130), ('british', 112), ('alternative rock', 110), ('hard rock', 78), ('metal', 63), ('psychedelic', 61), ('thrash metal', 54), ('heavy metal', 47)] wc = self.group.weekly_chart_list[0] self.assertEqual([(tag.name, tag.stats.count) for tag in self.group.get_weekly_tag_chart( wc.start, wc.end).tags[:10]], tags)
def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache=True) self.group = self.api.get_group('Rock')
# Place here your API Key API_KEY = '' # username we are getting data from USERNAME = '' if __name__ == "__main__": if len(argv) < 3: print >> stderr, 'Usage: %s <last.fm API key> <last.fm username>' % basename(argv[0]) print >> stderr, 'Example: %s 91009080e1635307d49a60be883e616 jp' % basename(argv[0]) else: API_KEY=argv[1] USERNAME=argv[2] # connect and retrieve the user api = Api(API_KEY, no_cache = True) user = api.get_user(USERNAME) # now retrieve the favourite albums from last year top_albums = user.get_top_albums('12month') # print out the albums for the year print " Favourite albums" print "-" * 64 # loop through the albums for album in top_albums: # print the names just 30 characters, left aligned with spaces padding print u' {0:<30} - {1:<30} '.format( album.artist.name[:30], album.name[:30] )
def setUp(self): self.api = Api(api_key, no_cache = True) self.playlist = self.api.get_playlist('lastfm://playlist/album/2287667')
('Surfing With the Alien', 'Joe Satriani', 30), ('Cowboys', 'Portishead', 29), ('Trial of Tears', 'Dream Theater', 29), ('All Along the Watchtower', 'Jimi Hendrix', 28), ('Surrounded', 'Dream Theater', 28), ('Closer', 'Nine Inch Nails', 28), ('The Glass Prison', 'Dream Theater', 28), ('Superstition', 'Stevie Wonder', 28), ('Hollow Years', 'Dream Theater', 28)] self.assertEqual( [(track.name, track.artist.name, track.stats.playcount) for track in self.user.library.tracks[10:20]], tracks) apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache=True) data = { 'name': 'RJ', 'real_name': 'Richard Jones ', 'url': 'http://www.last.fm/user/RJ', 'image': { 'large': 'http://userserve-ak.last.fm/serve/126/8270359.jpg', 'medium': 'http://userserve-ak.last.fm/serve/64/8270359.jpg', 'small': 'http://userserve-ak.last.fm/serve/34/8270359.jpg' }, 'autheticated': False, 'events': [api.get_event(834368)] } for k, v in data.iteritems():
class LastFM(Worker): _title = 'lastfm' _key = None _lfm = None # wait this amount of seconds between lookups _lookup_threshold = 10 _last_lookup = None _history_filename = None _artist_history = {} _track_history = {} _db = None def __init__(self, config, no_cache=False): Worker.__init__(self) self._key = config.get('LastFM', 'key', None) self._lfm = Api(self._key, no_cache=no_cache) self._db = DB(config.get('LastFM', 'path', join(config.configdir(), 'echonest.db'))) self._db.execute( u"CREATE TABLE IF NOT EXISTS artist_lookup "\ "(artist text, timestamp integer, "\ "PRIMARY KEY(artist))") self._db.execute( u"CREATE TABLE IF NOT EXISTS track_lookup "\ "(artist text, title text, timestamp integer, "\ "PRIMARY KEY(artist, title))") self._db.commit() def get_artist_timestamp(self, artist): self._db.execute( u"SELECT timestamp FROM artist_lookup "\ "WHERE artist=?", (artist,)) row = self._db.fetchone() if row: return row[0] return 0 def set_artist_timestamp(self, artist, timestamp): self._db.execute( u"REPLACE INTO artist_lookup (artist, timestamp) "\ "VALUES (?, ?)", (artist, timestamp)) self._db.commit() def get_track_timestamp(self, artist, title): self._db.execute( u"SELECT timestamp FROM track_lookup "\ "WHERE artist=? and title=?", (artist, title)) row = self._db.fetchone() if row: return row[0] return 0 def set_track_timestamp(self, artist, title, timestamp): self._db.execute( u"REPLACE INTO track_lookup (artist, title, timestamp) "\ "VALUES (?, ?, ?)", (artist, title, timestamp)) self._db.commit() def _delay(self): if self._last_lookup: last_lookup = (datetime.utcnow() - self._last_lookup) if last_lookup.seconds < self._lookup_threshold: self._logger.debug(u"sleeping %ss" % ( self._lookup_threshold - last_lookup.seconds)) sleep(self._lookup_threshold - last_lookup.seconds) self._last_lookup = datetime.utcnow() def _track(self, artist, title): self._delay() result = self._lfm.search_track(track=title, artist=artist) # for some tracks (e.g. 'after all' from 'the peacocks' lastfm doesn't # return the correct track as first result (for 'peacocks' it works?!)) # so let's try to find the correct track for t in result: self._logger.info(u'{} {} vs {} {}'.format(artist, title, t.artist.name.lower() ,t.name.lower())) if t.artist.name.lower() == artist and t.name.lower() == title: return t if len(result) == 0: return None return result[0] # return the first def _artist(self, artist): self._delay() result = self._lfm.search_artist(artist=artist) for a in result: if a.name.lower() == artist: return a if len(result) == 0: return None return result[0] def similar_tracks(self, callback, artist, title, threshold): self.queue_callback(self._similar_tracks, callback, artist, title, threshold) def similar_tracks_low(self, callback, artist, title, threshold): self.queue_callback_low(self._similar_tracks, callback, artist, title, threshold) def _similar_tracks(self, callback, artist, title, threshold): """ callback(artist, title, sim_artist, sim_title, match, source) """ timestamp = now() diff = timestamp - self.get_track_timestamp(artist, title) if diff < threshold: self._logger.debug(u"similar_tracks[%s-%s]: looked up %d seconds ago" % (artist, title, diff)) return self.set_track_timestamp(artist, title, timestamp) try: self._logger.debug(u"similar_tracks[%s-%s]: lookup" % (artist, title)) t = self._track(artist, title) if t is None: self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title)) return else: p = t.similar self._logger.info(u"similar_tracks[%s-%s]: %d result(s)" % (artist, title, len(p))) for sim in p: callback(artist, title, sim.artist.name.lower(), sim.name.lower(), sim.stats.match, self._title) except URLError, e: self._logger.info(e) self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title)) return except Exception, e: self._logger.error(e) self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title)) return
('Paradice is Empty', 'I-Disagree'), ('Paradice', 'The Mods'), ('paradice slave', 'flower of flesh and blood'), ('return to paradice', 'Flunk'), ('return to paradice', 'Nostalgia 77'), ('return to paradice', 'Katalyst'), ('return to paradice', 'Ennio Morricone'), ('return to paradice', 'Cornucopia'), ('return to paradice', 'Lost Balance')] self.assertEqual( [(album.name, album.artist.name) for album in api.search_album("paradice")[:10]], albums ) from apikey import api_key api = Api(api_key, no_cache = True) data = { 'name': "Supersonic", 'artist': api.get_artist("Oasis"), 'id': 2038492, 'mbid': '2c2a24de-67b6-483b-b597-9c6b7891ba90', 'url': 'http://www.last.fm/music/Oasis/Supersonic', 'release_date': datetime.datetime(1994, 7, 28, 0, 0), 'image': { 'extralarge': 'http://userserve-ak.last.fm/serve/300x300/23049597.jpg', 'large': 'http://userserve-ak.last.fm/serve/174s/23049597.jpg', 'medium': 'http://userserve-ak.last.fm/serve/64s/23049597.jpg', 'small': 'http://userserve-ak.last.fm/serve/34s/23049597.jpg' } }
def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache = True) self.playlist = self.api.get_playlist('lastfm://playlist/album/2287667')
def testTrackSearch(self): tracks = [('Plug In Baby', 'Muse'), ('Tell Me Baby', 'Red Hot Chili Peppers'), ('Combat Baby', 'Metric'), ('Hey Baby', 'No Doubt'), ('Cry Baby Cry', 'The Beatles'), ('Find My Baby', 'Moby'), ('Nobody Puts Baby in the Corner', 'Fall Out Boy'), ('Baby Fratelli', 'The Fratellis'), ("I Can't Quit You Baby", 'Led Zeppelin'), ('Baby Britain', 'Elliott Smith')] self.assertEqual([(t.name, t.artist.name) for t in list(api.search_track('baby')[:10])], tracks) apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache=True) data = { 'name': 'Lithium', 'mbid': '', 'url': 'http://www.last.fm/music/Evanescence/_/Lithium', 'duration': 223000, 'streamable': True, 'full_track': False, 'artist': api.get_artist('Evanescence'), 'album': api.get_album('The Open Door', 'Evanescence'), 'position': 4, 'image': {} } for k, v in data.iteritems():
def setUp(self): self.api = Api(api_key, no_cache = True) self.group = self.api.get_group('Rock')
# Year we are reviewing the favourite tracks YEAR = 0 # username we are getting data from USERNAME = '' if __name__ == "__main__": if len(argv) < 4: print >> stderr, 'Usage: %s <last.fm API key> <last.fm username> <year>' % basename(argv[0]) print >> stderr, 'Example: %s 91009080e1635307d49a60be883e616 jp 2012' % basename(argv[0]) else: API_KEY=argv[1] USERNAME=argv[2] YEAR=argv[3] # connect and retrieve the user api = Api(API_KEY, no_cache = True) user = api.get_user(USERNAME) # now retrieve the favourite albums from last year top_tracks = user.get_top_tracks('12month') # print out the albums for the year print " Favourite tracks " print "-" * 120 # loop through the tracks for track in top_tracks: # we will onlys consider the songs having mbid if track.mbid: full_album = api.get_album( album=track.album.name, artist=track.artist.name ) track_year = '' if type(full_album.release_date) is datetime:
class TestTag(unittest.TestCase): """ A test class for the Tag module. """ def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache = True) self.tag = self.api.get_tag("rock").most_similar def tearDown(self): pass def testTagName(self): self.assertEqual(self.tag.name, "alternative") def testTagUrl(self): self.assertEqual(self.tag.url, "http://www.last.fm/tag/alternative") def testTagStreamable(self): self.assertEqual(self.tag.streamable, True) def testTagSimilar(self): tags = ['rock', 'indie', 'indie rock', 'alternative rock', 'seen live', 'nasty pop', 'electronic', 'electronica', 'kiwi indie niceness', 'singer-songwriter'] self.assertEqual([t.name for t in self.tag.similar[:10]], tags) def testTagMostSimilar(self): self.assertEqual(self.tag.most_similar.name, 'rock') def testTagTopAlbums(self): albums = [('OK Computer', 'Radiohead'), ('Nevermind', 'Nirvana'), ('In Rainbows', 'Radiohead'), ('Absolution', 'Muse'), ('Kid A', 'Radiohead'), ('InTRO', 'Bruno Sanfilippo'), ('A Long And Ugly Road (European edition)', 'June Madrona'), ('The Bends', 'Radiohead'), ('A Rush of Blood to the Head', 'Coldplay'), ('Black Holes And Revelations', 'Muse')] self.assertEqual( [(album.name, album.artist.name) for album in self.tag.top_albums[:10]], albums) def testTagTopAlbum(self): top_album = self.tag.top_album self.assertEqual((top_album.name, top_album.artist.name), ('OK Computer', 'Radiohead')) def testTagTopArtists(self): artists = ['Radiohead', 'Muse', 'Coldplay', 'Placebo', 'Red Hot Chili Peppers', 'Bj\xf6rk', 'Beck', 'The Smashing Pumpkins', 'Nirvana', 'The White Stripes'] self.assertEqual([artist.name for artist in self.tag.top_artists[:10]], artists) def testTagTopArtist(self): self.assertEqual(self.tag.top_artist.name, 'Radiohead') def testTagTopTracks(self): tracks = [('Nude', 'Radiohead'), ('Karma Police', 'Radiohead'), ('Creep', 'Radiohead'), ('Paranoid Android', 'Radiohead'), ('Starlight', 'Muse'), ('Clocks', 'Coldplay'), ('Wonderwall', 'Oasis'), ("Don't Let Him Waste Your Time", 'Jarvis Cocker'), ('Time Is Running Out', 'Muse'), ('Somebody Told Me', 'The Killers')] self.assertEqual( [(track.name, track.artist.name) for track in self.tag.top_tracks[:10]], tracks) def testTagTopTrack(self): top_track = self.tag.top_track self.assertEqual((top_track.name, top_track.artist.name), ('Nude', 'Radiohead')) def testTagPlaylist(self): self.assertEqual(self.tag.playlist.url, 'lastfm://playlist/tag/alternative/freetracks') def testTagWeeklyChartList(self): wcl = [('Sun, 25 May 2008', 'Sun, 01 Jun 2008'), ('Sun, 01 Jun 2008', 'Sun, 08 Jun 2008'), ('Sun, 08 Jun 2008', 'Sun, 15 Jun 2008'), ('Sun, 15 Jun 2008', 'Sun, 22 Jun 2008'), ('Sun, 22 Jun 2008', 'Sun, 29 Jun 2008'), ('Sun, 29 Jun 2008', 'Sun, 06 Jul 2008'), ('Sun, 06 Jul 2008', 'Sun, 13 Jul 2008'), ('Sun, 13 Jul 2008', 'Sun, 20 Jul 2008'), ('Sun, 20 Jul 2008', 'Sun, 27 Jul 2008'), ('Sun, 27 Jul 2008', 'Sun, 03 Aug 2008')] self.assertEqual( [ ( wc.start.date().strftime('%a, %d %b %Y'), wc.end.date().strftime('%a, %d %b %Y') ) for wc in self.tag.weekly_chart_list[:10] ], wcl ) def testTagGetWeeklyArtistChart(self): artists = [('Radiohead', 1199680000), ('Coldplay', 1000771456), ('Muse', 777053504), ('Placebo', 574809984), ('The Killers', 549784640), ('The Smashing Pumpkins', 523733376), ('The Cure', 503786272), ('Gorillaz', 488720000), ('Beck', 474790016), ('Weezer', 448279168)] wc = self.tag.weekly_chart_list[0] self.assertEqual( [(artist.name, artist.stats.weight) for artist in self.tag.get_weekly_artist_chart(wc.start, wc.end).artists[:10]], artists) def testTagGetTopTags(self): tags = ['rock', 'seen live', 'alternative', 'indie', 'electronic', 'pop', 'metal', 'female vocalists', 'alternative rock', 'classic rock'] self.assertEqual([tag.name for tag in self.api.get_global_top_tags()[:10]], tags) def testTagSearch(self): tags = ['alternative', 'alternative rock', 'alternative metal', 'alternative pop', 'alternative dance', 'alternative rap', 'alternative country', 'adult alternative', 'alternative hip-hop', 'alternative folk'] self.assertEqual([tag.name for tag in self.api.search_tag('alternative')[:10]], tags)
class TestTag(unittest.TestCase): """ A test class for the Tag module. """ def setUp(self): self.api = Api(api_key, no_cache = True) self.tag = self.api.get_tag("rock").most_similar def tearDown(self): pass def testTagName(self): self.assertEqual(self.tag.name, "alternative") def testTagUrl(self): self.assertEqual(self.tag.url, "http://www.last.fm/tag/alternative") def testTagStreamable(self): self.assertEqual(self.tag.streamable, True) def testTagSimilar(self): tags = ['rock', 'indie', 'indie rock', 'alternative rock', 'seen live', 'nasty pop', 'electronic', 'electronica', 'kiwi indie niceness', 'singer-songwriter'] self.assertEqual([t.name for t in self.tag.similar[:10]], tags) def testTagMostSimilar(self): self.assertEqual(self.tag.most_similar.name, 'rock') def testTagTopAlbums(self): albums = [('OK Computer', 'Radiohead'), ('Nevermind', 'Nirvana'), ('In Rainbows', 'Radiohead'), ('Absolution', 'Muse'), ('Kid A', 'Radiohead'), ('InTRO', 'Bruno Sanfilippo'), ('A Long And Ugly Road (European edition)', 'June Madrona'), ('The Bends', 'Radiohead'), ('A Rush of Blood to the Head', 'Coldplay'), ('Black Holes And Revelations', 'Muse')] self.assertEqual( [(album.name, album.artist.name) for album in self.tag.top_albums[:10]], albums) def testTagTopAlbum(self): top_album = self.tag.top_album self.assertEqual((top_album.name, top_album.artist.name), ('OK Computer', 'Radiohead')) def testTagTopArtists(self): artists = ['Radiohead', 'Muse', 'Coldplay', 'Placebo', 'Red Hot Chili Peppers', u'Bj\xf6rk', 'Beck', 'The Smashing Pumpkins', 'Nirvana', 'The White Stripes'] self.assertEqual([artist.name for artist in self.tag.top_artists[:10]], artists) def testTagTopArtist(self): self.assertEqual(self.tag.top_artist.name, 'Radiohead') def testTagTopTracks(self): tracks = [('Nude', 'Radiohead'), ('Karma Police', 'Radiohead'), ('Creep', 'Radiohead'), ('Paranoid Android', 'Radiohead'), ('Starlight', 'Muse'), ('Clocks', 'Coldplay'), ('Wonderwall', 'Oasis'), ("Don't Let Him Waste Your Time", 'Jarvis Cocker'), ('Time Is Running Out', 'Muse'), ('Somebody Told Me', 'The Killers')] self.assertEqual( [(track.name, track.artist.name) for track in self.tag.top_tracks[:10]], tracks) def testTagTopTrack(self): top_track = self.tag.top_track self.assertEqual((top_track.name, top_track.artist.name), ('Nude', 'Radiohead')) def testTagPlaylist(self): self.assertEqual(self.tag.playlist.url, 'lastfm://playlist/tag/alternative/freetracks') def testTagWeeklyChartList(self): wcl = [('Sun, 25 May 2008', 'Sun, 01 Jun 2008'), ('Sun, 01 Jun 2008', 'Sun, 08 Jun 2008'), ('Sun, 08 Jun 2008', 'Sun, 15 Jun 2008'), ('Sun, 15 Jun 2008', 'Sun, 22 Jun 2008'), ('Sun, 22 Jun 2008', 'Sun, 29 Jun 2008'), ('Sun, 29 Jun 2008', 'Sun, 06 Jul 2008'), ('Sun, 06 Jul 2008', 'Sun, 13 Jul 2008'), ('Sun, 13 Jul 2008', 'Sun, 20 Jul 2008'), ('Sun, 20 Jul 2008', 'Sun, 27 Jul 2008'), ('Sun, 27 Jul 2008', 'Sun, 03 Aug 2008')] self.assertEqual( [ ( wc.start.date().strftime('%a, %d %b %Y'), wc.end.date().strftime('%a, %d %b %Y') ) for wc in self.tag.weekly_chart_list[:10] ], wcl ) def testTagGetWeeklyArtistChart(self): artists = [('Radiohead', 1199680000), ('Coldplay', 1000771456), ('Muse', 777053504), ('Placebo', 574809984), ('The Killers', 549784640), ('The Smashing Pumpkins', 523733376), ('The Cure', 503786272), ('Gorillaz', 488720000), ('Beck', 474790016), ('Weezer', 448279168)] wc = self.tag.weekly_chart_list[0] self.assertEqual( [(artist.name, artist.stats.weight) for artist in self.tag.get_weekly_artist_chart(wc.start, wc.end).artists[:10]], artists) def testTagGetTopTags(self): tags = ['rock', 'seen live', 'alternative', 'indie', 'electronic', 'pop', 'metal', 'female vocalists', 'alternative rock', 'classic rock'] self.assertEqual([tag.name for tag in self.api.get_global_top_tags()[:10]], tags) def testTagSearch(self): tags = ['alternative', 'alternative rock', 'alternative metal', 'alternative pop', 'alternative dance', 'alternative rap', 'alternative country', 'adult alternative', 'alternative hip-hop', 'alternative folk'] self.assertEqual([tag.name for tag in self.api.search_tag('alternative')[:10]], tags)
def testCountryTopTrack(self): top_track = self.country.top_track self.assertEqual((top_track.name, top_track.artist.name), ('Dream Fighter', 'Perfume')) def testCountryEvents(self): event_ids = [ 961510, 925636, 959392, 875466, 951038, 950520, 957543, 930614, 871240, 857063 ] self.assertEqual([e.id for e in self.country.events[:10]], event_ids) apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache=True) data = { 'city': "Tokyo", 'country': api.get_country("Japan"), 'street': '1-3-61, Koraku, Bunkyo-ku', 'postal_code': '112-8562' } for k, v in data.iteritems(): def testFunc(self): self.assertEqual(getattr(self.location, k), v) setattr(TestGeo, "testLocation%s" % k.replace('_', ' ').title().replace(' ', ''), testFunc)
def setUp(self): self.api = Api(api_key, no_cache = True) self.tag = self.api.get_tag("rock").most_similar
def testArtistTopTracks(self): tracks = [ 'You Give Love a Bad Name', "Livin' on a Prayer", "It's My Life", 'Wanted Dead or Alive', 'Always', 'Bed of Roses', 'Have a Nice Day', 'Runaway', 'Bad Medicine', 'Keep the Faith' ] self.assertEqual([track.name for track in self.artist.top_tracks[:10]], tracks) def testArtistTopTrack(self): self.assertEqual(self.artist.top_track.name, 'You Give Love a Bad Name') apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache=True) data = { 'name': 'Bon Jovi', 'mbid': '5dcdb5eb-cb72-4e6e-9e63-b7bace604965', 'url': 'http://www.last.fm/music/Bon+Jovi', 'image': { 'large': 'http://userserve-ak.last.fm/serve/126/24125.jpg', 'medium': 'http://userserve-ak.last.fm/serve/64/24125.jpg', 'small': 'http://userserve-ak.last.fm/serve/34/24125.jpg' }, 'streamable': False } for k, v in data.iteritems():
def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache = True) self.group = self.api.get_group('Rock')
def testUserLibraryTracks(self): tracks = [('All Right Now', 'Free', 30), ('Surfing With the Alien', 'Joe Satriani', 30), ('Cowboys', 'Portishead', 29), ('Trial of Tears', 'Dream Theater', 29), ('All Along the Watchtower', 'Jimi Hendrix', 28), ('Surrounded', 'Dream Theater', 28), ('Closer', 'Nine Inch Nails', 28), ('The Glass Prison', 'Dream Theater', 28), ('Superstition', 'Stevie Wonder', 28), ('Hollow Years', 'Dream Theater', 28)] self.assertEqual([(track.name, track.artist.name, track.stats.playcount) for track in self.user.library.tracks[10:20]], tracks) apikey = "152a230561e72192b8b0f3e42362c6ff" api = Api(apikey, no_cache = True) data = { 'name': 'RJ', 'real_name': 'Richard Jones ', 'url': 'http://www.last.fm/user/RJ', 'image': {'large': 'http://userserve-ak.last.fm/serve/126/8270359.jpg', 'medium': 'http://userserve-ak.last.fm/serve/64/8270359.jpg', 'small': 'http://userserve-ak.last.fm/serve/34/8270359.jpg'}, 'autheticated': False, 'events': [api.get_event(834368)] } for k,v in data.iteritems(): def testFunc(self): self.assertEqual(getattr(self.user, k), v) setattr(TestUser, "testUser%s" % k.replace('_', ' ').title().replace(' ', ''), testFunc)
def setUp(self): apikey = "152a230561e72192b8b0f3e42362c6ff" self.api = Api(apikey, no_cache=True) self.tag = self.api.get_tag("rock").most_similar