video_service_thumbnail_url_generating_functions.py 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. """A function for each service to download the video thumbnail
  2. Each function should accept one argument (the video id from that service) and should return the download URL for the video's thumbnail.
  3. """
  4. """
  5. LIBRARIES
  6. """
  7. import urllib # For downloading the video thumbnails. Not as clean as, e.g., the requests module, but installed by default in many Python distributions.
  8. import json
  9. """
  10. END OF LIBRARIES
  11. """
  12. def generate_thumbnail_download_link_youtube(video_id_from_shortcode):
  13. """Thumbnail URL generator for YouTube videos."""
  14. thumbnail_download_link="https://img.youtube.com/vi/" + video_id_from_shortcode + "/0.jpg"
  15. return thumbnail_download_link
  16. def generate_thumbnail_download_link_vimeo(video_id_from_shortcode):
  17. """Thumbnail URL generator for Vimeo videos."""
  18. # Following the Vimeo API at https://developer.vimeo.com/api#video-request, we need to request the video's metadata and get the thumbnail from that. First, then, we'll get the metadata in JSON format, and then will parse it to find the thumbnail URL.
  19. video_metadata = urllib.urlopen("https://vimeo.com/api/v2/video/" + video_id_from_shortcode + ".json") # Download the video's metadata in JSON format.
  20. video_metadata_parsed = json.load(video_metadata) # Parse the JSON
  21. video_thumbnail_large_location = video_metadata_parsed[0]['thumbnail_large'] # Go into the JSON and get the URL of the thumbnail.
  22. return video_thumbnail_large_location