Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

41 rader
1.1 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. def get_videos(query):
  8. """ Given a search query, return a list of video IDs """
  9. youtube = googleapiclient.discovery.build('youtube', 'v3',
  10. developerKey=config.API_KEY,
  11. cache_discovery=False)
  12. request = youtube.search().list(
  13. part="snippet",
  14. q=query,
  15. maxResults=100
  16. )
  17. response = request.execute()
  18. if 'items' not in response:
  19. LOGGER.warning("No items found")
  20. return []
  21. wfilter = wordfilter.Wordfilter()
  22. return [item["id"]["videoId"]
  23. for item in response['items']
  24. if 'id' in item
  25. and 'videoId' in item['id']
  26. and not wfilter.blacklisted(
  27. item['snippet'].get('title', '')
  28. + item['snippet'].get('description', ''))]
  29. if __name__ == "__main__":
  30. print(get_videos("+lofi beats to chill and relax to"))