support for non-album tracks
This commit is contained in:
parent
600faed824
commit
51997c1b7f
1 changed files with 18 additions and 11 deletions
29
fetchcose.py
29
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
|
||||
|
|
Loading…
Reference in a new issue