squarepusher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in squarepusher.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/squarepusher ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'squarepusher'
5
+
6
+ def error_msg(msg)
7
+ $stderr.puts "ERROR - #{msg}"
8
+ end
9
+
10
+ def fail(msg, code=1)
11
+ error_msg(msg)
12
+ exit code
13
+ end
14
+
15
+ options = { :size => :original }
16
+ OptionParser.new do |opts|
17
+ opts.banner = "USAGE: squarepusher [options] <api-key> <api-secret> <token> <output_dir>"
18
+
19
+ opts.on("-s", "--size SIZE", "secret") do |v|
20
+ options[:size] = v.to_sym
21
+ end
22
+
23
+ opts.parse!(ARGV)
24
+
25
+ if ARGV.size != 4
26
+ $stderr.puts opts
27
+ exit 1
28
+ end
29
+ end
30
+
31
+ key, secret, token, output_dir = ARGV
32
+
33
+ client = Squarepusher::Client.new(key, secret, token, options)
34
+
35
+ client.each_photoset do |pset|
36
+ puts pset.inspect
37
+ client.download_photoset(pset, output_dir)
38
+ end
@@ -0,0 +1,3 @@
1
+ module Squarepusher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,101 @@
1
+ require 'fileutils'
2
+ require 'optparse'
3
+
4
+ FlickRawOptions = {}
5
+
6
+ $fileutils = FileUtils::Verbose
7
+
8
+ module Squarepusher
9
+
10
+ class Client
11
+
12
+ def initialize(key, secret, token, args={})
13
+ FlickRawOptions['timeout'] = args[:timeout] || 5
14
+
15
+ require 'flickraw'
16
+
17
+ FlickRaw.api_key = key
18
+ FlickRaw.shared_secret = secret
19
+
20
+ flickr.auth.checkToken(:auth_token => token)
21
+
22
+ # FlickRaw.auth_token = token
23
+ # FlickRaw.timeout = 5
24
+
25
+ size = args[:size] || :large
26
+
27
+ @url_for_photo = case size
28
+ when :original
29
+ lambda { |p| FlickRaw.url_o(p) }
30
+ when :large
31
+ lambda { |p| FlickRaw.url_b(p) }
32
+ when :medium_640
33
+ lambda { |p| FlickRaw.url_z(p) }
34
+ when :medium_500
35
+ lambda { |p| FlickRaw.url(p) }
36
+ when :small
37
+ lambda { |p| FlickRaw.url_m(p) }
38
+ when :thumb
39
+ lambda { |p| FlickRaw.url_t(p) }
40
+ when :small_square
41
+ lambda { |p| FlickRaw.url_s(p) }
42
+ else
43
+ raise Exception("unrecognized size: #{size}")
44
+ end
45
+ end
46
+
47
+ def each_photoset
48
+ flickr.photosets.getList.each do |pset|
49
+ yield pset
50
+ end
51
+ end
52
+
53
+ def download_photoset(photoset, output_dir)
54
+ set_dir = File.join(output_dir, photoset.title.gsub(/\s+/, '-'))
55
+ $fileutils.mkdir_p set_dir
56
+
57
+ photos = flickr.photosets.getPhotos(:photoset_id => photoset.id, :extras => "original_format,url_o")["photo"]
58
+ photos.each do |p|
59
+ # puts p.inspect
60
+ name = p.title.gsub(/[^\w_+\.-]/, '-')
61
+
62
+ url = @url_for_photo[p]
63
+
64
+ path = File.join(set_dir, "#{name}.jpg")
65
+ if File.exists?(path)
66
+ puts "#{path} exists; skipping"
67
+ else
68
+ download_image(url, path)
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def download_image(url, path)
76
+ puts "#{url} -> #{path}"
77
+ uri = URI.parse(url)
78
+ Net::HTTP.start(uri.host, uri.port) do |http|
79
+ full_path = uri.request_uri
80
+ response = http.get(full_path)
81
+
82
+ # puts response.inspect
83
+ case response
84
+ when Net::HTTPError
85
+ when Net::HTTPFound
86
+ location = response["location"]
87
+ if not location =~ /^http.*/
88
+ location = "#{uri.scheme}://#{uri.host}:#{uri.port}#{location}"
89
+ end
90
+ download(location, path)
91
+ else
92
+ open(path, 'w') do |out|
93
+ out << response.body
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "squarepusher/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'squarepusher'
7
+ s.version = Squarepusher::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+
10
+ s.authors = 'mhawthorne'
11
+ s.email = 'mhawthorne@gmail.com'
12
+ s.homepage = 'https://github.com/mhawthorne/squarepusher'
13
+ s.summary = "downloads photos from flickr"
14
+
15
+ s.add_dependency 'flickraw'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.rubyforge_project = 'nowarning'
23
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: squarepusher
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - mhawthorne
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-02 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: flickraw
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email: mhawthorne@gmail.com
37
+ executables:
38
+ - squarepusher
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Rakefile
47
+ - bin/squarepusher
48
+ - lib/squarepusher.rb
49
+ - lib/squarepusher/version.rb
50
+ - squarepusher.gemspec
51
+ has_rdoc: true
52
+ homepage: https://github.com/mhawthorne/squarepusher
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: nowarning
81
+ rubygems_version: 1.5.0
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: downloads photos from flickr
85
+ test_files: []
86
+