|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import re |
| 4 | +import sys |
| 5 | +from optparse import OptionParser |
| 6 | +from nts import downloader as nts |
| 7 | + |
| 8 | + |
| 9 | +def main(): |
| 10 | + episode_regex = r'.*nts\.live\/shows.+(\/episodes)\/.+' |
| 11 | + show_regex = r'.*nts\.live\/shows\/([^/]+)$' |
| 12 | + |
| 13 | + # defaults to darwin |
| 14 | + download_dir = '~/Downloads' |
| 15 | + if sys.platform.startswith('win32'): |
| 16 | + download_dir = '%USERPROFILE%\\Downloads\\' |
| 17 | + # expand it |
| 18 | + download_dir = os.path.expanduser('~/Downloads') |
| 19 | + |
| 20 | + usage = "Usage: %prog [options] args" |
| 21 | + parser = OptionParser(usage=usage) |
| 22 | + parser.add_option("-o", "--out-dir", dest="output_directory", default=download_dir, action="store", type="string", |
| 23 | + help="where the files will be downloaded, defaults to ~/Downloads on macOS and %USERPROFILE%\\Downloads", metavar="DIR") |
| 24 | + parser.add_option("-v", "--version", default=False, |
| 25 | + dest="version", action="store_true", |
| 26 | + help="print the version number and quit") |
| 27 | + parser.add_option("-q", "--quiet", default=False, |
| 28 | + dest="quiet", action="store_true", |
| 29 | + help="only print errors") |
| 30 | + |
| 31 | + (options, args) = parser.parse_args() |
| 32 | + |
| 33 | + if options.version: |
| 34 | + print(f'nts {nts.__version__}') |
| 35 | + exit(0) |
| 36 | + |
| 37 | + if len(args) < 1: |
| 38 | + print("please pass an URL or a file containing a list of urls.\n") |
| 39 | + parser.print_help() |
| 40 | + exit(1) |
| 41 | + |
| 42 | + download_dir = os.path.expanduser(options.output_directory) |
| 43 | + download_dir = os.path.abspath(options.output_directory) |
| 44 | + |
| 45 | + def url_matcher(url): |
| 46 | + if isinstance(url, str): |
| 47 | + url = url.strip() |
| 48 | + match_ep = re.match(episode_regex, url) |
| 49 | + match_sh = re.match(show_regex, url) |
| 50 | + |
| 51 | + if match_ep: |
| 52 | + nts.download(url=url, quiet=options.quiet, |
| 53 | + save_dir=download_dir) |
| 54 | + elif match_sh: |
| 55 | + episodes = nts.get_episodes_of_show(match_sh.group(1)) |
| 56 | + for ep in episodes: |
| 57 | + url_matcher(ep) |
| 58 | + else: |
| 59 | + print(f'{url} is not an NTS url.\n') |
| 60 | + parser.print_help() |
| 61 | + exit(1) |
| 62 | + |
| 63 | + for arg in args: |
| 64 | + if os.path.isfile(arg): |
| 65 | + # check if file |
| 66 | + file = "" |
| 67 | + with open(arg, 'r') as f: |
| 68 | + file = f.read() |
| 69 | + lines = filter(None, file.split('\n')) |
| 70 | + for line in lines: |
| 71 | + url_matcher(line) |
| 72 | + else: |
| 73 | + url_matcher(arg) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + main() |
0 commit comments