tmdb_trailer 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,6 +7,8 @@ gem "mongo"
7
7
  gem "bson_ext"
8
8
  gem "i18n"
9
9
  gem "json"
10
+ gem "net-http-persistent"
11
+
10
12
  # Add dependencies to develop your gem here.
11
13
  # Include everything needed to run rake, tests, features, etc.
12
14
  group :development do
data/Gemfile.lock CHANGED
@@ -13,6 +13,7 @@ GEM
13
13
  json (1.4.6)
14
14
  mongo (1.2.1)
15
15
  bson (>= 1.2.1)
16
+ net-http-persistent (1.4.1)
16
17
  rake (0.8.7)
17
18
  rcov (0.9.9)
18
19
  rspec (2.2.0)
@@ -34,5 +35,6 @@ DEPENDENCIES
34
35
  jeweler (~> 1.5.1)
35
36
  json
36
37
  mongo
38
+ net-http-persistent
37
39
  rcov
38
40
  rspec (~> 2.2.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
data/lib/tmdb_trailer.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  require 'pstore'
2
2
  require 'open-uri'
3
+ require 'net/http/persistent'
4
+ require 'mongo'
3
5
  require 'json'
4
6
 
5
7
  module TMDB
6
8
  class Trailer
9
+ attr_reader :coll # MongoDB collection
10
+ attr_accessor :country # country associated with Trailer instance
7
11
  API_KEY = File.read(File.join(File.expand_path(File.dirname(__FILE__)), "secret.txt")).gsub("\n","")
8
- attr_reader :current_page
9
- attr_reader :coll
10
- attr_accessor :country
12
+ NEXT_PAGE = 1
11
13
  # country - country code i.e "us" for united states, "hk" for hongkong
12
14
  # mode - use "production" when using for production
13
15
  # - use "test" for testing
@@ -39,17 +41,20 @@ module TMDB
39
41
  @pstore[@country] = 1
40
42
  end
41
43
  end
44
+
45
+ #initialize persistent http connection
46
+ @http = Net::HTTP::Persistent.new
42
47
  end
43
48
 
44
49
  # 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
45
50
  def populate
46
51
  page = get_page
47
- (page..page+4).each do |current|
48
- js = get_json(current)
49
- break if js.length == 1
50
- store_result(js)
51
- @current_page = current + 1
52
- remember_page
52
+ (page..page+4).each do |page|
53
+ movies = get_movies(page)
54
+ num_of_movies = movies.length
55
+ break if num_of_movies == 1
56
+ store_result(movies)
57
+ remember_page(page + NEXT_PAGE)
53
58
  end
54
59
  end
55
60
 
@@ -64,15 +69,15 @@ module TMDB
64
69
  end
65
70
 
66
71
  # get the result of parsed json response of Movie.browse API call of TMDB
67
- def get_json(page)
72
+ def get_movies(page)
68
73
  votes = @country == "us" ? 3 : 1
69
74
  @params = "countries=#{@country}&order_by=rating&" +
70
75
  "min_votes=#{votes}&order=desc&page=#{page}&per_page=50"
71
76
  url = "#{@base_uri}#{API_KEY}?#{@params}"
72
- http = open(url)
73
- resp= http.read
74
- js = JSON.parse(resp)
75
- return js
77
+ uri = URI.parse(url)
78
+ response = @http.request(uri).body
79
+ movies = JSON.parse(response)
80
+ return movies
76
81
  end
77
82
 
78
83
  # store individual json result into MongoDB
@@ -98,15 +103,22 @@ module TMDB
98
103
  page = @country == "us" ? 50 : 1
99
104
  total = @country == "us" ? 2500 : 0
100
105
  loop do
101
- js = get_json(page)
102
- number_of_movies = js.length
103
- break if number_of_movies == 1
106
+ movies = get_movies(page)
107
+ num_of_movies = movies.length
108
+ break if num_of_movies == 1
104
109
  page += 1
105
- total += number_of_movies
110
+ total += num_of_movies
106
111
  end
107
112
  return total
108
113
  end
109
114
 
115
+ # get the page which the API should use the next it calls Movie.browse
116
+ def current_page
117
+ @pstore.transaction do
118
+ @pstore[@country]
119
+ end
120
+ end
121
+
110
122
  # if called, give a summary of progress of populating the database
111
123
  def report
112
124
  total = total_movies
@@ -122,22 +134,23 @@ module TMDB
122
134
  genres = []
123
135
  keywords = []
124
136
  url = "http://api.themoviedb.org/2.1/Movie.getInfo/en/json/#{API_KEY}/#{id}"
125
- s = open(url).read
126
- res = JSON.parse(s)
127
- trailer = res[0]["trailer"] if res[0]["trailer"]
128
- res[0]["genres"].each do |genre|
137
+ uri = URI.parse(url)
138
+ response = @http.request(uri).body
139
+ movie = JSON.parse(response)
140
+ trailer = movie[0]["trailer"] if movie[0]["trailer"]
141
+ movie[0]["genres"].each do |genre|
129
142
  genres << genre["name"]
130
143
  end
131
- keywords = res[0]["keywords"]
132
- res[0]["released"] =~ /^(\d*)/
144
+ keywords = movie[0]["keywords"]
145
+ movie[0]["released"] =~ /^(\d*)/
133
146
  year = $1
134
147
  return trailer,genres,keywords,year
135
148
  end
136
149
 
137
150
  # stores locally the page which the API should use the next it calls Movie.browse
138
- def remember_page
151
+ def remember_page(page)
139
152
  @pstore.transaction do
140
- @pstore[@country] = @current_page
153
+ @pstore[@country] = page
141
154
  end
142
155
  end
143
156
 
@@ -45,7 +45,7 @@ describe TMDB::Trailer do
45
45
 
46
46
  it "should grab json response from a given url" do
47
47
  page = @trailer.get_page
48
- js = @trailer.get_json(page)
48
+ js = @trailer.get_movies(page)
49
49
  js.length.should eq(37)
50
50
  end
51
51
 
@@ -54,16 +54,16 @@ describe TMDB::Trailer do
54
54
  @trailer.populate
55
55
  pstore = PStore.new("test.pstore")
56
56
  pstore.transaction do
57
- expected = pstore[country] + 0
58
- @trailer.current_page.should eq(expected)
57
+ @expected = pstore[country] + 0
59
58
  end
59
+ @trailer.current_page.should eq(@expected)
60
60
  end
61
61
 
62
62
  it "should store results to mongodb" do
63
63
  doc = {}
64
64
  size_before = @trailer.coll.count
65
65
  page = @trailer.get_page
66
- js = @trailer.get_json(page)
66
+ js = @trailer.get_movies(page)
67
67
  @trailer.store_result(js)
68
68
  @trailer.coll.count.should eq(size_before + js.length)
69
69
  end
data/tmdb_trailer.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tmdb_trailer}
8
- s.version = "0.2.5"
8
+ s.version = "0.2.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["redgetan"]
12
- s.date = %q{2011-02-14}
12
+ s.date = %q{2011-02-15}
13
13
  s.description = %q{ Movie Trailer information will be stored in mongoDB. Information includes year released, movie genres, keywords, and country}
14
14
  s.email = %q{redge.tan@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
53
53
  s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
54
54
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
55
55
  s.add_runtime_dependency(%q<json>, [">= 0"])
56
+ s.add_runtime_dependency(%q<net-http-persistent>, [">= 0"])
56
57
  s.add_development_dependency(%q<rspec>, ["~> 2.2.0"])
57
58
  s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
58
59
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
@@ -62,6 +63,7 @@ Gem::Specification.new do |s|
62
63
  s.add_dependency(%q<bson_ext>, [">= 0"])
63
64
  s.add_dependency(%q<i18n>, [">= 0"])
64
65
  s.add_dependency(%q<json>, [">= 0"])
66
+ s.add_dependency(%q<net-http-persistent>, [">= 0"])
65
67
  s.add_dependency(%q<rspec>, ["~> 2.2.0"])
66
68
  s.add_dependency(%q<bundler>, ["~> 1.0.10"])
67
69
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
@@ -72,6 +74,7 @@ Gem::Specification.new do |s|
72
74
  s.add_dependency(%q<bson_ext>, [">= 0"])
73
75
  s.add_dependency(%q<i18n>, [">= 0"])
74
76
  s.add_dependency(%q<json>, [">= 0"])
77
+ s.add_dependency(%q<net-http-persistent>, [">= 0"])
75
78
  s.add_dependency(%q<rspec>, ["~> 2.2.0"])
76
79
  s.add_dependency(%q<bundler>, ["~> 1.0.10"])
77
80
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tmdb_trailer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - redgetan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-14 00:00:00 -05:00
18
+ date: 2011-02-15 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -75,8 +75,22 @@ dependencies:
75
75
  prerelease: false
76
76
  name: json
77
77
  - !ruby/object:Gem::Dependency
78
- type: :development
78
+ type: :runtime
79
79
  version_requirements: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirement: *id005
89
+ prerelease: false
90
+ name: net-http-persistent
91
+ - !ruby/object:Gem::Dependency
92
+ type: :development
93
+ version_requirements: &id006 !ruby/object:Gem::Requirement
80
94
  none: false
81
95
  requirements:
82
96
  - - ~>
@@ -87,12 +101,12 @@ dependencies:
87
101
  - 2
88
102
  - 0
89
103
  version: 2.2.0
90
- requirement: *id005
104
+ requirement: *id006
91
105
  prerelease: false
92
106
  name: rspec
93
107
  - !ruby/object:Gem::Dependency
94
108
  type: :development
95
- version_requirements: &id006 !ruby/object:Gem::Requirement
109
+ version_requirements: &id007 !ruby/object:Gem::Requirement
96
110
  none: false
97
111
  requirements:
98
112
  - - ~>
@@ -103,12 +117,12 @@ dependencies:
103
117
  - 0
104
118
  - 10
105
119
  version: 1.0.10
106
- requirement: *id006
120
+ requirement: *id007
107
121
  prerelease: false
108
122
  name: bundler
109
123
  - !ruby/object:Gem::Dependency
110
124
  type: :development
111
- version_requirements: &id007 !ruby/object:Gem::Requirement
125
+ version_requirements: &id008 !ruby/object:Gem::Requirement
112
126
  none: false
113
127
  requirements:
114
128
  - - ~>
@@ -119,12 +133,12 @@ dependencies:
119
133
  - 5
120
134
  - 1
121
135
  version: 1.5.1
122
- requirement: *id007
136
+ requirement: *id008
123
137
  prerelease: false
124
138
  name: jeweler
125
139
  - !ruby/object:Gem::Dependency
126
140
  type: :development
127
- version_requirements: &id008 !ruby/object:Gem::Requirement
141
+ version_requirements: &id009 !ruby/object:Gem::Requirement
128
142
  none: false
129
143
  requirements:
130
144
  - - ">="
@@ -133,7 +147,7 @@ dependencies:
133
147
  segments:
134
148
  - 0
135
149
  version: "0"
136
- requirement: *id008
150
+ requirement: *id009
137
151
  prerelease: false
138
152
  name: rcov
139
153
  description: " Movie Trailer information will be stored in mongoDB. Information includes year released, movie genres, keywords, and country"