Class: TMDB::Trailer
- Inherits:
-
Object
- Object
- TMDB::Trailer
- Defined in:
- lib/tmdb_trailer.rb
Constant Summary collapse
- API_KEY =
File.read(File.join(File.(File.dirname(__FILE__)), "secret.txt")).gsub("\n","")
- BASE_URI =
"http://api.themoviedb.org/2.1/"
- NEXT_PAGE =
1
Instance Attribute Summary collapse
-
#coll ⇒ Object
readonly
MongoDB collection.
-
#country ⇒ Object
country associated with Trailer instance.
Instance Method Summary collapse
-
#current_page ⇒ Object
get the page which the API should use the next it calls Movie.browse.
-
#get_extra_info(id) ⇒ Object
gets the trailer, genre, keyword, year information using Movie.getInfo API call.
-
#get_movies(page) ⇒ Object
get the result of parsed json response of Movie.browse API call of TMDB.
-
#get_page ⇒ Object
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.
-
#initialize(country, db_name) ⇒ Trailer
constructor
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.
-
#populate ⇒ Object
the most important method.
-
#remember_page(page) ⇒ Object
stores locally the page which the API should use the next it calls Movie.browse.
-
#report ⇒ Object
if called, give a summary of progress of populating the database.
-
#store_result(js) ⇒ Object
store individual json result into MongoDB.
-
#total_movies ⇒ Object
gets the total number of movies that a country has on TMDB.
Constructor Details
#initialize(country, db_name) ⇒ Trailer
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
16 17 18 19 20 21 22 23 24 25 26 27 |
# 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 |
Instance Attribute Details
#coll ⇒ Object (readonly)
MongoDB collection
9 10 11 |
# File 'lib/tmdb_trailer.rb', line 9 def coll @coll end |
#country ⇒ Object
country associated with Trailer instance
10 11 12 |
# File 'lib/tmdb_trailer.rb', line 10 def country @country end |
Instance Method Details
#current_page ⇒ Object
get the page which the API should use the next it calls Movie.browse
94 95 96 |
# File 'lib/tmdb_trailer.rb', line 94 def current_page @pstore.transaction { @pstore[@country] } end |
#get_extra_info(id) ⇒ Object
gets the trailer, genre, keyword, year information using Movie.getInfo API call
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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_movies(page) ⇒ Object
get the result of parsed json response of Movie.browse API call of TMDB
50 51 52 53 54 55 56 57 58 59 |
# 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 |
#get_page ⇒ Object
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
i.e api.themoviedb.org/2.1/Movie.browse/en-US/json/API_KEY?countries=jp&order_by=rating&order=desc&min_votes=1&page=4&per_page=50
45 46 47 |
# File 'lib/tmdb_trailer.rb', line 45 def get_page @pstore.transaction { @pstore[@country] } end |
#populate ⇒ Object
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
30 31 32 33 34 35 36 37 38 39 |
# 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 |
#remember_page(page) ⇒ Object
stores locally the page which the API should use the next it calls Movie.browse
121 122 123 |
# File 'lib/tmdb_trailer.rb', line 121 def remember_page(page) @pstore.transaction { @pstore[@country] = page } end |
#report ⇒ Object
if called, give a summary of progress of populating the database
99 100 101 102 103 |
# 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_result(js) ⇒ Object
store individual json result into MongoDB
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 |
#total_movies ⇒ Object
gets the total number of movies that a country has on TMDB
80 81 82 83 84 85 86 87 88 89 90 91 |
# 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 |