-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_video_title.py
36 lines (27 loc) · 935 Bytes
/
get_video_title.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 14 11:31:06 2022
@author: AllenKll
"""
"""
Must create a local 'config.py' with your google API key in it:
api_key = "YOUR_API_KEY"
"""
import requests
import config
def get_video_title(video_id):
try:
r = requests.get(f"https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&q={video_id}&key={config.api_key}",timeout=5)
except:
print("Encountered an error with youtube.")
return "UNKNOWN - Youtube connection porblem"
if r.status_code != 200:
return f"UNKNOWN - Youtube Error {r.status_code}"
items = r.json()["items"]
for item in items:
if item["id"]["kind"] != "youtube#video": continue
if item["id"]["videoId"]== video_id:
return(item["snippet"]["title"])
return "UNKNOWN - Possibly Unlisted"
if __name__ == "__main__":
print(get_video_title("dQw4w9WgXcQ"))