-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYT2TVDB.py
67 lines (50 loc) · 1.81 KB
/
YT2TVDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! python3
import re
import cloudconvert
def get_thumbnail_url(youtube_url, thumbnail_res):
youtube_url = youtube_url.rstrip()
youtube_id = ""
# Get YouTube video ID
search_pattern = re.search("(?:\/|%3D|v=|vi=)([0-9A-z-_]{11})(?:[%#?&]|$)", youtube_url)
if search_pattern:
youtube_id = search_pattern.group(1)
youtube_thumbnail_url = f"https://i.ytimg.com/vi/{youtube_id}/{thumbnail_res}default.jpg"
print(f"\nVideo's max resolution thumbnail: {youtube_thumbnail_url}")
return youtube_thumbnail_url
def download_thumbnail(youtube_thumbnail_url):
# Get the API key from the API_KEY file
with open("API_KEY", "r") as file:
API_KEY = file.read()
# All of cloudconvert's processes and conversions
print("Creating process...")
api = cloudconvert.Api(API_KEY)
process = api.createProcess({
"inputformat": "jpg",
"outputformat": "jpg"
})
print("Converting image...")
process.start({
"input": "download",
"file": youtube_thumbnail_url,
"outputformat": "jpg",
"preset": "AduhH6JcFl",
"wait": True
})
process.refresh()
process.wait()
print(process["message"])
print("Downloading image to the same directory as the YT2TVDB.py file...")
process.download()
print("Download finished!")
def main():
youtube_url = input("Type or paste the YouTube link: ")
try:
thumbnail_url = get_thumbnail_url(youtube_url, "maxres")
download_thumbnail(thumbnail_url)
except:
print("Max resolution failed, retrying with a lower resolution")
thumbnail_url = get_thumbnail_url(youtube_url, "mq")
download_thumbnail(thumbnail_url)
input("\nPress the return key to exit") # Prevents CMD from auto-exiting
if __name__ == '__main__':
main()