ngpod_scraper 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Client" do
4
+ describe "new" do
5
+ it "should convert the config's keys to symbols" do
6
+ config = {'url' => "file://spec/fixtures/test.html", 'photo' => {'path_format' => 'test.jpg'}}
7
+
8
+ client = NgpodScraper::Client.new(config)
9
+ client.config[:url].should == config['url']
10
+ client.config[:photo][:path_format].should == config['photo']['path_format']
11
+ end
12
+ end
13
+
14
+ describe "run" do
15
+ before :each do
16
+ url = 'http://photography.nationalgeographic.com/photography/photo-of-the-day'
17
+ FakeWeb.register_uri(:get, url, :body => 'spec/fixtures/test.html')
18
+
19
+ photo_url = 'http://s.ngeo.com/wpf/media-live/photos/000/210/cache/istiqlal-mosque-jakarta_21078_990x742.jpg'
20
+ FakeWeb.register_uri(:get, photo_url, :body => 'spec/fixtures/test.jpg')
21
+
22
+ @config = {
23
+ :url => url,
24
+ :show_logs => false,
25
+ :photo => {
26
+ :path_format => 'tmp/#{year}-#{month}/#{day}-#{hour}.jpg',
27
+ :wallpaper_path_format => 'tmp/photo-of-the-day.jpg',
28
+ :wallpaper_width => 1280,
29
+ :wallpaper_height => 800,
30
+ :wallpaper_background_color => 'black'
31
+ }
32
+ }
33
+ end
34
+
35
+ it "should build a photo" do
36
+ client = NgpodScraper::Client.new(@config)
37
+ photo = client.run
38
+ photo.should_not be_nil
39
+ end
40
+
41
+ it "should not do anything if the photo exists" do
42
+ path = 'spec/fixtures/test.jpg'
43
+ @config[:path_format] = path
44
+ Pow(path).exists?.should be_true
45
+
46
+ photo = Photo.new(:file => Pow(path).open, :path_format => path)
47
+ photo.should_not_receive(:save)
48
+
49
+ client = NgpodScraper::Client.new(@config)
50
+ client.should_receive(:get_photo).and_return(photo)
51
+
52
+ client.run
53
+ end
54
+
55
+ it "should save the photo if the photo does not exist" do
56
+ path = 'tmp/#{year}-#{month}/test.jpg'
57
+ Pow("tmp/#{Time.now.strftime('%Y-%m')}/test.jpg").exists?.should be_false
58
+
59
+ @config[:photo][:path_format] = path
60
+ client = NgpodScraper::Client.new(@config)
61
+ photo = client.run
62
+
63
+ Pow("tmp/#{Time.now.strftime('%Y-%m')}/test.jpg").exists?.should be_true
64
+ end
65
+
66
+ it "should save the wallpaper if the photo does not exist" do
67
+ path = 'tmp/test.jpg'
68
+
69
+ wallpaper_path = 'tmp/wallpaper.jpg'
70
+ Pow("tmp/wallpaper.jpg").exists?.should be_false
71
+
72
+ @config[:photo][:path_format] = path
73
+ @config[:photo][:wallpaper_path_format] = wallpaper_path
74
+
75
+ client = NgpodScraper::Client.new(@config)
76
+ photo = client.run
77
+
78
+ Pow("tmp/wallpaper.jpg").exists?.should be_true
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe "NgpodScraper" do
4
+ describe "client(path)" do
5
+ it "should return a client whose config is from the path" do
6
+ path = 'spec/fixtures/config.yml'
7
+
8
+ config = YAML.load_file(path)
9
+ config.should be_a(Hash)
10
+ config.should_not be_empty
11
+
12
+ client = NgpodScraper.client(path)
13
+ client.config[:url].should == config['url']
14
+ client.config[:photo][:path_format].should == config['photo']['path_format']
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe "PhotoPage" do
4
+ describe "new" do
5
+ it "should set the url and config of the page" do
6
+ url = 'http://photography.nationalgeographic.com/photography/photo-of-the-day'
7
+ config = {:show_logs => true}
8
+ photo_page = PhotoPage.new(url, config)
9
+ photo_page.url.should == url
10
+ photo_page.config.should == config
11
+ end
12
+ end
13
+
14
+ describe "photo" do
15
+ it "should get the photo of the day from the page" do
16
+ url = 'http://photography.nationalgeographic.com/photography/photo-of-the-day'
17
+ FakeWeb.register_uri(:get, url, :body => 'spec/fixtures/test.html')
18
+
19
+ photo_url = 'http://s.ngeo.com/wpf/media-live/photos/000/210/cache/istiqlal-mosque-jakarta_21078_990x742.jpg'
20
+ FakeWeb.register_uri(:get, photo_url, :body => 'spec/fixtures/test.jpg')
21
+
22
+ photo_page = PhotoPage.new(url)
23
+ photo = photo_page.photo
24
+
25
+ photo.should be_a(Photo)
26
+ end
27
+ end
28
+
29
+ describe "photo_url" do
30
+ it "should be the url of the photo of the day in the page" do
31
+ url = 'http://photography.nationalgeographic.com/photography/photo-of-the-day'
32
+ body = Pow('spec/fixtures/test.html').open.read
33
+ FakeWeb.register_uri(:get, url, :body => body)
34
+
35
+ photo_page = PhotoPage.new(url)
36
+ photo_page.photo_url.gsub(/\s+/, '').length.should > 0
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+ describe "Photo" do
3
+ describe "new" do
4
+ it "should set its attributes" do
5
+ attributes = {
6
+ :path_format => 'spec/tmp/#{f.name}',
7
+ :wallpaper_path_format => 'spec/tmp/photo-of-the-day.jpg',
8
+ :wallpaper_width => 1280,
9
+ :wallpaper_height => 800
10
+ }
11
+
12
+ photo = Photo.new(attributes)
13
+ photo.path_format.should == attributes[:path_format]
14
+ photo.wallpaper_path_format.should == attributes[:wallpaper_path_format]
15
+ photo.wallpaper_width.should == attributes[:wallpaper_width]
16
+ photo.wallpaper_height.should == attributes[:wallpaper_height]
17
+ end
18
+ end
19
+
20
+ describe "exists?" do
21
+ it "should be false if there's no file saved in photo.path" do
22
+ path = Pow('spec/fixtures/1.jpg')
23
+ path.exists?.should be_false
24
+
25
+ photo = Photo.new(:file => Pow('spec/fixtures/test.jpg').open, :path_format => 'spec/fixtures/1.jpg')
26
+ photo.exists?.should be_false
27
+ end
28
+
29
+ it "should be true if there's there's file saved in photo.path" do
30
+ path = Pow('spec/fixtures/test.jpg')
31
+ path.exists?.should be_true
32
+
33
+ photo = Photo.new(:file => path.open, :path_format => path.to_s)
34
+ photo.exists?.should be_true
35
+ end
36
+ end
37
+
38
+ describe "path" do
39
+ it "should eval the path_format" do
40
+ file = Pow('spec/fixtures/test.jpg').open
41
+ time = Time.now
42
+
43
+ photo = Photo.new(:url => file.path, :file => file, :path_format => 'tmp/#{year}/#{month}/#{day}/#{hour}-#{name}')
44
+ photo.path.should =~ /#{"tmp/#{time.strftime('%Y/%m/%d/%H')}-#{Pow(file.path).name}"}/
45
+ end
46
+ end
47
+
48
+ describe "path" do
49
+ it "should eval the wallpaper_path_format" do
50
+ file = Pow('spec/fixtures/test.jpg').open
51
+ time = Time.now
52
+
53
+ photo = Photo.new(:url => file.path, :file => file, :wallpaper_path_format => 'tmp/#{year}/#{month}/#{day}/#{hour}-#{name}')
54
+ photo.wallpaper_path.should =~ /#{"tmp/#{time.strftime('%Y/%m/%d/%H')}-#{Pow(file.path).name}"}/
55
+ end
56
+ end
57
+
58
+ describe "save" do
59
+ it "should save the photo in its path" do
60
+ file = Pow('spec/fixtures/test.jpg').open
61
+ photo = Photo.new(:url => file.path, :file => file, :path_format => 'tmp/#{year}/#{name}')
62
+ Pow(photo.path).exists?.should be_false
63
+
64
+ photo.save
65
+
66
+ Pow(photo.path).exists?.should be_true
67
+ end
68
+ end
69
+
70
+ describe "wallpaper_scale" do
71
+ it "should be the wallpaper_height/image.height if that is the smallest" do
72
+ file = Pow('spec/fixtures/test.jpg').open
73
+ photo = Photo.new(:file => file, :path_format => file.path)
74
+ photo.wallpaper_height = photo.image.rows / 4
75
+ photo.wallpaper_width = photo.image.columns / 2
76
+
77
+ photo.wallpaper_scale.should == photo.wallpaper_height.to_f / photo.image.rows
78
+ end
79
+
80
+ it "should be the wallpaper_width/image.width if that is the smallest" do
81
+ file = Pow('spec/fixtures/test.jpg').open
82
+ photo = Photo.new(:file => file, :path_format => file.path)
83
+ photo.wallpaper_height = photo.image.rows / 2
84
+ photo.wallpaper_width = photo.image.columns / 4
85
+
86
+ photo.wallpaper_scale.should == photo.wallpaper_width.to_f / photo.image.columns
87
+ end
88
+
89
+ it "should be 1 if both height and width rations are greater that 1 (don't enlarge the image)" do
90
+ file = Pow('spec/fixtures/test.jpg').open
91
+ photo = Photo.new(:file => file, :path_format => file.path)
92
+ photo.wallpaper_height = photo.image.rows * 2
93
+ photo.wallpaper_width = photo.image.columns * 4
94
+
95
+ photo.wallpaper_scale.should == 1
96
+ end
97
+ end
98
+
99
+ describe "wallpaper" do
100
+ it "should be the image centered in a background" do
101
+ file = Pow('spec/fixtures/test.jpg').open
102
+
103
+ photo = Photo.new(:file => file, :path_format => file.path, :wallpaper_background_color => 'black')
104
+ photo.wallpaper_height = photo.image.rows * 2
105
+ photo.wallpaper_width = photo.image.columns * 4
106
+
107
+ #sorry, I don't know how I can test this
108
+ photo.wallpaper.should be_a(Magick::Image)
109
+ end
110
+ end
111
+
112
+ describe "save_wallpaper" do
113
+ it "should save the wallpaper in the wallpaper_path" do
114
+ file = Pow('spec/fixtures/test.jpg').open
115
+
116
+ photo = Photo.new(:file => file, :path_format => file.path)
117
+ photo.wallpaper_background_color = 'black'
118
+ photo.wallpaper_height = 800
119
+ photo.wallpaper_width = 1280
120
+ photo.wallpaper_path_format = "tmp/#{Time.now.to_i}-wallpaper.jpg"
121
+
122
+ Pow(photo.wallpaper_path).exists?.should_not be_true
123
+
124
+ photo.save_wallpaper
125
+
126
+ Pow(photo.wallpaper_path).exists?.should be_true
127
+ end
128
+ end
129
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,16 @@
1
+ require 'ngpod_scraper'
2
+ require 'bundler/setup'
3
+ require 'fakeweb'
4
+
5
+ include NgpodScraper
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.before :suite do
9
+ FakeWeb.allow_net_connect = false
10
+ end
11
+
12
+ config.after :each do
13
+ Pow('tmp').delete! if Pow('tmp').exists?
14
+ FakeWeb.clean_registry
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ngpod_scraper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - George Mendoza
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: fakeweb
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 1
31
+ - 2
32
+ - 8
33
+ version: 1.2.8
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 27
45
+ segments:
46
+ - 1
47
+ - 3
48
+ - 0
49
+ version: 1.3.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: nokogiri
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 1
63
+ - 4
64
+ - 2
65
+ version: 1.4.2
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: pow
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 17
77
+ segments:
78
+ - 0
79
+ - 2
80
+ - 3
81
+ version: 0.2.3
82
+ type: :runtime
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: rmagick
86
+ prerelease: false
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 57
93
+ segments:
94
+ - 2
95
+ - 13
96
+ - 1
97
+ version: 2.13.1
98
+ type: :runtime
99
+ version_requirements: *id005
100
+ - !ruby/object:Gem::Dependency
101
+ name: valuable
102
+ prerelease: false
103
+ requirement: &id006 !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 59
109
+ segments:
110
+ - 0
111
+ - 8
112
+ - 2
113
+ version: 0.8.2
114
+ type: :runtime
115
+ version_requirements: *id006
116
+ description: A tool for scraping National Geographic's Photo of the Day
117
+ email:
118
+ - gsmendoza@gmail.com
119
+ executables:
120
+ - ngpod_scraper
121
+ extensions: []
122
+
123
+ extra_rdoc_files: []
124
+
125
+ files:
126
+ - .gitignore
127
+ - Gemfile
128
+ - LICENSE
129
+ - README.markdown
130
+ - Rakefile
131
+ - VERSION
132
+ - bin/ngpod_scraper
133
+ - lib/ngpod_scraper.rb
134
+ - lib/ngpod_scraper/client.rb
135
+ - lib/ngpod_scraper/ngpod_scraper.rb
136
+ - lib/ngpod_scraper/photo.rb
137
+ - lib/ngpod_scraper/photo_page.rb
138
+ - lib/ngpod_scraper/version.rb
139
+ - ngpod_scraper.gemspec
140
+ - spec/fixtures/config.yml
141
+ - spec/fixtures/test.html
142
+ - spec/fixtures/test.jpg
143
+ - spec/ngpod_scraper/client_spec.rb
144
+ - spec/ngpod_scraper/ngpod_scraper_spec.rb
145
+ - spec/ngpod_scraper/photo_page_spec.rb
146
+ - spec/ngpod_scraper/photo_spec.rb
147
+ - spec/spec.opts
148
+ - spec/spec_helper.rb
149
+ homepage: http://github.com/gsmendoza/ngpod_scraper
150
+ licenses: []
151
+
152
+ post_install_message:
153
+ rdoc_options: []
154
+
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ none: false
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ hash: 3
163
+ segments:
164
+ - 0
165
+ version: "0"
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ none: false
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ hash: 3
172
+ segments:
173
+ - 0
174
+ version: "0"
175
+ requirements: []
176
+
177
+ rubyforge_project: ngpod_scraper
178
+ rubygems_version: 1.8.10
179
+ signing_key:
180
+ specification_version: 3
181
+ summary: National Geographic Photo of the Day Screen Scraper
182
+ test_files: []
183
+