forked from simon-weber/gmusicapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
100 lines (69 loc) · 2.64 KB
/
example.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
#!/user/bin/env python
#Copyright 2012 Simon Weber.
#This file is part of gmusicapi - the Unofficial Google Music API.
#Gmusicapi is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#Gmusicapi is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with gmusicapi. If not, see <http://www.gnu.org/licenses/>.
from gmusicapi.api import Api
from getpass import getpass
def init():
"""Makes an instance of the api and attempts to login with it.
Returns the authenticated api.
"""
api = Api()
logged_in = False
attempts = 0
while not logged_in and attempts < 3:
email = raw_input("Email: ")
password = getpass()
logged_in = api.login(email, password)
attempts += 1
return api
def main():
"""Demonstrates some api features.
Logs in, gets library, searches for a song, selects the first result,
then creates a new playlist and adds that song to it.
Finally, it renames and deletes the playlist.
"""
api = init()
print "Loading library...",
library = api.get_all_songs()
print "done"
print len(library), "tracks detected."
print
query = raw_input("Search Query: ")
search_results = api.search(query)
#Note that this only looks at hits on songs.
#Songs matched on artist/album hits are discarded by selecting ['songs'].
songs = search_results['results']['songs']
if len(songs) == 0:
print "No songs from that search."
return
song = songs[0]
print "Selected", song['title'],"by",song['artist']
song_id = song['id']
playlist_name = raw_input("New playlist name: ")
res = api.create_playlist(playlist_name)
if not res['success']:
print "Failed to make the playlist."
return
print "Made new playlist named",res['title']
playlist_id = res['id']
res = api.add_songs_to_playlist(playlist_id, song_id)
print "Added to playlist."
res = api.change_playlist_name(playlist_id, "api playlist")
print "Changed playlist name to 'api playlist'."
raw_input("Press enter to delete the playlist.")
res = api.delete_playlist(playlist_id)
print "Deleted playlist."
print "Done!"
api.logout()
if __name__ == '__main__':
main()