Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions nts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import sys
from optparse import OptionParser
from nts import downloader as nts
import downloader as nts


def main():
Expand Down Expand Up @@ -40,6 +40,15 @@ def main():
dest="quiet",
action="store_true",
help="only print errors")

parser.add_option(
"-a",
"--albumize",
dest="albumize",
default=False,
action="store_true",
help="splits show based on track ids and time stamp and tags as an album"
)

(options, args) = parser.parse_args()

Expand All @@ -64,7 +73,8 @@ def url_matcher(url):
if match_ep:
nts.download(url=url,
quiet=options.quiet,
save_dir=download_dir)
save_dir=download_dir,
albumize=options.albumize)
elif match_sh:
episodes = nts.get_episodes_of_show(match_sh.group(1))
for ep in episodes:
Expand Down
85 changes: 81 additions & 4 deletions nts/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ffmpeg
import music_tag

__version__ = '1.3.8'
__version__ = '1.4.0'

# defaults to darwin
download_dir = '~/Downloads'
Expand Down Expand Up @@ -54,7 +54,7 @@ def mixcloud_try(parsed):
return resp['url']
return None

def download(url, quiet, save_dir, save=True):
def download(url, quiet, save_dir, albumize, save=True):
nts_url = url
page = requests.get(url).content
bs = BeautifulSoup(page, 'html.parser')
Expand Down Expand Up @@ -119,8 +119,10 @@ def download(url, quiet, save_dir, save=True):
ffmpeg.input(old_file_path).output(new_file_path, acodec='copy').run(overwrite_output=True)
os.remove(old_file_path)
file_ext = '.ogg'

set_metadata(os.path.join(save_dir, file), parsed, image, image_type)
if not albumize:
set_metadata(os.path.join(save_dir, file), parsed, image, image_type)
else:
set_metadata_album(save_dir, file, parsed, image, image_type)

return parsed

Expand Down Expand Up @@ -148,6 +150,10 @@ def parse_nts_data(bs, api_data):
tracks = parse_tracklist(api_data)

description = api_data.get('description', '')

timestamps = parse_timestamps(api_data)

show_alias = api_data.get('show_alias', '').replace('-',' ').title()

return {
'safe_title': safe_title,
Expand All @@ -160,6 +166,8 @@ def parse_nts_data(bs, api_data):
'tracks': tracks,
'image_url': image_url,
'description': description,
'timestamps': timestamps,
'show_alias':show_alias
}


Expand All @@ -169,6 +177,17 @@ def parse_tracklist(api_data):
tracks = map(lambda x: {'name': x.get('title', ''), 'artist': x.get('artist', '')}, tracks)
return list(tracks)

def parse_timestamps(api_data):
times = api_data.get('embeds', {}).get('tracklist', {}).get('results', [])
timestamps = []
for time in times:
temp = {'offset': time.get('offset', 0), 'duration': time.get('duration', 0)}
if temp['offset'] == None:
temp['offset'] = time.get('offset_estimate', 0)
if temp['duration'] == None:
temp['duration'] = time.get('duration_estimate', 0)
timestamps.append(temp)
return timestamps

def parse_artists(title, bs):
# parse artists in the title
Expand Down Expand Up @@ -241,6 +260,15 @@ def get_title(parsed):
def get_tracklist(parsed):
return '\n'.join(list(map(lambda x: f'{x["name"]} by {x["artist"]}', parsed['tracks'])))

def get_timestamps(parsed):
return parsed['timestamps']

def get_tracklist_nof(parsed):
return parsed['tracks']

def get_show(parsed):
return parsed['show_alias']

def get_date(parsed):
return f'{parsed["date"].date().isoformat()}'

Expand Down Expand Up @@ -283,6 +311,55 @@ def set_metadata(file_path, parsed, image, image_type):

f.save()

def set_metadata_album(save_dir, file, parsed, image, image_type):
apath = os.path.join(save_dir,parsed["safe_title"])
os.mkdir(apath)
file = os.path.join(save_dir,file)
ts = get_timestamps(parsed)
tracks = get_tracklist_nof(parsed)
num_tracks = len(tracks) + 2
tn_inc = 1
if ts[0]['offset'] != 0:
nfile = os.path.join(apath,"intro.ogg")
ffmpeg.input(file,ss=0,t=ts[0]['offset']).output(nfile, acodec='copy').run()
f = music_tag.load_file(nfile)
f['tracktitle'] = 'NTS Intro'
f['artwork'] = image
f['albumartist'] = get_show(parsed)
f['compilation'] = 1
f['album'] = get_title(parsed)
f['artist'] = 'NTS'
f.raw['year'] = get_date(parsed)
f['genre'] = get_genres(parsed)
f['comment'] = get_comment(parsed)
f['tracknumber'] = 1
f['totaltracks'] = num_tracks
f.save()
tn_inc += 1

for i in range(len(tracks)):
nfile = os.path.join(apath,unsafe_char(tracks[i]['name'])+".ogg")
if i == len(tracks)-1:
start=ts[i-1]['offset']+ts[i-1]['duration']
ffmpeg.input(file,ss=start).output(nfile, acodec='copy').run()
else:
ffmpeg.input(file,ss=ts[i]['offset'],t=ts[i]['duration']).output(nfile).run()
f = music_tag.load_file(nfile)
f['tracktitle'] = tracks[i]['name']
f['artwork'] = image
f['albumartist'] = get_show(parsed)
f['compilation'] = 1
f['album'] = get_title(parsed)
f['artist'] = tracks[i]['artist']
f.raw['year'] = get_date(parsed)
f['genre'] = get_genres(parsed)
f['comment'] = get_comment(parsed)
f['tracknumber'] = i+tn_inc
f['totaltracks'] = num_tracks
f.save()
print(get_artists(parsed))
os.remove(file)

def main():
episode_regex = r'.*nts\.live\/shows.+(\/episodes)\/.+'
show_regex = r'.*nts\.live\/shows\/([^/]+)$'
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Options:
~/Downloads on macOS and %USERPROFILE%\Downloads
-v, --version print the version number and quit
-q, --quiet only print errors
-a, --albumize splits show based on track ids and time stamp and tags as an album
ensure track ids and timestamps are on NTS
```

Just paste the episode url and it will be downloaded in your Downloads folder.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="nts-everdrone",
version="1.3.8",
version="1.4.0",
author="Giorgio Tropiano",
author_email="giorgiotropiano@gmail.com",
description="NTS Radio downloader tool",
Expand Down