Question

TMDB.org recently made a change to their API which removes the capability to browse their database. My Rails app used to use the tmdb-ruby gem to browse the TMDB database, but this gem only worked with v2.0 of the API, which is now defunct.

TMDB.org recommends using this gem, and since it is forked from the gem I previously used, it makes it a bit easier.

My PostgreSQL database is already populated with data imported from TMDB when v2.0 was still extant and when I could use the browse feature.

How can I now use the find feature (ie: @movie = TmdbMovie.find(:title => "Iron Man", :limit => 1) ) to find a random movie, without supplying the title of the Movie.

This is my rake file which worked with the older gem.

I would like to know how to have it work the same way but whilst using the find instead of the browse.

Thanks

Was it helpful?

Solution

I don't think find is what you need in order to get what you want (getting the oldest movies in the database and working its way up to the newest movie). Looking at the TMDb API documentation, it looks like they now have discover that may have replaced the browse that you used to use.

I don't see discover anywhere in Irio's ruby-tmdb fork, but it looks like most of the specific methods they have (like TmdbMovie.find) call a generic method Tmdb.api_call.

You should be able to use the generic method to do something like:

api_return = Tmdb.api_call(
  "discover/movie", 
  {
    page: 1, 
    sort_by: 'release_date.asc',
    query: '' # Necessary because Tmdb.api_call throws a nil error if you don't specify a query param value
  }, 
  "en"
)
results = api_return["results"]

results.flatten!(1)
results.uniq!
results.delete_if &:nil?

results.map!{|m| TmdbMovie.new(m, true)} # `true` tells TmdbMovie.new to expand results

If this works, you could even fork Irio's fork, implement a TmdbMovie.discover method supporting all the options and handling edge cases like TmdbMovie.find does, and send them a pull request since it just looks like they haven't gotten around to implementing this yet and I'm sure other people would like to have this method as well :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top