-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathezportify.py
154 lines (142 loc) · 4.24 KB
/
ezportify.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from getpass import getpass
import sys
from google import Mobileclient
import urllib
import urllib2
import json
import os
import argparse
import platform
if ".exe" in sys.argv[0]:
os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(os.getcwd(), 'cacert.pem') ## This is needed to make the .exe version work
try:
import certifi
except:
print "You need to easy_install certifi"
input("Press enter to exit")
sys.exit()
try:
import gpsoauth
except:
print "You need to easy_install gpsoauth"
input("Press enter to exit")
sys.exit()
try:
input = raw_input
except NameError:
pass
def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
if sys.stdin is not sys.__stdin__:
return fallback_getpass(prompt, stream)
import msvcrt
for c in prompt:
msvcrt.putch(c)
pw = ""
while 1:
c = msvcrt.getwch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
if pw == '':
pass
else:
pw = pw[:-1]
msvcrt.putch('\b')
msvcrt.putch(" ")
msvcrt.putch('\b')
else:
pw = pw + c
msvcrt.putch("*")
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw
def hitapi(oauth, url):
headers = { 'Authorization' : 'Bearer ' + oauth}
req = urllib2.Request(url, headers=headers)
try:
response = urllib2.urlopen(req)
except urllib2.HTTPError, e:
response = e.fp
the_page = response.read()
return json.loads(the_page)
def googlelogin():
google_email = input("Enter Google email address: ")
if platform.system() == "Windows":
google_pass = win_getpass("Enter Google password: ")
else:
google_pass = getpass("Enter Google password: ")
googleapi = Mobileclient()
google_loggedin = googleapi.login(google_email, google_pass)
if not google_loggedin:
print "Invalid Google username/password"
input("Press enter to exit")
sys.exit()
return googleapi
def main(args):
if not args.dump:
googleapi = googlelogin()
print "Enter Spotify OAUTH Token from https://developer.spotify.com/web-api/console/get-current-user-playlists/ "
oauth = input("Be sure to check the Relevant scopes checkbox: ")
playlists = hitapi(oauth, 'https://api.spotify.com/v1/me/playlists')
if 'error' in playlists.keys():
print "The oauth token is invalid"
print "Make sure you check the checkbox or checkboxes under 'Relevant scopes'. Clear your token and try again if needed"
input("Press enter to exit")
sys.exit()
items = playlists['items']
while playlists['next']:
playlists = hitapi(oauth, playlists['next'])
items += playlists['items']
print len(items)
f = open('ezportify-tracks.txt', 'w')
for playlist in items:
try:
print playlist
print playlist['name']
f.write('\n:::' + playlist['name'] + ":::\n")
queries = []
gtracks = []
trackresp = hitapi(oauth, playlist['href'])['tracks']
tracks = trackresp['items']
while trackresp['next']:
trackresp = hitapi(oauth, trackresp['next'])
tracks += trackresp['items']
ot = 1
for track in tracks:
try:
searchstr = ''
if 'artists' in track['track'].keys() and track['track']['artists']:
searchstr += track['track']['artists'][0]['name'] + ' - '
searchstr += track['track']['name']
searchstr_ascii = searchstr.encode("utf-8", "replace")
f.write(searchstr_ascii + "\n")
gtrack = None
if not args.dump:
gtrack = googleapi.find_best_track(searchstr_ascii)
if gtrack:
gtracks.append(gtrack["nid"])
print ot, '/', len(tracks), 'found', searchstr_ascii
else:
print ot, '/', len(tracks), 'Not found', searchstr_ascii, 'for', playlist['name']
except:
print 'Track', ot, 'Failed'
ot += 1
if len(gtracks) > 0:
print "Creating in Google Music... ",
playlist_id = googleapi.create_playlist(playlist['name'])
googleapi.add_songs_to_playlist(playlist_id, gtracks)
print "Done"
except:
print playlist['name'], 'Failed to copy. Skipping'
f.close()
input("Press enter to exit")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Spotify To Google Playlist Transcription')
parser.add_argument("-d", "--dump", help="Only Dump Playlists To File",
action="store_true")
parser.add_argument('--version', action='version', version='%(prog)s 0.4')
args = parser.parse_args()
main(args)