Skip to content

Instantly share code, notes, and snippets.

@rectifyer
Created October 11, 2016 17:38
Show Gist options
  • Save rectifyer/c93f5e9e9b661d5203c6ba1ae7a2663a to your computer and use it in GitHub Desktop.
Save rectifyer/c93f5e9e9b661d5203c6ba1ae7a2663a to your computer and use it in GitHub Desktop.
Get images from TMDB
require 'httparty'
# TMDB
TMDB_API_KEY = '[your api key]'
# get configuration parameters
response = HTTParty.get("https://api.themoviedb.org/3/configuration?api_key=#{TMDB_API_KEY}")
config = JSON.parse(response.body)
image_prefix = config['images']['secure_base_url']
# --------------------
# movie images
tmdb_id = 20526 # Tron: Legacy
response = HTTParty.get("https://api.themoviedb.org/3/movie/#{tmdb_id}/images?api_key=#{TMDB_API_KEY}")
images = JSON.parse(response.body)
# this returns full size images by default (use a smaller image if possible)
# it is recommend to check the language and image size to work best in your app
poster = image_prefix + 'original' + images['posters'].first['file_path']
fanart = image_prefix + 'original' + images['backdrops'].first['file_path']
# --------------------
# tv show images
tmdb_id = 62560 # Mr. Robot
response = HTTParty.get("https://api.themoviedb.org/3/tv/#{tmdb_id}/images?api_key=#{TMDB_API_KEY}")
images = JSON.parse(response.body)
# this returns full size images by default (use a smaller image if possible)
# it is recommend to check the language and image size to work best in your app
poster = image_prefix + 'original' + images['posters'].first['file_path']
fanart = image_prefix + 'original' + images['backdrops'].first['file_path']
# --------------------
# season images
tmdb_id = 62560 # Mr. Robot
season_number = 1
response = HTTParty.get("https://api.themoviedb.org/3/tv/#{tmdb_id}/season/#{season_number}/images?api_key=#{TMDB_API_KEY}")
images = JSON.parse(response.body)
# this returns full size images by default (use a smaller image if possible)
# it is recommend to check the language and image size to work best in your app
poster = image_prefix + 'original' + images['posters'].first['file_path']
# --------------------
# episode images
tmdb_id = 62560 # Mr. Robot
season_number = 1
episode_number = 1
response = HTTParty.get("https://api.themoviedb.org/3/tv/#{tmdb_id}/season/#{season_number}/episode/#{episode_number}/images?api_key=#{TMDB_API_KEY}")
images = JSON.parse(response.body)
# this returns full size images by default (use a smaller image if possible)
# it is recommend to check the language and image size to work best in your app
screenshot = image_prefix + 'original' + images['stills'].first['file_path']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment