-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
66 lines (53 loc) · 1.91 KB
/
convert.py
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
import gmusic
import spotify
from Track import *
from Playlist import *
from os import getenv
def main():
# Google Music MobileClient api
gm_api = gmusic.login_to_gmusic_with_oauth()
print('Logged in to Google music using ', getenv('GMUSIC_OAUTH_CREDS_PATH'))
all_songs = gm_api.get_all_user_playlist_contents()
# Spotify api
sp_api = spotify.login2spotify()
user = sp_api.me()
print('Logged in to spotify as ', user['display_name'])
sp_user_id = user['id']
found = False
while not found:
try:
pl_name = input("Playlist name: ")
pl = next((p for p in all_songs if p['name'] == pl_name), None)
if pl == None:
print('Could not find playlist with name ', pl_name,
', please try again.')
else:
found = True
except EOFError:
print('Ok yes goodbye')
sys.exit(1)
# Found desired playlist, so get data about its tracks
all_song_meta_data = gm_api.get_all_songs()
badtrackID = ''
tracks = []
count = 0
for t in pl['tracks']:
# Check for bad source.
# '2' indicates hosted on Google Music, '1' otherwise
if t['source'] == '1':
badtrackID = t['trackId']
song = next((t for t in all_song_meta_data if t['id'] == badtrackID), None)
else: # t['source'] == '2'
song = t['track']
if song is not None:
count += 1
title = song['title']
artist = song['artist']
album = song['album']
track = Track(title=title, artist=artist, album=album)
tracks.append(track)
new_playlist = Playlist(tracks=tracks, plTitle=pl_name)
print('ok, added ', count, ' songs to Playlist object with length ', new_playlist.length)
spotify.new_playlist(sp_api, new_playlist, interactive=True)
if __name__ == "__main__":
main()