Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
80 righe
2.2 KiB
80 righe
2.2 KiB
""" Wrapper to the YouTube API """
|
|
|
|
import logging
|
|
|
|
import googleapiclient.discovery
|
|
import wordfilter
|
|
|
|
import config
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
YTAPI = googleapiclient.discovery.build('youtube', 'v3',
|
|
developerKey=config.API_KEY,
|
|
cache_discovery=False)
|
|
|
|
|
|
def get_region_codes():
|
|
""" get a list of all region codes """
|
|
# pylint:disable=no-member
|
|
request = YTAPI.i18nRegions().list(part="snippet")
|
|
response = request.execute()
|
|
return [item['id'] for item in response['items']]
|
|
|
|
|
|
def get_topic_id(region, name):
|
|
""" try to find the music category """
|
|
# pylint:disable=no-member
|
|
request = YTAPI.videoCategories().list(part="snippet", regionCode=region)
|
|
response = request.execute()
|
|
if 'items' not in response:
|
|
LOGGER.warning("No categories found")
|
|
return None
|
|
|
|
for item in response['items']:
|
|
if name in item['snippet']['title'].lower():
|
|
return item['id']
|
|
|
|
LOGGER.warning(f"No {name} category found")
|
|
return None
|
|
|
|
|
|
def get_videos(query, **kwargs):
|
|
""" Given a search query, return a list of video IDs """
|
|
# pylint:disable=no-member
|
|
request = YTAPI.search().list(
|
|
part="snippet",
|
|
q=query,
|
|
maxResults=100,
|
|
**kwargs
|
|
)
|
|
response = request.execute()
|
|
|
|
if 'items' not in response:
|
|
LOGGER.warning("No items found")
|
|
return []
|
|
|
|
wfilter = wordfilter.Wordfilter()
|
|
return [(item["id"]["videoId"], item['snippet'])
|
|
for item in response['items']
|
|
if 'id' in item
|
|
and 'videoId' in item['id']
|
|
and not wfilter.blacklisted(
|
|
item['snippet'].get('title', '')
|
|
+ item['snippet'].get('description', ''))]
|
|
|
|
|
|
def get_details(vid):
|
|
""" Given a video ID, get the content details """
|
|
# pylint:disable=no-member
|
|
request = YTAPI.videos().list(part="contentDetails",
|
|
id=vid)
|
|
response = request.execute()
|
|
for item in response['items']:
|
|
if item['id'] == vid:
|
|
return item['contentDetails']
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(get_videos("+lofi beats to chill and relax to"))
|
|
|