Last active
July 14, 2023 15:00
-
-
Save WingTillDie/80e95ec777efb9f95731ef102271f128 to your computer and use it in GitHub Desktop.
Get YouTube Thumbnail URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from enum import Enum | |
from IPython import get_ipython | |
URL_TYPE = Enum('URL_TYPE', ['unsupported', 'long_url', 'short_url', 'shorts', 'music']) | |
def determine_url_type(url: str): | |
if url[:29] == 'https://www.youtube.com/watch': | |
url_type = URL_TYPE.long_url | |
elif url[:17] == 'https://youtu.be/': | |
url_type = URL_TYPE.short_url | |
elif url[:31] == 'https://www.youtube.com/shorts/': | |
url_type = URL_TYPE.shorts | |
elif url[:31] == 'https://music.youtube.com/watch': | |
url_type = URL_TYPE.music | |
else: | |
url_type = URL_TYPE.unsupported | |
return url_type | |
def get_url_with_args(url: str): | |
url_without_args, url_args = url.split('?') | |
url_args = url_args.split('&') | |
url_args_dict = {} | |
for url_arg in url_args: | |
key, value = url_arg.split('=') | |
url_args_dict[key] = value | |
return url_without_args, url_args_dict | |
def get_video_id(url: str): | |
url_without_args, url_args_dict = get_url_with_args(url) | |
url_type = determine_url_type(url) | |
if url_type == URL_TYPE.long_url or url_type == URL_TYPE.music: | |
video_id = url_args_dict['v'] | |
elif url_type == URL_TYPE.short_url or url_type == URL_TYPE.shorts: | |
video_id = url_without_args.split('/')[-1] | |
else: | |
raise Exception('Unsupported URL, URL should start with:\nhttps://www.youube.com/watch') | |
return video_id | |
def get_thumbnail_url(url: str): | |
video_id = get_video_id(url) | |
return f'https://i.ytimg.com/vi_webp/{video_id}/maxresdefault.webp' | |
def test_get_thumbnail_url(): | |
''' | |
Outputs: | |
URL_TYPE.long_url https://i.ytimg.com/vi_webp/dfl9KIX1WpU/maxresdefault.webp | |
URL_TYPE.short_url https://i.ytimg.com/vi_webp/dfl9KIX1WpU/maxresdefault.webp | |
URL_TYPE.shorts https://i.ytimg.com/vi_webp/ANXvsktKf78/maxresdefault.webp | |
URL_TYPE.music https://i.ytimg.com/vi_webp/JQGRg8XBnB4/maxresdefault.webp | |
''' | |
test_urls = [ | |
'https://www.youtube.com/watch?v=dfl9KIX1WpU&t=30', | |
'https://youtu.be/dfl9KIX1WpU?t=30', | |
'https://www.youtube.com/shorts/ANXvsktKf78?app=desktop', | |
'https://music.youtube.com/watch?v=JQGRg8XBnB4', | |
] | |
for test_url in test_urls: | |
print(determine_url_type(test_url), get_thumbnail_url(test_url)) | |
def main_get_thumbnail_url(url): | |
''' | |
url: YouTube URL , e.g. | |
https://www.youtube.com/watch?v=dfl9KIX1WpU&t=30 | |
https://youtu.be/dfl9KIX1WpU?t=30 | |
https://www.youtube.com/shorts/ANXvsktKf78?app=desktop | |
''' | |
try: | |
thunmbnail_url = get_thumbnail_url(url) | |
except Exception as e: | |
print(*e.args, file=sys.stderr) | |
else: | |
print(get_thumbnail_url(url)) | |
def get_non_whitespace_char_index(str_: str) -> int: | |
import itertools | |
whitespace_chars = {' ', '\t'} | |
for char, index in zip(str_, itertools.count()): | |
if char not in whitespace_chars: | |
return index | |
break | |
return len(str_) | |
def remove_whitespace_prefix(str_: str) -> str: | |
index = get_non_whitespace_char_index(str_) | |
return str_[index:] | |
def remove_indent(str_multiline: str) -> str: | |
strs = str_multiline.splitlines() | |
ret_strs = [] | |
for str_ in strs: | |
ret_strs.append(remove_whitespace_prefix(str_)) | |
return '\n'.join(ret_strs) | |
# Remove indent of usage | |
def remove_indent_and_strip_newline(str_multiline: str) -> str: | |
str_multiline = remove_indent(str_multiline) | |
str_multiline = str_multiline.strip('\n') | |
return str_multiline | |
def usage(): | |
print(remove_indent_and_strip_newline(''' | |
Usage: | |
./youtube_thumbnail.py <YouTube video URL> | |
e.g. | |
./youtube_thumbnail.py https://youtu.be/dfl9KIX1WpU?t=30 | |
Prints YouTube thumbnail URL | |
''')) | |
def main(args): | |
if len(args) != 2: | |
usage() | |
elif(args[1] == '-h' or args[1] == '--help'): | |
usage() | |
elif(args[1] == '-t' or args[1] == '--test'): | |
test_get_thumbnail_url() | |
else: | |
url = args[1] | |
main_get_thumbnail_url(url) | |
# BETTER return None? | |
if __name__ == '__main__': | |
def is_ipython(): | |
return get_ipython() is not None | |
# TODO test | |
if is_ipython(): | |
args = [ | |
'./youtube_thumbnail.py', | |
'https://youtu.be/dfl9KIX1WpU?t=30' | |
] | |
else: | |
args = sys.argv | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment