3

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

Travis Bell
  • 204
  • 2
  • 7
PMP
  • 231
  • 6
  • 25
  • What are you trying to do in your rake file? Get two of the oldest movies in TMDb and save them to your database? Understanding what you're really trying to do will help to find you the best alternate way of getting what you want. – carols10cents Oct 19 '13 at 18:35
  • What my rake file was doing was browsing movies in a Descending order in the TMDB Database. So it was finding the oldest movie and working it's way up to the newest movie. – PMP Oct 20 '13 at 10:24

1 Answers1

1

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 :)

carols10cents
  • 6,943
  • 7
  • 39
  • 56
  • Thanks for your answer, but I'm having trouble understanding two things. 1- Where would this code go? If it's in the rake file, how would it be formulated? 2- How would I, with the code you supplied, fetch information like `movie.name` and `movie.overview` as it is displayed in my current [Rake File](https://gist.github.com/mpcoding/733c740aa7544db1699c#file-schedule-rake-L17) – PMP Oct 20 '13 at 18:22
  • 1. The sample code I have here should be able to go right into your schedule.rake, replacing the call to `TmdbMovie.browse`. 2. The data that is returned from `Tmdb.api_call` is just raw data, so that's why I have the `results.map!` line. That will transform the list of raw movie data into instances of the `TmdbMovie` object. [It looks like you might have to tell TmdbMovie to expand_results](https://github.com/Irio/ruby-tmdb/blob/master/lib/ruby-tmdb3/tmdb_movie.rb#L46), I've updated my sample code with that. Then on each result you should be able to call `.name`, etc. Have you tried this yet? – carols10cents Oct 20 '13 at 18:34
  • I have updated my Rake file to look like [THIS](https://gist.github.com/mpcoding/60a1115238d0b0075c35), yet, I get the following error: `rake aborted! undefined method gsub for nil:NilClass` – PMP Oct 20 '13 at 19:20
  • can you post a [gist](http://gist.github.com) of the full stack trace of the error and let me know which line in schedule.rake corresponds to the line number in the stack trace that references schedule.rake? – carols10cents Oct 20 '13 at 19:22
  • Ok, on [that line of the ruby-tmdb gem](https://github.com/Irio/ruby-tmdb/blob/8087357e74e189abc5e3c70240b260a359864d5e/lib/ruby-tmdb3/tmdb.rb#L75) it's expecting there to always be a query parameter. Try adding one with an empty string (I've updated my sample code) and see if the discover API cares if you pass a query param. – carols10cents Oct 20 '13 at 20:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39602/discussion-between-pmp-and-carolclarinet) – PMP Oct 20 '13 at 20:23