Skip to content

Instantly share code, notes, and snippets.

@kinkerl
Created December 27, 2012 20:00
Show Gist options
  • Save kinkerl/4391467 to your computer and use it in GitHub Desktop.
Save kinkerl/4391467 to your computer and use it in GitHub Desktop.
google music backup download
#!/usr/bin/env python
# https://github.com/simon-weber/Unofficial-Google-Music-API
from gmusicapi.api import Api
from getpass import getpass
import os
import requests
def init():
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 get_absolute_music_path(f):
return os.path.join('music', f['artist'], f['album'])
def get_absolute_music_filename(f):
return os.path.join(get_absolute_music_path(f), f['title'] + ".mp3")
def main():
api = init()
if not api.is_authenticated():
print "Sorry, those credentials weren't accepted."
return
library = api.get_all_songs()
for f in library[:3]:
if not os.path.exists(get_absolute_music_filename(f)):
if not os.path.exists(get_absolute_music_path(f)):
os.makedirs(get_absolute_music_path(f))
if not os.path.exists(get_absolute_music_filename(f)):
print "."
url, count = api.get_song_download_info(f['id'])
req = requests.get(url)
with open(get_absolute_music_filename(f), 'w') as target:
target.write(req.content)
api.logout()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment