From 6dba3d2b63d619ea2438c7a7b30a5a5adfc13d0b Mon Sep 17 00:00:00 2001 From: Subhamoy Biswas Date: Tue, 21 Jan 2025 20:40:34 +0530 Subject: [PATCH] (refactor): improved default caption input validation and minor docs update --- README.md | 4 ++-- pytubepp/main.py | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index be9b1ff..48c1af6 100644 --- a/README.md +++ b/README.md @@ -111,13 +111,13 @@ pytubepp "https://youtube.com/watch?v=2lAe1cqCOXo" -i | Flag | Usage | Requires Parameter | Requires URL | Parameters | Default | | :--- | :--- | :--- | :--- | :--- | :--- | | -s | Choose preferred download stream | YES | YES | `144` `144p` `240` `240p` `360` `360p` `480` `480p` `720` `720p` `hd` `1080` `1080p` `fhd` `1440` `1440p` `2k` `2160` `2160p` `4k` `4320` `4320p` `8k` `mp3` (Pass any one of them) | Your chosen Default Stream via `-ds` flag | -| -c | Choose preferred caption | YES | YES | All [ISO 639-1 Language Codes](https://www.w3schools.com/tags/ref_language_codes.asp) + some others (Pass any one of them) + `none` for No Caption eg: `en` for English | Your chosen Default Caption via `-dc` flag | +| -c | Choose preferred caption | YES | YES | All [ISO 639-1 Language Codes](https://www.w3schools.com/tags/ref_language_codes.asp) + auto generated ones + `none` for No Caption (Pass any one of them) eg: `en` for English | Your chosen Default Caption via `-dc` flag | | -i | Shows the video information like: Title, Author, Views, Publication Date, Duration, Available Download Streams | NO | YES | No parameters | No default | | -ls | Lists all available streams (video, audio, caption) (only for debuging purposes) | NO | YES | No parameters | No default | | -ri | Shows the video information in raw json format | NO | YES | No parameters | No default | | -jp | Shows raw json output in prettified view (with indentation: 4) (primarily used with -ri flag)| NO | YES | No parameters | No default | | -ds | Set default download stream | YES | NO | `144p` `240p` `360p` `480p` `720p` `1080p` `1440p` `2160p` `4320p` `mp3` `max` (Pass any one of them) | `max` | -| -dc | Set default caption | YES | NO | All [ISO 639-1 Language Codes](https://www.w3schools.com/tags/ref_language_codes.asp) + some others + `none` for No Caption (Pass any one of them) eg: `en` for English | `none` | +| -dc | Set default caption | YES | NO | All [ISO 639-1 Language Codes](https://www.w3schools.com/tags/ref_language_codes.asp) + auto generated ones + `none` for No Caption (Pass any one of them) eg: `en` for English | `none` | | -df | Set custom download folder path | YES | NO | Use the full path excluding the last trailing slash within double quotes eg(in Linux): `"/path/to/folder"` (Make sure the folder path you enterted is already created and accessable) | Within `PytubePP Downloads` folder in your System's `Downloads` folder | | -r | Reset to default configuration (Download Folder, Default Stream) | NO | NO | No parameters | No default | | -sc | Show all current user configurations | NO | NO | No parameters | No default | diff --git a/pytubepp/main.py b/pytubepp/main.py index d3cb583..7995d6e 100644 --- a/pytubepp/main.py +++ b/pytubepp/main.py @@ -593,8 +593,13 @@ def main(): if hasattr(args, 'default_caption'): if args.default_caption != downloader.default_caption: - if not all(c.isalpha() or c in '.-' for c in args.default_caption) or len(args.default_caption) > 10: - print('\nInvalid caption code! Only a-z, A-Z, dash (-) and dot (.) are allowed with maximum 10 characters...!!') + if not (re.match(r'^[a-z]{2}(-[A-Za-z]+)?$', args.default_caption) or + re.match(r'^a\.[a-z]{2}(-[A-Za-z]+)?$', args.default_caption) or + re.match(r'^none$', args.default_caption)): + print('\nInvalid caption code! Allowed formats are:\n' + '- ISO 639-1 language codes (e.g: en, zh-Hans)\n' + '- Auto-generated variants: a.ISO639-1LanguageCode (e.g: a.en, a.zh-Hans)\n' + '- none\n') else: update_config('defaultCaption', args.default_caption) print(f'\nDefault caption updated to: {args.default_caption}')