video_service_thumbnail_url_generating_functions.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  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. # from urllib.request import urlopen # For downloading the video thumbnails. Not as clean as, e.g., the requests module, but installed by default in many Python distributions.
  8. from six.moves.urllib.request import urlopen
  9. import json
  10. """
  11. END OF LIBRARIES
  12. """
  13. def generate_thumbnail_download_link_youtube(video_id_from_shortcode):
  14. """Thumbnail URL generator for YouTube videos."""
  15. thumbnail_download_link="https://img.youtube.com/vi/" + video_id_from_shortcode + "/0.jpg"
  16. return thumbnail_download_link
  17. def generate_thumbnail_download_link_vimeo(video_id_from_shortcode):
  18. """Thumbnail URL generator for Vimeo videos."""
  19. # 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.
  20. video_metadata = urlopen("https://vimeo.com/api/v2/video/" + str(video_id_from_shortcode) + ".json").read() # Download the video's metadata in JSON format.
  21. video_metadata_parsed = json.loads(video_metadata.decode('utf-8')) # Parse the JSON
  22. video_thumbnail_large_location = video_metadata_parsed[0]['thumbnail_large'] # Go into the JSON and get the URL of the thumbnail.
  23. return video_thumbnail_large_location