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.
 
 
 

33 lines
844 B

  1. """ Wrapper to the YouTube API """
  2. import googleapiclient.discovery
  3. import config
  4. def get_videos(query):
  5. """ Given a search query, return a list of video IDs """
  6. youtube = googleapiclient.discovery.build('youtube', 'v3',
  7. developerKey=config.API_KEY,
  8. cache_discovery=False)
  9. request = youtube.search().list(
  10. part="snippet",
  11. q=query,
  12. maxResults=100
  13. )
  14. response = request.execute()
  15. if 'items' not in response:
  16. LOGGER.warning("No items found")
  17. return []
  18. return [item["id"]["videoId"]
  19. for item in response['items']
  20. if 'id' in item
  21. and 'videoId' in item['id']]
  22. if __name__ == "__main__":
  23. print(get_videos("+lofi beats to chill and relax to"))