[bandcamp] alternative approach using JSON data elements parsing to fetch the data

This commit is contained in:
Gilou 2020-09-29 14:15:53 +02:00
parent 7117b849c1
commit 955906045f
1 changed files with 31 additions and 16 deletions

View File

@ -93,12 +93,30 @@ class BandcampIE(InfoExtractor):
track_number = None track_number = None
duration = None duration = None
formats = [] scriptdatablocks = re.findall(r'<script type="text/javascript" src="[^"]+" nonce="[^"]+" (?:data-[a-z-]+="[^"]+" ?)+>', webpage)
trackinfo_block = self._html_search_regex( bandcamp_data = {}
r'trackinfo(?:["\']|&quot;):\[\s*({.+?})\s*\],(?:["\']|&quot;)', for block in scriptdatablocks:
webpage, 'track info', default='{}') datablocks = re.findall(r'data-([a-z]+)="([^"]+)"', block)
for name, dblock in datablocks:
if name in ('tralbum', 'embed'):
data = self._parse_json(dblock, 'test', transform_source=unescapeHTML)
bandcamp_data[name] = data
else:
continue
for key, value in bandcamp_data.items():
print(key)
for ikey, ivalue in value.items():
if isinstance(ivalue, dict):
print('\t', ikey)
for iikey, iivalue in ivalue.items():
print('\t\t', iikey, iivalue)
else:
print('\t', ikey, ivalue)
formats = []
track_info = bandcamp_data['tralbum']['trackinfo'][0]
track_info = self._parse_json(trackinfo_block, title)
if track_info: if track_info:
file_ = track_info.get('file') file_ = track_info.get('file')
if isinstance(file_, dict): if isinstance(file_, dict):
@ -120,10 +138,11 @@ class BandcampIE(InfoExtractor):
duration = float_or_none(track_info.get('duration')) duration = float_or_none(track_info.get('duration'))
def extract(key): def extract(key):
data = self._html_search_regex( for values in bandcamp_data['tralbum']['current'], bandcamp_data['embed'], bandcamp_data['tralbum']:
r',(["\']|&quot;)%s\1:\1(?P<value>(?:\\\1|((?!\1).))+)\1' % key, if key in values and values[key]:
webpage, key, default=None, group='value') return values[key]
return data.replace(r'\"', '"').replace('\\\\', '\\') if data else data else:
return None
track = extract('title') track = extract('title')
artist = extract('artist') artist = extract('artist')
@ -132,14 +151,9 @@ class BandcampIE(InfoExtractor):
extract('publish_date') or extract('album_publish_date')) extract('publish_date') or extract('album_publish_date'))
release_date = unified_strdate(extract('album_release_date')) release_date = unified_strdate(extract('album_release_date'))
download_link = self._search_regex( download_link = bandcamp_data['tralbum'].get('freeDownloadPage')
r'freeDownloadPage(?:["\']|&quot;):\s*(["\']|&quot;)(?P<url>(?:(?!\1).)+)\1', webpage,
'download link', default=None, group='url')
if download_link: if download_link:
track_id = self._search_regex( print(download_link)
r'\?id=(?P<id>\d+)&',
download_link, 'track id')
download_webpage = self._download_webpage( download_webpage = self._download_webpage(
download_link, track_id, 'Downloading free downloads page') download_link, track_id, 'Downloading free downloads page')
@ -202,6 +216,7 @@ class BandcampIE(InfoExtractor):
self._sort_formats(formats) self._sort_formats(formats)
title = '%s - %s' % (artist, track) if artist else track title = '%s - %s' % (artist, track) if artist else track
print(title)
if not duration: if not duration:
duration = float_or_none(self._html_search_meta( duration = float_or_none(self._html_search_meta(