flickr-folder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ config/flickr.yml
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Alyn Porter
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = flickr-folder
2
+
3
+ Uses the Flickr API (through flickr_fu) to fill a folder with images that match a specified search string and quantity.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Ryan Alyn Porter. See LICENSE for details.
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "flickr-folder"
8
+ gem.summary = %Q{Fill a folder with images from Flickr.}
9
+ gem.description = %Q{Uses the Flickr API (through flickr_fu) to fill a folder with images that match a specified search string and quantity.}
10
+ gem.homepage = "http://github.com/endymion/flickr-folder"
11
+ gem.authors = ["Ryan Alyn Porter"]
12
+ gem.add_development_dependency "shoulda", ">= 0"
13
+ gem.add_development_dependency "flickr_fu", ">= 0"
14
+ gem.add_development_dependency "sqlite3-ruby", ">= 0"
15
+ gem.add_development_dependency "sequel", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "flickr-folder #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{flickr-folder}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Alyn Porter"]
12
+ s.date = %q{2010-01-10}
13
+ s.description = %q{Uses the Flickr API (through flickr_fu) to fill a folder with images that match a specified search string and quantity.}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "flickr-folder.gemspec",
26
+ "lib/flickr-folder.rb",
27
+ "test/helper.rb",
28
+ "test/test_flickr-folder.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/endymion/flickr-folder}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{Fill a folder with images from Flickr.}
35
+ s.test_files = [
36
+ "test/helper.rb",
37
+ "test/test_flickr-folder.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
46
+ s.add_development_dependency(%q<flickr_fu>, [">= 0"])
47
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
48
+ s.add_development_dependency(%q<sequel>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_dependency(%q<flickr_fu>, [">= 0"])
52
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
53
+ s.add_dependency(%q<sequel>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<shoulda>, [">= 0"])
57
+ s.add_dependency(%q<flickr_fu>, [">= 0"])
58
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
59
+ s.add_dependency(%q<sequel>, [">= 0"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,123 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'FileUtils'
4
+ require 'flickr_fu'
5
+ require 'sqlite3'
6
+ require 'sequel'
7
+
8
+ class FlickrFolder
9
+
10
+ DB_FILE_NAME = '.flickr-folder.db'
11
+ attr :config, true
12
+ attr :flickr, true
13
+
14
+ def initialize(config)
15
+ if !config or
16
+ [:search, :folder, :config].any? {|either| config[either].nil?}
17
+ raise ArgumentError, 'Configuration hash required with :search, :folder and :config parameters.'
18
+ end
19
+ @config = config
20
+ @flickr = Flickr.new(@config[:config])
21
+ end
22
+
23
+ def update
24
+ data_setup
25
+ photos = []
26
+
27
+ # Count existing photo files to figure out how many new ones are needed.
28
+ return photos.size unless (files_needed_count = @config[:folder][:number] - photo_count) > 0
29
+
30
+ page = 1
31
+ while true do
32
+
33
+ # Get a list of matching images from Flikr.
34
+ (unfiltered_photos = (@flickr.photos.search @config[:search].merge({:page => page}))).each do |photo|
35
+
36
+ # Filter results.
37
+ next unless @config[:filter].nil? or @config[:filter].call(photo)
38
+
39
+ # Skip known photos.
40
+ cached_photos = @data[:photos]
41
+ next if cached_photos.filter(:id => photo.id).count > 0
42
+
43
+ puts "New photo: #{photo.id}, \"#{photo.title}\", max #{photo.sizes.last.width.to_s}px wide" if @config[:verbose]
44
+
45
+ # Pick the first photo that's above the minimum size, if specified. Otherwise grab the largest size.
46
+ selected_size = photo.sizes.last
47
+ minimum = @config[:folder][:minimum_resolution]
48
+ next if minimum and
49
+ photo.sizes.last.width.to_i < minimum and photo.sizes.last.height.to_i < minimum
50
+ photo.sizes.each do |size|
51
+ if size.width.to_i > minimum or size.height.to_i > minimum
52
+ selected_size = size
53
+ break
54
+ end
55
+ end if minimum
56
+
57
+ puts "Downloading size: #{selected_size.width} x #{selected_size.height}" if @config[:verbose]
58
+ url = URI.parse(selected_size.source)
59
+ request = Net::HTTP::Get.new(url.path)
60
+ response = Net::HTTP.start(url.host, url.port) do |http|
61
+ http.request(request)
62
+ end
63
+ format = photo.original_format || (photo.sizes.first.source =~ /\.(\w+)$/; $1)
64
+ open(File.join(@config[:folder][:path], "#{photo.id}.#{format}"), "wb") do |file|
65
+ file.write(response.body)
66
+ end
67
+
68
+ photos << photo
69
+ cached_photos.insert(:id => photo.id)
70
+
71
+ return photos.size if photos.size >= files_needed_count
72
+
73
+ end
74
+
75
+ page += 1
76
+
77
+ end
78
+
79
+ end
80
+
81
+ def photo_files
82
+ Dir.new(@config[:folder][:path]).entries.reject do |file|
83
+ file.eql? '.' or file.eql? '..' or file.eql? DB_FILE_NAME
84
+ end
85
+ end
86
+
87
+ def photo_count
88
+ photo_files.size
89
+ end
90
+
91
+ def purge_photos(count)
92
+ photo_files.sort {|x,y|
93
+ File.new(photo_path(x)).mtime <=>
94
+ File.new(photo_path(y)).mtime
95
+ }.slice(0, count).each do |file|
96
+ FileUtils.rm(photo_path(file))
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def data_path
103
+ photo_path(DB_FILE_NAME)
104
+ end
105
+
106
+ def photo_path(file)
107
+ File.join(@config[:folder][:path], file)
108
+ end
109
+
110
+ def data_setup
111
+ create_table = false
112
+ unless File.file?(data_path)
113
+ FileUtils::mkpath(@config[:folder][:path])
114
+ SQLite3::Database.new(data_path)
115
+ create_table = true
116
+ end
117
+ @data = Sequel.sqlite(data_path)
118
+ @data.create_table :photos do
119
+ primary_key :id
120
+ end if create_table
121
+ end
122
+
123
+ end
@@ -0,0 +1,11 @@
1
+ require 'FileUtils'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'flickr-folder'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,87 @@
1
+ require 'helper'
2
+
3
+ class TestFlickrFolder < Test::Unit::TestCase
4
+
5
+ context "The creation of a FlikrFolder" do
6
+ should "fail if there is no option hash specified." do
7
+ assert_raise ArgumentError do
8
+ FlickrFolder.new
9
+ end
10
+ end
11
+ should "fail if there is no :search option specified" do
12
+ assert_raise ArgumentError do
13
+ FlickrFolder.new({ :folder => 'tmp/photos' })
14
+ end
15
+ end
16
+ should "fail if there is no :folder option specified" do
17
+ assert_raise ArgumentError do
18
+ FlickrFolder.new({ :search => 'miami beach' })
19
+ end
20
+ end
21
+ end
22
+
23
+ @@purged_once = false
24
+ context "A FlickrFolder" do
25
+ setup do
26
+ FileUtils::rm_rf('tmp') unless @@purged_once; @@purged_once = true
27
+ @folder = FlickrFolder.new({
28
+ :config => 'config/flickr.yml',
29
+ :folder => {
30
+ :path => 'tmp/photos',
31
+ :number => 3,
32
+ :minimum_resolution => 1000
33
+ },
34
+ :search => {
35
+ :tags => 'miami beach',
36
+ :sort => 'interestingness-desc'
37
+ }
38
+ })
39
+ end
40
+ should "00 find photos after a search for a known positive" do
41
+ assert_not_nil @folder
42
+ @folder.update
43
+ assert_equal 3, @folder.photo_count
44
+ end
45
+ should "01 purge photos" do
46
+ assert_not_nil @folder
47
+ assert_equal 3, @folder.photo_count
48
+ @folder.purge_photos 2
49
+ assert_equal 1, @folder.photo_count
50
+ end
51
+ should "02 find more photos after a few are deleted" do
52
+ assert_not_nil @folder
53
+ assert_equal 1, @folder.photo_count
54
+ @folder.update
55
+ assert_equal 3, @folder.photo_count
56
+ end
57
+ end
58
+
59
+ context "A filtered FlickrFolder" do
60
+ setup do
61
+ FileUtils::rm_rf('tmp')
62
+ @folder = FlickrFolder.new({
63
+ :config => 'config/flickr.yml',
64
+ :folder => {
65
+ :path => 'tmp/photos',
66
+ :number => 3,
67
+ :minimum_resolution => 100
68
+ },
69
+ :search => {
70
+ :tags => 'miami beach',
71
+ :sort => 'interestingness-desc'
72
+ },
73
+ :filter => Proc.new do |photo|
74
+ true if photo.id.slice(0,1).eql? '1'
75
+ end
76
+ })
77
+ end
78
+ should "only download files that match the filter" do
79
+ assert_not_nil @folder
80
+ @folder.update
81
+ assert_equal 3, @folder.photo_count
82
+ @folder.photo_files.each do |photo_file|
83
+ assert_equal '1', photo_file.slice(0,1)
84
+ end
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flickr-folder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Alyn Porter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: flickr_fu
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: sqlite3-ruby
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: sequel
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Uses the Flickr API (through flickr_fu) to fill a folder with images that match a specified search string and quantity.
56
+ email:
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - flickr-folder.gemspec
72
+ - lib/flickr-folder.rb
73
+ - test/helper.rb
74
+ - test/test_flickr-folder.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/endymion/flickr-folder
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Fill a folder with images from Flickr.
103
+ test_files:
104
+ - test/helper.rb
105
+ - test/test_flickr-folder.rb