From 51997c1b7fe883c492fd35492f1198f6d5dba61b Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 10 Apr 2016 14:24:15 -0400 Subject: [PATCH] support for non-album tracks --- fetchcose.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/fetchcose.py b/fetchcose.py index 7371f67..8c9708b 100644 --- a/fetchcose.py +++ b/fetchcose.py @@ -8,6 +8,7 @@ import json import multiprocessing from splinter import Browser +from splinter.exceptions import ElementDoesNotExist log = logging.getLogger(__name__) @@ -16,17 +17,19 @@ def visit_track(b, url): log.debug('Visiting track %s' % url) b.visit(url) b.find_by_css('.playbutton').first.click() - artist = b.find_by_xpath('//span[@itemprop="byArtist"]').first.value - album = b.find_by_xpath('//span[@itemprop="inAlbum"]').first.value - title = b.find_by_css('#name-section h2.trackTitle').first.value + info = {} + info['artist'] = b.find_by_xpath('//span[@itemprop="byArtist"]') \ + .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! b.find_by_css('.playbutton').first.click() - return dict( - artist=artist, - album=album, - title=title, - url=b.find_by_css('audio').first['src'] - ) + info['url'] = b.find_by_css('audio').first['src'] + return info def visit_album(b, url): @@ -41,7 +44,10 @@ def visit_album(b, url): def download_track(track): 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' fpath = join(args.download_base, fdir, fname) 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['title'] = track['title'] audio['artist'] = track['artist'] - audio['album'] = track['album'] + if 'album' in track: + audio['album'] = track['album'] audio.save() log.info('track %(title)s downloaded' % track) return fpath