Skip to content

Instantly share code, notes, and snippets.

@albodelu
Forked from rectifyer/tmdb.rb
Created October 22, 2016 00:24
Show Gist options
  • Save albodelu/35c74bc3737f2de45a1319be24263819 to your computer and use it in GitHub Desktop.
Save albodelu/35c74bc3737f2de45a1319be24263819 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