Class: FlickrCollage::Flickr
- Inherits:
-
Object
- Object
- FlickrCollage::Flickr
- Includes:
- Methadone::CLILogging
- Defined in:
- lib/FlickrCollage.rb
Overview
Class for Flickr. Get and download top-rated images for keywords
Constant Summary
- API_KEY =
"27ad23a09746546c02c79f39879ee408"
- SHARED_SECRET =
"707eaf92eece0744"
- SEARCH_KEYWORD =
"Giant Robots Smashing into Other Giant Robots"
- SEARCH_COUNT =
1
Instance Attribute Summary collapse
-
#img_path ⇒ String
Path for saving images.
Instance Method Summary collapse
-
#download(opts = {}) ⇒ Array<String>, String
Download images from urls.
-
#initialize(opts = {}) ⇒ Flickr
constructor
A new instance of Flickr.
-
#scrape(opts = {}) ⇒ String
Scrape top-rated images for keyword.
-
#search(opts = {}) ⇒ Array<String>, String
Search top-rated images for keyword.
Constructor Details
#initialize(opts = {}) ⇒ Flickr
Returns a new instance of Flickr
48 49 50 51 52 53 54 55 56 |
# File 'lib/FlickrCollage.rb', line 48 def initialize(opts = {}) opts[:api_key] ||= API_KEY opts[:shared_secret] ||= SHARED_SECRET opts[:img_path] ||= IMG_PATH FlickRaw.api_key = opts[:api_key] FlickRaw.shared_secret = opts[:shared_secret] @img_path = opts[:img_path] end |
Instance Attribute Details
#img_path ⇒ String
Returns Path for saving images
46 47 48 |
# File 'lib/FlickrCollage.rb', line 46 def img_path @img_path end |
Instance Method Details
#download(opts = {}) ⇒ Array<String>, String
Download images from urls
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/FlickrCollage.rb', line 95 def download(opts = {}) opts ||= [] photos = [] return photos unless opts.any? opts = [keyword: opts[:keyword], url: opts[:url]] unless opts.is_a?(Array) FileUtils.mkdir_p("#{@img_path}tmp") begin opts.each do |img| debug "flickr.download: #{img[:keyword]} #{img[:url]}" next unless defined?(img[:url]) && !img[:url].nil? open(img[:url]) do |url| File.open("#{@img_path}tmp/#{img[:keyword]}.jpg", "wb") do |file| file.puts url.read end file = "#{@img_path}tmp/#{img[:keyword]}.jpg" debug "flickr.download: file #{file}" photos << file if Image.valid?(file) end end rescue StandardError => e warn "flickr.download: caught exception: #{e.inspect}" raise e if RAISE_ERROR end photos.size > 1 ? photos : photos.first end |
#scrape(opts = {}) ⇒ String
Scrape top-rated images for keyword
128 129 130 131 132 133 134 |
# File 'lib/FlickrCollage.rb', line 128 def scrape(opts = {}) opts[:keyword] ||= SEARCH_KEYWORD url = search(keyword: opts[:keyword]) debug "flickr.scrape: #{opts[:keyword]} #{url}" download(keyword: opts[:keyword], url: url) end |
#search(opts = {}) ⇒ Array<String>, String
Search top-rated images for keyword
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/FlickrCollage.rb', line 66 def search(opts = {}) opts[:keyword] ||= SEARCH_KEYWORD opts[:count] ||= SEARCH_COUNT photos = [] begin list = flickr.photos.search(text: opts[:keyword], sort: "interestingness-desc", per_page: opts[:count]) debug "flickr.search: #{opts[:keyword]} (img: #{list.size})" list.each do |photo| info = flickr.photos.getInfo(photo_id: photo.id) debug "flickr.search: get image url for #{photo.id}" # debug "flickr.search: #{FlickRaw.url_photopage(info)}" photos << FlickRaw.url_c(info) end rescue StandardError => e warn "flickr.search: caught exception #{e}" raise e if RAISE_ERROR end photos.size > 1 ? photos : photos.first end |