Commit f8041519 authored by Georg Teufelberger's avatar Georg Teufelberger
Browse files

Add possibility to pass ranges during episode selection

parent 56347217
......@@ -32,6 +32,26 @@ To see a list of possible arguments check
**For protected lectures** the vo-scraper will ask for your login credentials before downloading the video(s).
### Q: How can I choose which episodes of a lecture to download?
#### A: You will be prompted with the list of episodes available for downloading for each lecture.
You can either specify single episodes by typing their indices separated by space, or add ranges in Haskell syntax, like `1..5` for `1 2 3 4`.
Ranges are upper-bound-inclusive. Custom steps sizes are supported too, e.g. `1..3..10`
You may find this example of ranges useful:
| Range | Equivalent | In Words |
|-----------|-----------------|-------------------------------------------------------------------------------------------|
| `1..4` | `1 2 3 4` | Episode one to four |
| `..4` | `0 1 2 3 4` | All episodes up to four (the fifth) |
| `3..` | `3 4 5 6 [...]` | All episodes starting from three (the fourth) |
| `..` | `0 1 2 3 [...]` | All episodes |
| `2..4..6` | `2 4 6` | Every other episodes from two to six |
| `..2..6` | `0 2 4 6` | Every other episodes until six (when I started paying attention) |
| `1..3..` | `1 3 5 [...]` | Every other episodes starting from the second (i.e.. all the second episodes of the week) |
| `..3..` | `0 3 6 [...]` | Every third episodes, starting from the beginning |
### Q: How do I pass a file with links to multiple lectures?
#### A: Use `--file <filename>`
......
......@@ -241,6 +241,58 @@ def pretty_print_episodes(vo_json_data, selected):
str(episode['createdBy']).ljust(max_lecturer_length)
)
def make_range(item, max_episode_number):
"""
Keyword arguments:
item -- a string in the form of 'x..z' or 'x..y..z'
max_episode_number -- The highest episode number to have an upperbound for the range of episodes
Returns:
A range from x to z, with step size y, 1 if y wasn't provided
"""
if len(item.split('..')) == 2:
# user passed something like 'x..z', so step size is 1
lower_bound, upper_bound = item.split('..')
step = 1
else:
# user passed something like 'x..y..z', so step size is y
lower_bound, step, upper_bound = item.split('..')
# set the bounds to the outer limits if no number was passed
lower_bound = int(lower_bound) if lower_bound else 0
upper_bound = int(upper_bound) if upper_bound else max_episode_number
step = int(step)
return range(lower_bound, upper_bound+1, step)
def get_user_choice(max_episode_number):
"""
Prompts the user to pick multiple episodes and returns them
Keyword arguments:
max_episode_number -- The highest episode number to have an upperbound for the range of episodes
Returns:
A list containg the user picked choices
"""
# Prompt user
user_input = input(
"Enter numbers of the above lectures you want to download separated by space (e.g. 0 5 12 14)\nJust press enter if you don't want to download anything from this lecture\n"
).split()
choice = list()
for elem in user_input:
if elem.isnumeric():
choice.append(int(elem))
else:
choice += make_range(elem, max_episode_number)
# make elements of `choice` unique
choice = set(choice)
# sort them, to download in order and not randomly
choice = sorted(choice)
return choice
def vo_scrapper(vo_link, user, passw):
"""
......@@ -291,10 +343,8 @@ def vo_scrapper(vo_link, user, passw):
else:
# Let user pick videos
try:
choice = [int(x) for x in input(
"Enter numbers of the above lectures you want to download separated by space (e.g. 0 5 12 14)\nJust press enter if you don't want to download anything from this lecture\n"
).split()]
except:
choice = get_user_choice(max(range(len(vo_json_data['episodes']))))
except KeyboardInterrupt:
print()
print_information("Exiting...")
sys.exit()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment