-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmusic-csv.py
156 lines (145 loc) · 6.15 KB
/
gmusic-csv.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
155
156
#!/usr/bin/env pyhton
from csv import DictWriter
from gmusicapi import Mobileclient
from operator import itemgetter
from gmusicapi.exceptions import NotLoggedIn
import getpass, os.path
import sys, getopt
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding("utf-8")
""""(CallFailure, ParseException, ValidationException,
AlreadyLoggedIn, NotLoggedIn)"""
# Fix Python 2.x.
try: input = raw_input
except NameError: pass
#GLOBAL VARS
api = Mobileclient()
content = []
helpString = ("usage: gmusic-csv [option]\noptions:"
+"\n -o <filename> : preemptively name CSV file"
+"\n -p '<playlist>' : get songs in playlist"
+"\n -h : display this menu\nexample:"
+"\n gmusic-csv -o my_library"
+"\n gmusic-csv -p 'summer 2013'")
class Credintials(object):
def login(self, *args):
res = api.oauth_login(Mobileclient.FROM_MAC_ADDRESS)
if (not res):
print ('\n===================================================='
+ '\nPlease log in. Credentials will be saved as'
+ '\n' + Mobileclient.OAUTH_FILEPATH + '.'
+ '\n----------------------------------------------------')
Mobileclient.perform_oauth(storage_filepath=Mobileclient.OAUTH_FILEPATH, open_browser=True)
res = api.oauth_login(Mobileclient.FROM_MAC_ADDRESS)
print ('----------------------------------------------------')
if (not res):
print ('Login failed. Try again or press ctrl+c to cancel.')
Credintials.login(self, *args)
return
else:
print ('Login successful!\nnow performing selected or default option...')
print ('====================================================')
class Library(object):
#get all music owned by user
def getMusic(self):
try:
print('retrieving library...\n'),
tempList = api.get_all_songs()
for x in range(len(tempList)):
gatherList = {
"album": tempList[x].get('album'),
"artist": tempList[x].get('artist'),
"name": tempList[x].get('title'),
"trackNumber": tempList[x].get('trackNumber'),
"playCount": tempList[x].get('playCount')
}
content.append(gatherList)
except NotLoggedIn:
sys.exit("Error processing request. Error: NotLoggedIn.")
#get songs from specified playlist in users collection
def getPlaylist(self, name):
try:
print('retrieving playlist.'),
playlists = api.get_all_user_playlist_contents()
allSongs = api.get_all_songs()
allSongsDict = dict()
for x in range(len(allSongs)):
gatherList = {
"album": allSongs[x].get('album'),
"artist": allSongs[x].get('artist'),
"name": allSongs[x].get('title'),
"trackNumber": allSongs[x].get('trackNumber'),
"playCount": allSongs[x].get('playCount')
}
allSongsDict[allSongs[x].get('id')] = gatherList
for playlist in playlists:
if playlist['name'] == name:
tracks = playlist['tracks']
for track in tracks:
id = track['trackId']
#print(id)
if id in allSongsDict:
print(allSongsDict[id])
content.append(allSongsDict[id])
except NotLoggedIn:
sys.exit("Could not log in - verify credintials.")
class WriteOut(object):
def writeToFile(self, filename):
if "." in filename:
filename = filename.split(".")[0]
elif filename == "":
filename = input('save file as: ')
temp = sorted(content, key = itemgetter('trackNumber'))
temp = sorted(temp, key = itemgetter('album'))
temp = sorted(temp, key = itemgetter('artist'))
# filename = input('save file as (do not include .csv or any extension!)\n: ')
if os.path.exists(str(filename)+'.csv'):
print (filename, ' exists already. Choose another file name.')
WriteOut.writeToFile(self, filename)
return
try:
with open(filename+'.csv','w') as outfile:
print (" exporting library to CSV format!"),
writer = DictWriter(outfile, ('artist','album','trackNumber','name','playCount'))
writer.writeheader()
writer.writerows(temp)
print ('\n \''+str(filename)+str('.csv')+'\' saved in current directory!')
except IOError:
sys.exit("<Error> Invalid filename!")
def main(argv):
try:
# option = "library"
filename = ""
playlist = ""
try:
opts, args = getopt.getopt(argv, "hp:o:")
except:
print(helpString)
sys.exit(2)
for flag, argument in opts:
if flag == "-h":
print(helpString)
sys.exit(2)
elif flag == "-o":
filename = str(argument)
if filename == "":
print ("enter a <filename> when using the -o flag.")
sys.exit(2)
elif flag == "-p":
playlist = str(argument)
Credintials().login()
if playlist == "":
Library().getMusic()
else:
Library().getPlaylist(playlist)
if len(content) <=1:
print ('{} does not exist as a playlist. Try again.').format(playlist)
sys.exit(2)
print ('====================================================')
WriteOut().writeToFile(filename)
print ('====================================================')
except KeyboardInterrupt:
print ("\nShutdown requested...exiting")
if __name__ == '__main__':
main(sys.argv[1:])