support for non-album tracks

This commit is contained in:
boyska 2016-04-10 14:24:15 -04:00
parent 600faed824
commit 51997c1b7f

View file

@ -8,6 +8,7 @@ import json
import multiprocessing import multiprocessing
from splinter import Browser from splinter import Browser
from splinter.exceptions import ElementDoesNotExist
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -16,17 +17,19 @@ def visit_track(b, url):
log.debug('Visiting track %s' % url) log.debug('Visiting track %s' % url)
b.visit(url) b.visit(url)
b.find_by_css('.playbutton').first.click() b.find_by_css('.playbutton').first.click()
artist = b.find_by_xpath('//span[@itemprop="byArtist"]').first.value info = {}
album = b.find_by_xpath('//span[@itemprop="inAlbum"]').first.value info['artist'] = b.find_by_xpath('//span[@itemprop="byArtist"]') \
title = b.find_by_css('#name-section h2.trackTitle').first.value .first.value
try:
info['album'] = b.find_by_xpath('//span[@itemprop="inAlbum"]') \
.first.value
except ElementDoesNotExist: # non-album tracks
pass
info['title'] = b.find_by_css('#name-section h2.trackTitle').first.value
time.sleep(0.2) # how nice! time.sleep(0.2) # how nice!
b.find_by_css('.playbutton').first.click() b.find_by_css('.playbutton').first.click()
return dict( info['url'] = b.find_by_css('audio').first['src']
artist=artist, return info
album=album,
title=title,
url=b.find_by_css('audio').first['src']
)
def visit_album(b, url): def visit_album(b, url):
@ -41,7 +44,10 @@ def visit_album(b, url):
def download_track(track): def download_track(track):
log.debug('Downloading "%s"' % track['title']) log.debug('Downloading "%s"' % track['title'])
fdir = join(track['artist'], track['album']).replace(' ', '_') if 'album' in track:
fdir = join(track['artist'], track['album']).replace(' ', '_')
else:
fdir = join(track['artist'])
fname = track['title'].replace(' ', '_') + '.mp3' fname = track['title'].replace(' ', '_') + '.mp3'
fpath = join(args.download_base, fdir, fname) fpath = join(args.download_base, fdir, fname)
os.makedirs(join(args.download_base, fdir), exist_ok=True) os.makedirs(join(args.download_base, fdir), exist_ok=True)
@ -59,7 +65,8 @@ def download_track(track):
audio = mutagen.mp3.EasyMP3(fpath) # TODO: is it always mp3? audio = mutagen.mp3.EasyMP3(fpath) # TODO: is it always mp3?
audio['title'] = track['title'] audio['title'] = track['title']
audio['artist'] = track['artist'] audio['artist'] = track['artist']
audio['album'] = track['album'] if 'album' in track:
audio['album'] = track['album']
audio.save() audio.save()
log.info('track %(title)s downloaded' % track) log.info('track %(title)s downloaded' % track)
return fpath return fpath