Skip to content

Commit

Permalink
Option to list category names
Browse files Browse the repository at this point in the history
  • Loading branch information
allejok96 committed Mar 21, 2021
1 parent 560f1a5 commit 839b1f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
22 changes: 15 additions & 7 deletions jwb-index
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ p.add_argument('--download-subtitles', action='store_true',
help='download VTT subtitle files')
p.add_argument('--exclude', metavar='CODE', dest='exclude_categories',
action=action_factory(lambda x: tuple(x.split(','))),
help='comma separated list of categories to exclude')
help='comma separated list of categories to skip (sub-categories will also be skipped)')
p.add_argument('--filter', '-f', dest='filter_categories', metavar='CODE',
action=action_factory(lambda x: tuple(x.split(','))),
help='comma separated list of categories to extract (use with --latest)')
p.add_argument('--fix-broken', action='store_true', dest='overwrite_bad',
help='check existing files and re-download them if they are broken')
p.add_argument('--forever', action='store_true', dest='stream_forever',
help='re-run program when the last video finishes')
p.add_argument('--free', type=int, metavar='MiB', dest='keep_free',
action=action_factory(lambda x: x * 1024 * 1024), # MiB to B
help='disk space in MiB to keep free (warning: deletes old MP4 files, use separate folder!)')
Expand All @@ -87,9 +85,11 @@ p.add_argument('--languages', '-L', nargs=0, action=action_factory(print_languag
help='display a list of valid language codes')
p.add_argument('--latest', action='store_const', const=['LatestVideos'],
dest='include_categories',
help='index the "Latest Videos" section')
help='index the "Latest Videos" category')
p.add_argument('--limit-rate', '-R', metavar='RATE', dest='rate_limit',
help='maximum download rate, passed to curl (default: 1m = 1 megabyte/s, 0 = no limit)')
p.add_argument('--list-categories', '-C', nargs='?', const=True, metavar='CODE', dest='print_category',
help='print a list of (sub) category names')
p.add_argument('--mode', '-m',
choices=['filesystem', 'html', 'html_tree', 'm3u', 'm3u_multi', 'm3u_tree', 'run', 'stdout', 'txt'],
help='output mode (see wiki)')
Expand All @@ -109,12 +109,20 @@ p.add_argument('--since', metavar='YYYY-MM-DD', dest='min_date',
help='only index media newer than this date')
p.add_argument('--sort',
choices=['newest', 'oldest', 'name', 'random'],
help='sort lines in output files')
help='sort output')
p.add_argument('positional_arguments', nargs='*', metavar='DIR|FILE|COMMAND',
help='where to send output (depends on mode)')

s = p.parse_args(namespace=Settings())

# Quick print of categories list
if s.print_category:
# Ugly: may be str even if it should be bool
if isinstance(s.print_category, str):
s.include_categories = s.print_category.split(',')
s.print_category = True
parse_broadcasting(s)
exit()
# Required arguments
if not (s.mode or s.download or s.download_subtitles or s.import_dir):
msg('please use --mode or --download')
Expand Down Expand Up @@ -142,7 +150,7 @@ elif len(s.positional_arguments) > 1:
msg('unexpected argument: {}'.format(s.positional_arguments[1]))
exit(1)

# Paths
# Check / set paths
if not os.path.isdir(s.work_dir):
msg('not a directory: ' + s.work_dir)
exit(1)
Expand All @@ -160,7 +168,7 @@ if s.import_dir:
exit()

# Some heads-up
if s.quiet < 1:
if s.quiet < 1 and not s.print_category:
if s.download and s.curl_path is not None and s.rate_limit != '0':
msg('note: download rate limit is active')
if not s.safe_filenames and (s.mode not in ('', 'stdout') or s.friendly_filenames):
Expand Down
1 change: 1 addition & 0 deletions jwlib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Settings:
include_categories = ('VideoOnDemand',)
exclude_categories = ('VODSJJMeetings',)
filter_categories = ()
print_category = False

# Disk space check stuff
keep_free = 0 # bytes
Expand Down
28 changes: 22 additions & 6 deletions jwlib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def parse_broadcasting(s: Settings):
queue = list(s.include_categories)
for key in queue:

url = 'https://data.jw-api.org/mediator/v1/categories/{L}/{c}?detailed=1&clientType=www'
url = url.format(L=s.lang, c=key)
url = 'https://data.jw-api.org/mediator/v1/categories/{L}/{c}?detailed={d}&clientType=www'
url = url.format(L=s.lang, c=key, d=int(not s.print_category))

try:
with urllib.request.urlopen(url) as j_raw: # j as JSON
Expand All @@ -156,9 +156,19 @@ def parse_broadcasting(s: Settings):
cat.home = cat.key in s.include_categories

if s.quiet < 1:
msg('indexing: {} ({})'.format(cat.key, cat.name))
if s.print_category:
if key == 'LatestVideos':
msg('Categories linked to the latest videos:')
else:
msg('Sub-categories of {}:'.format(key))
else:
msg('indexing: {} ({})'.format(cat.key, cat.name))

for j_sub in j['category'].get('subcategories', []):

if s.print_category:
print(j_sub['key'])
continue
sub = Category()
sub.key = j_sub['key']
sub.name = j_sub['name']
Expand All @@ -175,9 +185,15 @@ def parse_broadcasting(s: Settings):

for j_media in j['category'].get('media', []):
# Skip videos marked as hidden
if 'tags' in j['category']['media']:
if 'WebExclude' in j['category']['media']['tags']:
continue
if 'tags' in j_media.get('tags', []):
continue

if s.print_category and key == 'LatestVideos':
if s.quiet < 1:
print('{} -> {}'.format(j_media['primaryCategory'], j_media['title']))
else:
print(j_media['primaryCategory'])
continue

# Apply category filter
if s.filter_categories and j_media['primaryCategory'] not in s.filter_categories:
Expand Down

0 comments on commit 839b1f1

Please sign in to comment.