def testGetLyricsXML(self): '''testGetLyricsXML - Ensures the lyrics are fetched correctly''' lyrics = LyricsDownloader('On A Plain', 'Nirvana', False) lyrics._clean_up_artist_title() test_xml = urllib.urlopen(THIS_DIR + '/data/LyricsDownloader/lyrics_xml').read() self.assertEqual(lyrics._get_lyrics_xml(), test_xml)
def testCleanUpArtistTitle(self): '''testCleanUpArtistTitle - Ensures the artist is converted for use in the url''' callback = False lyrics = LyricsDownloader('Foo', "Test 123 ()$^*=:;|#@}{][!,.-_\\&'", callback) lyrics._clean_up_artist_title() self.assertEqual(lyrics.artist, 'test%20123%20()$^*=:;|#@}{][!,.-_\\%%')
def testCleanUpArtistTitle(self): '''testCleanUpArtistTitle - Ensures the artist is converted for use in the url''' callback = False lyrics = LyricsDownloader('Foo', "Test 123 ()$^*=:;|#@}{][!,.-_\\&'" , callback) lyrics._clean_up_artist_title() self.assertEqual(lyrics.artist, 'test%20123%20()$^*=:;|#@}{][!,.-_\\%%')
def testParseLyricsXML(self): '''testParseLyricsXML - Ensures the lyrics are parsed correctly''' callback = False lyrics = LyricsDownloader('On A Plain', 'Nirvana', callback) test_xml = urllib.urlopen(THIS_DIR + '/data/LyricsDownloader/lyrics_xml').read() final_lyrics = lyrics._parse_lyrics_xml(test_xml) temp = open(THIS_DIR + '/data/LyricsDownloader/final_lyrics') final_lyrics_from_file = temp.read() self.assertEqual(final_lyrics, final_lyrics_from_file)
def fetch_lyrics(self, callback=False): '''Fetch lyrics from the Internet using the LyricsDownloader, use a callback function to indicate completion since LyricsDownloader is asynchronous. Callback function must take lyrics as only input parameter.''' if not callback: callback = self.set_lyrics if not self.has_lyrics(): self.ld = LyricsDownloader(self.title, self.artist, callback) self.ld.search()
def testLyricsConstructor(self): '''testMetadataConstructor - Ensures instantiation of Lyric Downloader class''' callback = False lyrics = LyricsDownloader('Foo', 'Bar', callback) self.assertTrue(isinstance(lyrics, LyricsDownloader))
class Track(Playable): '''Representation of a music track.''' def __init__(self, filename, title, number, artist, album, year, length, lyrics, create_album_callback): Playable.__init__(self) # Check that these fields are integers for field in [number, year, length]: if type(field) != int: raise TrackTypeError("%s is not an integer" % field) self.filename = filename self.title = title self.number = number self.artist = artist self._album = album self.year = year # Length of the track in seconds (example 240) self.length = length self._lyrics = lyrics self.create_album_callback = create_album_callback @property def album(self): '''Get the album object and use the callback if it's not an instance.''' if not isinstance(self._album, Album): album = self.create_album_callback(self._album) self._album = album return album else: return self._album @property def length_string(self): '''Return length as a human readable string. Example: 2:46.''' return str(self.length / 60) + ":" + str.zfill(str(self.length % 60), 2) def _get_lyrics(self): '''Get the track lyrics.''' return self._lyrics def _set_lyrics(self, lyrics): '''Set the lyrics. LyricsDownloader returns None if it can't find anything so ensure that using lyrics is always a string.''' if lyrics is None: self._lyrics = '' else: self._lyrics = lyrics lyrics = property(_get_lyrics, _set_lyrics) def has_lyrics(self): '''Test if the track has any lyrics.''' if self.lyrics == "": return False else: return True def fetch_lyrics(self, callback=False): '''Fetch lyrics from the Internet using the LyricsDownloader, use a callback function to indicate completion since LyricsDownloader is asynchronous. Callback function must take lyrics as only input parameter.''' if not callback: callback = self.set_lyrics if not self.has_lyrics(): self.ld = LyricsDownloader(self.title, self.artist, callback) self.ld.search() def get_album_art_url(self): '''Get the album art location if it exists.''' if self.album.has_album_art(): return self.album.album_art_url else: return None # Implement playable interface def get_title(self): '''Get the title.''' return self.title def get_type(self): '''Get the type.''' return Playable.AUDIO_STREAM def get_uri(self): '''Get the URI.''' return "file://" + self.filename