Skip to content

Instantly share code, notes, and snippets.

@vied12
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vied12/d5e94ab4abd8df799582 to your computer and use it in GitHub Desktop.
Save vied12/d5e94ab4abd8df799582 to your computer and use it in GitHub Desktop.
Download your Grooveshark library
#!/usr/bin/env python
# Encoding: utf-8
# -----------------------------------------------------------------------------
# Project : Grooveshark Library Downloader
# -----------------------------------------------------------------------------
# Author : Edouard Richard <edou4rd@gmail.com>
# -----------------------------------------------------------------------------
# License : GNU General Public License
# -----------------------------------------------------------------------------
# Creation : 04-May-2015
# Last mod : 04-May-2015
# -----------------------------------------------------------------------------
# ------ README: --------------------------------------------------------------
# 1. Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
# 2. download python dependencies:
# pip install google-api-python-client==1.4.0 youtube-dl==2015.05.04
# 3. Retrieve you library from the localstorage and save it as a json file
# Open grooveshark.com and copy/past in the console:
# var libraryKey = Object.keys(localStorage).filter(function(key) { return key.match(/library\d/) });
# var lib = localStorage[libraryKey];
# lib;
# (see http://www.reddit.com/r/grooveshark/comments/34iqpl/how_to_recover_your_library/)
# 4. and run this script:
# python grooveshark.py --f path/to/your/library_file.json
# -----------------------------------------------------------------------------
DEVELOPER_KEY = ""
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
from apiclient.discovery import build
from oauth2client.tools import argparser
import youtube_dl
import json
class Logger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
def progression_hook(d):
# pprint(d)
if d['status'] == 'finished':
print('Done downloading , now converting : %s' % (d['filename']))
def youtube_search(keyword):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
# Call the search.list method to retrieve results matching the specified
# query term.
search_response = youtube.search().list(
q=keyword,
part="id,snippet",
type='video',
maxResults=1
).execute()
result = search_response.get("items", [])
if result:
return result[0]["id"]["videoId"]
if __name__ == "__main__":
argparser.add_argument("--f", help="Grooveshark library json file")
args = argparser.parse_args()
data = json.load(open(args.f))
# get the first result from youtube
for key, song in data['songs'].items():
if not data['songs'][key].get('youtube'):
title = '%s %s' % (song['D'], song['J'])
youtube_id = youtube_search(title)
data['songs'][key]['youtube'] = youtube_id
# save the result in a file
with open('data_with_youtube_id.json', 'w') as f:
f.write(json.dumps(data))
# download and extract sound
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'logger': Logger(),
'progress_hooks': [progression_hook],
}
with open('data_with_youtube_id.json') as f:
data = json.load(open(args.f))
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=%s' % (song['youtube']) for song in data['songs'].values() if song['youtube']])
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment