class TMDB::Trailer
Constants
- API_KEY
- BASE_URI
- NEXT_PAGE
Attributes
Public Class Methods
country - country code i.e “us” for united states, “hk” for hongkong db_name - database name to store mongo documents, will create one for you if it doesnt exists
# File lib/tmdb_trailer.rb, line 16 def initialize(country,db_name) @country = country db = Mongo::Connection.new.db(db_name) @coll = db["movies"] #if mongo collection does not exist, create index to avoid duplicate documents @coll.create_index("name", :unique => true) if @coll.count == 0 # initialize pstore @pstore = PStore.new("#{db_name}.pstore") @pstore.transaction { @pstore[@country] = 1 } if @coll.find(:country=> @country).count == 0 #initialize persistent http connection @http = Net::HTTP::Persistent.new end
Public Instance Methods
get the page which the API should use the next it calls Movie.browse
# File lib/tmdb_trailer.rb, line 94 def current_page @pstore.transaction { @pstore[@country] } end
gets the trailer, genre, keyword, year information using Movie.getInfo API call
# File lib/tmdb_trailer.rb, line 106 def get_extra_info(id) genres = [] keywords = [] url = "#{BASE_URI}Movie.getInfo/en/json/#{API_KEY}/#{id}" uri = URI.parse(url) response = @http.request(uri).body movie = JSON.parse(response)[0] trailer, keywords = movie["trailer"], movie["keywords"] movie["genres"].each {|g| genres << g["name"]} movie["released"] =~ /^(\d*)/ year = $1 return trailer,genres,keywords,year end
get the result of parsed json response of Movie.browse API call of TMDB
# File lib/tmdb_trailer.rb, line 50 def get_movies(page) votes = @country == "us" ? 3 : 0 @params = "countries=#{@country}&order_by=rating&" + "min_votes=#{votes}&order=desc&page=#{page}&per_page=50" url = "#{BASE_URI}Movie.browse/en/json/#{API_KEY}?#{@params}" uri = URI.parse(url) response = @http.request(uri).body movies = JSON.parse(response) return movies end
gets the page number with which the API should use the next it calls Movie.browse it will be used in the page query parameter of the api call
# File lib/tmdb_trailer.rb, line 45 def get_page @pstore.transaction { @pstore[@country] } end
the most important method. It gets the page that api has to call, sends API request, store the result to mongo, then store the next page for api to call at a future time on pstore
# File lib/tmdb_trailer.rb, line 30 def populate page = get_page (page..page+4).each do |page| movies = get_movies(page) num_of_movies = movies.length break if num_of_movies == 1 store_result(movies) remember_page(page + NEXT_PAGE) end end
stores locally the page which the API should use the next it calls Movie.browse
# File lib/tmdb_trailer.rb, line 121 def remember_page(page) @pstore.transaction { @pstore[@country] = page } end
if called, give a summary of progress of populating the database
# File lib/tmdb_trailer.rb, line 99 def report total = total_movies movies_in_mongo = @coll.find(:country => @country).count return "#{@country} currently has populated #{movies_in_mongo} out of #{total} movies available on tmdb" end
store individual json result into MongoDB
# File lib/tmdb_trailer.rb, line 62 def store_result(js) doc = {} js.each do |movie| id = movie["id"] trailer, genres , keywords, year = get_extra_info(id) doc["tmdb_id"] = id doc["name"] = movie["name"] doc["trailer"] = trailer doc["year"] = year doc["genres"] = genres doc["country"] = @country doc["keywords"] = keywords @coll.save(doc) doc = {} end end
gets the total number of movies that a country has on TMDB
# File lib/tmdb_trailer.rb, line 80 def total_movies page = @country == "us" ? 50 : 1 total = @country == "us" ? 2500 : 0 loop do movies = get_movies(page) num_of_movies = movies.length break if num_of_movies == 1 page += 1 total += num_of_movies end return total end