From de60bf8dd1c36d3a2c3ce3669c75e501324c263e Mon Sep 17 00:00:00 2001 From: neodarz Date: Mon, 5 Aug 2019 10:08:17 +0200 Subject: Add more sanity check --- artetv_dl/__main__.py | 28 +++++++++++++++++++++------- requirements.txt | 1 + 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/artetv_dl/__main__.py b/artetv_dl/__main__.py index 58ce3d9..f6897c8 100755 --- a/artetv_dl/__main__.py +++ b/artetv_dl/__main__.py @@ -5,13 +5,27 @@ from bs4 import BeautifulSoup from urllib.request import urlopen, unquote, urlretrieve import re, json, sys +import validators + +def show_help(msg=None): + if msg: + print(msg) + print("Usage:") + print(" "+sys.argv[0]+" ") + print("Example:") + print(" "+sys.argv[0]+" https://www.arte.tv/fr/videos/051868-000-A/liberte-egalite-indemnites-vers-un-revenu-universel/") + sys.exit(1) def download(url): content = urlopen(url) json_data = json.loads(content.read().decode()) name = json_data['videoJsonPlayer']['VTI'] - url = json_data['videoJsonPlayer']['VSR']['HTTPS_SQ_1']['url'] + try: + url = json_data['videoJsonPlayer']['VSR']['HTTPS_SQ_1']['url'] + except TypeError: + print("Sorry, it seems that the video is no longer available.") + sys.exit(0) name=name.replace('/', '-')+".mp4" try: @@ -24,14 +38,13 @@ def download(url): def main(): if len(sys.argv) != 2: - print("Usage:") - print(" "+sys.argv[0]+" ") - print("Example:") - print(" "+sys.argv[0]+" https://www.arte.tv/fr/videos/051868-000-A/liberte-egalite-indemnites-vers-un-revenu-universel/") - sys.exit(0) + show_help() url = sys.argv[1] + if not validators.url(url): + show_help("Error: This is not an url or maybe you forgot http(s):// before domain?") + programId = re.findall('[0-9A-Z]{6}-[0-9A-Z]{3}-[0-9A-Z]', url) collection = re.findall('[0-9A-Z]{2}-[0-9A-Z]{6}', url) @@ -62,7 +75,8 @@ def main(): download(url) else: - print("This is not a valid url") + print("Error: Nothing found here, are your sure your are on arte.tv website and it's a valid video url?") + show_help() if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index cd91942..8175f34 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ beautifulsoup4==4.6.0 +validators==0.13.0 -- cgit v1.2.1