PMP
PMP

Reputation: 231

Rails App Movies API - Heroku

I have a Rails Movie App which will potentially use APIs from other websites to fill my database.

I am using the TMDB.org API (I already have an API Key) to extract, for now lets say the Title and Description of a movie.

How would I go by extracting information from the tmdb site to my site and display the information in <% @movie.title %> and <% @movie.description %> The information taken will need to be placed in my PostgreSQL Database using Heroku So thats one question

The other question is how would I do this without running a method for every movie in the TMDB database

For Example, using the Ruby-TMDB Gem, instead of running

TmdbMovie.find(:title => "The Social Network", :limit => 10, :expand_results => true, :language => "en")

and

TmdbMovie.find(:title => "The Dark Knight Rises ", :limit => 10, :expand_results => true, :language => "en")

for every movie I want in my database (which is every movie in the TMDB Database), what would I run to GET ALL Movies.

And then Display them in my movies :show page

To sum it all up, how to I get database information from TMDB.org to my rails app and display information from a TMDB Movie :show page to my movie :show page, using the Ruby-TMDB Gem IN Heroku? Would there be a rake task, if so what would it be?

Many Thanks!

Upvotes: 0

Views: 757

Answers (1)

omarshammas
omarshammas

Reputation: 631

There are 2 problems you wish to tackle.

How and where in my rails app should I pull the data?

Yeah this can be accomplished with rake. Add a new rake file lib/tasks/tmdb.rake

namespace :db do
  task :pull_tmdb_data => :environment do

    Tmdb.api_key = "t478f8de5776c799de5a"
    # setup your default language
    Tmdb.default_language = "en"

    # find movies by id
    @movie = TmdbMovie.find(id: 123)
    Movie.create title: @movie.title, description: @movie.description

    # find movies in groups
    @movies = TmdbMovie.find(:title => 'Iron Man')
    @movies.each do |movie|
        Movie.create title: movie.title, description: movie.description
    end

  end
end

And now everytime you want to populate your db with tmdb you can simply run rake db:pull_tmdb_data

What are the necessary queries to pull all the movies from tmdb?

By glancing over their api there is no shortcut to duplicating the database, if you want to duplicate it your best bet may be to contact them directly. You can brute force it by trying every possible id for movies but beware that they do throttle you. Below is a quote from their website.

We do enforce a small amount of rate limiting. Please be aware that should you exceed these limits, you will receive a 503 error.

  • 30 requests every 10 seconds per IP
  • Maximum 20 simultaneous connections

It may be worth considering if you really need to duplicate tmdb. When tmdb is adding new movies and fixing errors in their data your database will be outdated as a result you will face a slew of data integrity issues which will be hard to resolve.

Upvotes: 1

Related Questions