Skip to content

Commit

Permalink
Merge pull request #3 from padenot/fixes
Browse files Browse the repository at this point in the history
A few fixes
  • Loading branch information
everdrone authored Jul 14, 2020
2 parents bbcf784 + b2067e1 commit e27fd4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
3 changes: 2 additions & 1 deletion bin/nts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
77 changes: 48 additions & 29 deletions nts/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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,
}


Expand Down Expand Up @@ -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
Expand All @@ -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__":
Expand Down

0 comments on commit e27fd4e

Please sign in to comment.