25개 이상의 토픽을 선택하실 수 없습니다.
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
54 lines
1.5 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_videos(query): |
|
""" Given a search query, return a list of video IDs """ |
|
# pylint:disable=no-member |
|
request = YTAPI.search().list( |
|
part="snippet", |
|
q=query, |
|
maxResults=100 |
|
) |
|
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"))
|
|
|