master
1!/usr/bin/env python
2
3# Rename? gm fs to playlists
4
5# gm-utp.py
6# google music api - upload to playlist
7# aguments
8# folder containing .mp3's
9# name of playlist to create
10#
11# intall : pip install googleapi
12# tested with python version 2.7.2
13
14
15# TODO
16# - fix check-for-existing-playlist to only add songs if they are new
17# - fix check for existing song in playlist
18# - fix trailing / in folder split
19# - add dirwalk, if it is a leaf folder add it as a playlist
20# - move add songs to playlist to outside of loop
21# - add to multiple playlists (-n xxx yyy zzz), works nicely with -d maybe
22# - Try/Catch uploading#
23
24
25from gmusicapi.api import Api
26from getpass import getpass
27import os
28import argparse
29
30def parse():
31 parser = argparse.ArgumentParser(
32 description='Upload music to Google Music Playlists')
33 parser.add_argument(
34 'folder', action='store',
35 help='the folder of music to upload')
36 parser.add_argument(
37 '-n', action='store', dest='playlist_name',
38 help='set the playlist name')
39 parser.add_argument(
40 '-d', action='store_true', default=False,
41 help='set playlist_name=folder')
42 parser.add_argument(
43 '-u', action='store', dest='user_name',
44 help='Google Username')
45 parser.add_argument(
46 '-p', action='store', dest='password',
47 help='Google Single Application Password reccomended')
48 parser.add_argument(
49 '-v', action='store_true', default=False,
50 help='verbose mode')
51
52 args = parser.parse_args()
53
54 # I have a feeling this logic can be done by argparse
55 # For now this is good enough
56 if args.playlist_name == None and args.d == True :
57 tmp = args.folder.split('/')
58 args.playlist_name = tmp[len(tmp)-1] # least sig dir
59 elif args.playlist_name != None and args.d == True :
60 if args.v : print "Warning : -n has priority, ignoring -d"
61
62 return args
63
64def init(args):
65 api = Api()
66 if args.user_name == None:
67 email = raw_input("Email: ")
68 else :
69 email = args.user_name
70 if args.password == None:
71 password = getpass()
72 else :
73 password = args.password
74 print "Signing in : " + email
75 logged_in = api.login(email, password)
76 return api
77
78def main():
79 # Parse the cli args then setup a google music api connection
80 args = parse()
81 api = init(args)
82
83 # check if authentication succeded
84 if not api.is_authenticated():
85 print "Sorry, those credentials weren't accepted."
86 return
87 if args.v : print "Login Success"
88
89 user_playlists = api.get_all_playlist_ids(auto=False,user=True)["user"]
90 if not args.playlist_name in user_playlists :
91 if args.v : print "Creating Playlist : " + args.playlist_name
92 playlist_id = api.create_playlist(args.playlist_name)
93 else :
94 playlist_id = user_playlists[args.playlist_name]
95 print "Current Playlist: "
96 print api.get_playlist_songs(playlist_id)
97
98 # get to the right dir where the files are
99 os.chdir(args.folder)
100
101 # upload and playlist-ize each .mp3 in the folder
102 for files in os.listdir("."):
103 if files.endswith(".mp3"):
104 if args.v : print "Uploading : " + files
105 u = api.upload(files)
106 song_id = u[files]
107 api.add_songs_to_playlist(playlist_id, song_id)
108
109 #It's good practice to logout when finished.
110 if args.v : print "Logging off."
111 api.logout()
112
113if __name__ == '__main__':
114 main()