You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

55 lines
1.5 KiB

  1. """ Wrapper to the YouTube API """
  2. import logging
  3. import googleapiclient.discovery
  4. import wordfilter
  5. import config
  6. LOGGER = logging.getLogger(__name__)
  7. YTAPI = googleapiclient.discovery.build('youtube', 'v3',
  8. developerKey=config.API_KEY,
  9. cache_discovery=False)
  10. def get_videos(query):
  11. """ Given a search query, return a list of video IDs """
  12. # pylint:disable=no-member
  13. request = YTAPI.search().list(
  14. part="snippet",
  15. q=query,
  16. maxResults=100
  17. )
  18. response = request.execute()
  19. if 'items' not in response:
  20. LOGGER.warning("No items found")
  21. return []
  22. wfilter = wordfilter.Wordfilter()
  23. return [(item["id"]["videoId"], item['snippet'])
  24. for item in response['items']
  25. if 'id' in item
  26. and 'videoId' in item['id']
  27. and not wfilter.blacklisted(
  28. item['snippet'].get('title', '')
  29. + item['snippet'].get('description', ''))]
  30. def get_details(vid):
  31. """ Given a video ID, get the content details """
  32. # pylint:disable=no-member
  33. request = YTAPI.videos().list(part="contentDetails",
  34. id=vid)
  35. response = request.execute()
  36. for item in response['items']:
  37. if item['id'] == vid:
  38. return item['contentDetails']
  39. return None
  40. if __name__ == "__main__":
  41. print(get_videos("+lofi beats to chill and relax to"))