def __init__(self, username, scope, client_id, client_secret, redirect_uri): self.username = username self.scope = scope self.saved_tracks = MusicLibrary(username) self.token = None self.CLIENT_ID = client_id self.CLIENT_SECRET = client_secret self.REDIRECT_URI = redirect_uri
def test_synchronize_list(self): mp = Deezer(self.username) mp.connect() f = open('.\\' + self.username + '_saved_tracks.txt', 'r', encoding='utf8') lines = f.readlines() f.close() music_list = MusicLibrary('vinye_mustaine') for line in lines: music_list.add(Song(*line.split('\t'))) print(str(music_list)) mp.synchronize_list(music_list.getsongs()) print(str(len(mp.saved_tracks.getsongs())))
def test_massive_song_add2(self): username = '' f = open('.\\'+ username +'_saved_tracks.txt','r', encoding='utf8') lines = f.readlines() f.close() l = MusicLibrary() i = 0 for x in range(0, len(lines)-1): if len(lines[x]) > 0: s = Song(*((lines[x]).replace('\n','').split('\t'))) l.add(s) i+=1 self.assertEqual(i, l.song_len(), "could not add - " + str(i)) '''try: self.assertEqual(i, l.song_len(), "could not add - " + str(i))
def test_getsongs(self): l = MusicLibrary() self.assertEqual(len(l.getsongs()), 0) for x in range(65,90): for y in range(65,90): for z in range(65,90): l.add(Song(chr(x),chr(y),chr(z))) self.assertEqual(len(l.getsongs()), pow(25,3))
def test_massive_song_add(self): l = MusicLibrary() ix, iy, iz = 0, 0, 0 for x in range(65,90): ix+=1 for y in range(65,90): iy+=1 for z in range(65,90): iz+=1 l.add(Song(chr(x),chr(y),chr(z))) self.assertEqual(ix, l.artist_len(), "wrong artist len") self.assertEqual(iy, l.album_len(), "wrong album number") self.assertEqual(iz, l.song_len(), "wrong song number")
def test_addsong(self): lib = MusicLibrary() lib.add(self.songs) teststr = 'Artists: 2\nAlbums: 3\nSongs: 4' self.assertEqual(str(lib), teststr)
def test_addartist(self): lib = MusicLibrary() lib.add(self.artists) teststr = 'Artists: 3\nAlbums: 4\nSongs: 8' self.assertEqual(str(lib), teststr)
def test_str(self): teststr = 'Artists: 3\nAlbums: 4\nSongs: 8' self.assertEqual(str(MusicLibrary(artists=self.artists)),teststr)
def test_constructor(self): self.assertEqual(MusicLibrary('A').name, 'A') self.assertEqual(len(MusicLibrary(artists=self.artists)),3)
def test_setitem(self): self.assertEqual(list(MusicLibrary(artists=self.artists).keys()),['A','B','C'])
class MusicProvider(): def __init__(self, username, scope, client_id, client_secret, redirect_uri): self.username = username self.scope = scope self.saved_tracks = MusicLibrary(username) self.token = None self.CLIENT_ID = client_id self.CLIENT_SECRET = client_secret self.REDIRECT_URI = redirect_uri def __export_txt__(self, location): f = None songlist = self.saved_tracks.getsongs() try: f = open(location + '\\' + self.username + '_saved_tracks.txt', 'w', encoding='utf8') for song in songlist: s = '\t'.join([ song['name'], song['album'], song['artist'], str(song['track_number']), str(song['duration_ms']), song['track_id'] ]) f.write(s + '\n') f.close() return True except Exception: if f is not None: f.close() return False def __export_json__(self, location): f = None try: f = open(location + '\\' + self.username + '_saved_tracks.json', 'w') f.write(json.dumps(self.saved_tracks)) f.close() return True except Exception: if f is not None: f.close() return False def export(self, format=MusicExportFormat.TXT, location='.\\'): if format is MusicExportFormat.TXT: return self.__export_txt__(location) elif format is MusicExportFormat.JSON: return self.__export_json__(location) else: return False def connect(self): raise NotImplementedError("Please Implement connect") def reconnect(self): raise NotImplementedError("Please Implement reconnect") def get_saved_tracks(self): raise NotImplementedError("Please Implement get_saved_tracks") def _ask_for_permission(self, url): print(''' User authentication requires interaction with your web browser. Once you enter your credentials and give authorization, you will be redirected to a url. Paste that url you were directed to to complete the authorization. ''') auth_url = url try: webbrowser.open(auth_url) print("Opened %s in your browser" % auth_url) except: print("Please navigate here: %s" % auth_url) print() print() try: response = raw_input("Enter the URL you were redirected to: ") except NameError: response = input("Enter the URL you were redirected to: ") print() print() try: return response.split("?code=")[1].split("&")[0] except IndexError: return None