Sha256: 52a816a677d863af23da303ed00ff9cbec3c0fa83b0f7d798fd7a93d983c5839

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'active_support/core_ext/object/blank'
require 'crawler/movie/providers/tmdb/configuration'
require 'crawler/movie'
require 'date'
require 'faraday'
require 'json'

module Crawler
  module Movie
    module Providers
      module Tmdb
        def self.search(query)
          movies = []
          current_page = 1

          loop do
            response = Faraday.get('https://api.themoviedb.org/3/search/movie',
              api_key: api_key,
              language: language,
              query: query,
              page: current_page,
              include_adult: include_adult,
              region: region
            )

            break if !response.success? || !response.body

            json = JSON.parse(response.body)
            results = json['results'].map do |movie|
              {
                id: movie['id'],
                source: 'the-movie-database',
                title: movie['title'],
                poster_url: movie['poster_path'].presence && "https://image.tmdb.org/t/p/original#{movie['poster_path']}",
                backdrop_url: movie['backdrop_path'].presence && "https://image.tmdb.org/t/p/original#{movie['backdrop_path']}",
                original_languages: movie['original_language'].present? ? [movie['original_language']] : [],
                original_title: movie['original_title'].presence,
                genres: movie['genre_ids'],
                overview: movie['overview'].presence,
                release_date: movie['release_date'].present? && Date.parse(movie['release_date'])
              }
            end

            movies.concat(results)

            break if current_page >= json['total_pages']

            current_page += 1
          end

          movies
        end
      end
    end
  end
end

Crawler::Movie.add_provider :tmdb, score: 0.95

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
crawler-movie-tmdb-1.0.1 lib/crawler/movie/providers/tmdb.rb
crawler-movie-tmdb-1.0.0 lib/crawler/movie/providers/tmdb.rb