Skip to content

Instantly share code, notes, and snippets.

@fayesafe
Created July 5, 2016 11:08
Show Gist options
  • Save fayesafe/b2cbd8d814089be139f3658af6977eda to your computer and use it in GitHub Desktop.
Save fayesafe/b2cbd8d814089be139f3658af6977eda to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error, TIT2, TALB, TPE1, TRCK, TCO
import os
import re
import requests
import sys
import time
USER_AGENT = 'Mozilla/5.0 \
(Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0'
def main(album_url):
bc_session = requests.session()
headers = {'User-Agent': USER_AGENT}
main_page = bc_session.get(album_url)
pattern = 'var\ TralbumData\ =\ \{(.*\s*)*?\}\;'
regex = re.compile(pattern)
match = regex.search(main_page.text)
json_info = [
y for y in
[x.strip() for x in match.group().split(' = ')[1].split('\n')]
if not (y.startswith('//') or y.startswith('url'))
]
repair_pattern = '^(.*?):'
repair_regex = re.compile(repair_pattern)
json_in = '\n'.join([re.sub(r'\s":', r'":', re.sub(repair_regex, r'"\1":', x))
for x in json_info
])[:-1]
json_in = [y for y in [x for x in json_in.split('\n')] if not ',' in y or not '//' in y.split(',')[1]]
album_info = json.loads('\n'.join(json_in))
album_title = album_info['current']['title']
artist = album_info['artist']
tracks = album_info['trackinfo']
cover_art = album_info['artFullsizeUrl']
os.makedirs(album_title, exist_ok=True)
cover_req = bc_session.get(cover_art, stream=True)
with open(album_title + '/cover.jpg', 'wb') as cover_file:
for chunk in cover_req.iter_content(chunk_size=256):
cover_file.write(chunk)
for track in tracks:
file_name = '{}/{:02d}-{}.mp3'.format(
album_title, track['track_num'], track['title']
)
track_req = bc_session.get(
'http:{}'.format(track['file']['mp3-128']), stream=True)
with open(file_name, 'wb') as mp3_file:
for chunk in track_req.iter_content(chunk_size=1024):
mp3_file.write(chunk)
audio = MP3(file_name)
try:
audio.add_tags()
except error:
pass
audio.tags.add(
APIC(
encoding=3,
mime='image/jpeg',
type=3,
desc='Cover',
data=open(album_title + '/cover.jpg', 'rb').read()
)
)
audio.tags.add(
TIT2(
encoding=3, text=track['title']
)
)
audio.tags.add(
TALB(
encoding=3, text=album_title
)
)
audio.tags.add(
TPE1(
encoding=3, text=artist
)
)
audio.tags.add(
TRCK(
encoding=3, text=str(track['track_num'])
)
)
audio.tags.add(
TCO(
encoding=3, text=artist
)
)
audio.save()
time.sleep(3)
print('successfully downloaded {} by {}'.format(
track['title'], artist))
os.remove(album_title + '/cover.jpg')
if __name__ == '__main__':
album_url = sys.argv[1]
main(album_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment