diff --git a/bin/nts b/bin/nts index a872666..babf47d 100755 --- a/bin/nts +++ b/bin/nts @@ -54,7 +54,8 @@ def url_matcher(url): nts.download(url=url, quiet=options.quiet, save_dir=download_dir) elif match_sh: episodes = nts.get_episodes_of_show(match_sh.group(1)) - url_matcher(episodes) + for ep in episodes: + url_matcher(ep) else: print(f'{url} is not an NTS url.\n') parser.print_help() diff --git a/nts/downloader.py b/nts/downloader.py index 7c62818..9668599 100644 --- a/nts/downloader.py +++ b/nts/downloader.py @@ -29,25 +29,31 @@ def download(url, quiet, save_dir, save=True): # guessing there is one parsed = parse_nts_data(bs) - # safe_title, date, title, artists, parsed_artists, genres = parse_nts_data(bs) + # safe_title, date, title, artists, parsed_artists, genres, image_url = parse_nts_data(bs) button = bs.select('.episode__btn.mixcloud-btn')[0] link = button.get('data-src') match = re.match(r'https:\/\/www.mixcloud\.com\/NTSRadio.+$', link) - # get album art + # get album art. If the one on mixcloud is available, use it. Otherwise, + # fall back to the nts website. page = requests.get(link).content bs = BeautifulSoup(page, 'html.parser') - img = bs.select('div.album-art')[0].img - srcset = img.get('srcset').split() - img = srcset[-2].split(',')[1] + image_type = '' + if len(bs.select('div.album-art')) != 0: + img = bs.select('div.album-art')[0].img + srcset = img.get('srcset').split() + img = srcset[-2].split(',')[1] + image = urllib.request.urlopen(img) + image_type = image.info().get_content_type() + image = image.read() + elif len(parsed["image_url"]): + image = urllib.request.urlopen(parsed["image_url"]) + image_type = image.info().get_content_type() + image = image.read() file_name = f'{parsed["safe_title"]} - {parsed["date"].year}-{parsed["date"].month}-{parsed["date"].day}' - image = urllib.request.urlopen(img) - image_type = image.info().get_content_type() - image = image.read() - # download if save: if not quiet: @@ -91,16 +97,18 @@ def download(url, quiet, save_dir, save=True): # comment audio['\xa9cmt'] = nts_url # genre - audio['\xa9gen'] = parsed['genres'][0] + if len(parsed['genres']) != 0: + audio['\xa9gen'] = parsed['genres'][0] # cover - match = re.match(r'jpe?g$', image_type) - img_format = None - if match: - img_format = mutagen.mp4.AtomDataType.JPEG - else: - img_format = mutagen.mp4.AtomDataType.PNG - cover = mutagen.mp4.MP4Cover(image, img_format) - audio['covr'] = [cover] + if image_type != '': + match = re.match(r'jpe?g$', image_type) + img_format = None + if match: + img_format = mutagen.mp4.AtomDataType.JPEG + else: + img_format = mutagen.mp4.AtomDataType.PNG + cover = mutagen.mp4.MP4Cover(image, img_format) + audio['covr'] = [cover] audio.save() return parsed @@ -117,6 +125,10 @@ def parse_nts_data(bs): station = title_box.div.div.h2.find(text=True, recursive=False).strip() + bg_tag = bs.select('section#bg[style]') + background_image_regex = r'background-image:url\((.*)\)' + image_url = re.match(background_image_regex, bg_tag[0]['style']).groups()[0] + # sometimes it's just the date date = title_box.div.div.h2.span.text if ',' in date: @@ -138,7 +150,8 @@ def parse_nts_data(bs): 'parsed_artists': parsed_artists, 'genres': genres, 'station': station, - 'tracks': tracks + 'tracks': tracks, + 'image_url': image_url, } @@ -241,7 +254,17 @@ def main(): exit(1) arg = sys.argv[1] - lines = [arg] + line = arg + + match_episode = re.match(episode_regex, line) + match_show = re.match(show_regex, line) + + lines = [] + + if match_episode: + lines += line.strip() + elif match_show: + lines += get_episodes_of_show(match_show.group(1)) if os.path.isfile(arg): # read list @@ -250,16 +273,12 @@ def main(): file = f.read() lines = filter(None, file.split('\n')) + if len(lines) == 0: + print('Didn\'t find shows to download.') + exit (1) + for line in lines: - match_episode = re.match(episode_regex, line) - match_show = re.match(show_regex, line) - if match_episode: - download(line.strip()) - elif match_show: - lines += get_episodes_of_show(match_show.group(1)) - else: - print(f'{line} is not an NTS url.') - exit(1) + download(line, False, download_dir) if __name__ == "__main__":