Get information about a YouTube video

This commit is contained in:
Christian Colglazier 2021-06-28 15:10:02 -04:00
parent be3a4240d0
commit f97e6b5b1c
2 changed files with 27 additions and 1 deletions

View File

@ -8,3 +8,28 @@ class YouTubeAPI(object):
def __init__(self, key):
self.url = 'https://www.googleapis.com/youtube/v3/'
self.key = key
# Sends get requests and turns requested data.
def getRequest(self, domain):
response = get('{}{}&key={}'.format(self.url,
domain,
self.key))
return json.loads(response.text)
def getVideo(self, part, id):
return self.getRequest('videos?part={}&id={}'.format(part, id))
def getVideoInfo(self, tag):
snippet = self.getVideo('snippet', tag)
status = self.getVideo('status', tag)
snippet = snippet['items'][0]['snippet']
status = status['items'][0]['status']
title = snippet['title']
date = snippet['publishedAt']
channel = snippet['channelTitle']
description = snippet['description']
tags = snippet['tags']
privacy = status['privacyStatus']
return title, date, channel, description, tags, privacy

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python3
from youtube_api import YouTubeAPI
import sys
# Read YouTube API Key
def getYouTubeAPIKey():
return open('youtube.key', 'r').read()
api = YouTubeAPI(getYouTubeAPIKey())
print(getYouTubeAPIKey())
print(api.getVideoInfo(sys.argv[1]))