Created
November 29, 2015 13:00
-
-
Save adityatj/1f4dfdc1dbdc474d3aa3 to your computer and use it in GitHub Desktop.
A simple album downloader script for Raaga
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#raaga downloader | |
import requests | |
import json | |
import os | |
import sys | |
__author__ = 'adityatj (GitHub)' | |
__version__ = '0.1' | |
ALBUM_TRACKS_URL = 'http://play.raaga.com/album_tracks_json.php?aid=%s' | |
ALTERNATE_TRACKS_URL = 'http://www.raaga.com/json/facade.asp?svc=playhttp&id=501799,502315,502653' | |
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36w'} | |
DIR_CREATED = False | |
COVER_DOWNLOADED = False | |
my_print = sys.stdout.write | |
def create_folder(album_name): | |
try: | |
if not os.path.exists(album_name): | |
os.makedirs(album_name) | |
return True | |
except: | |
return False | |
def download_remote_file(path, file_url, file_name): | |
my_print('Dowloading %s...' % file_name) | |
r = requests.get(file_url, headers=HEADERS) | |
if r.status_code == 200: | |
with open(path + '/' + file_name, 'wb') as f: | |
for chunk in r.iter_content(2048): | |
f.write(chunk) | |
my_print('Done\n') | |
else: | |
my_print('Failed\n') | |
def kickstart(album_id): | |
r = requests.get(ALBUM_TRACKS_URL % album_id, headers=HEADERS) | |
if r.status_code == 200: | |
content = r.text.strip() | |
#success | |
if content != '()': | |
my_print('\n') | |
data = json.loads(content[1:-1]) | |
if len(data['playlist']) > 0: | |
create_folder(data['playlist'][0]['album']) | |
download_remote_file(data['playlist'][0]['album'], data['playlist'][0]['cd_img'], 'cover.jpg') | |
track_num = 1 | |
for song in data['playlist']: | |
download_remote_file(song['album'], song['url'], song['song_title'] + '.m4a') | |
else: | |
print 'Invalid album code! Please re-check mate.' | |
else: | |
#failure | |
print 'HTTP Code: %d. Go figure whatever you can. <3' % r.status_code | |
#main | |
if len(sys.argv) != 3 or sys.argv[1] != '-url': | |
print 'Usage: raaga.py -url <url>' | |
exit(0) | |
else: | |
url = sys.argv[2] | |
if '-' in url: | |
kickstart(url.split('-')[-1]) | |
else: | |
print 'Invalid album code! Please re-check mate.' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment