hanklords / flickraw

Ruby library to access flickr api.
http://hanklords.github.com/flickraw/
MIT License
507 stars 98 forks source link

Thanks for this library! #107

Closed tenderlove closed 6 years ago

tenderlove commented 6 years ago

Hi, I just wanted to say thanks for this library. I'm using it to download all my photos from flickr so I can import them in to Photos.app. The README got me up and running quickly, and everything seems to be working great! I got a little tripped up with flickr's API, but once I read their docs everything went smoothly.

Here's the program I wrote (it streams photos from flickr in parallel):

require 'flickraw'
require 'net/http'
require 'net/https'
require 'uri'

queue = Queue.new

SIZE = 5
downloaders = SIZE.times.map do
  Thread.new do
    Thread.current.abort_on_exception = true

    http = Net::HTTP.new 'farm1.staticflickr.com', 443
    http.use_ssl = true
    http.start

    while work = queue.pop
      url = URI(work)
      filename = url.path[/[^\/]*$/]

      p filename
      req = Net::HTTP::Get.new(url.request_uri)
      http.request(req) do |res|
        File.open "pics/#{filename}", "wb" do |f|
          res.read_body do |segment|
            f.write segment
          end
        end
      end
    end
  end
end

FlickRaw.api_key = ENV["FLICKR_API_KEY"]
FlickRaw.shared_secret = ENV["FLICKR_SHARED_SECRET"]

flickr = FlickRaw::Flickr.new

flickr.access_token = ENV["FLICKR_ACCESS_TOKEN"]
flickr.access_secret = ENV["FLICKR_ACCESS_SECRET"]

# From here you are logged:
login = flickr.test.login
puts "You are now authenticated as #{login.username}"

per_page = 500

total_photos = flickr.people.getInfo(user_id: login.id).photos.count
pages = total_photos / per_page + (total_photos % per_page == 0 ? 0 : 1)
p pages

login_id = login.id
pages.times.map do |page|
  Thread.new do
    p PAGE: page
    flickr.people.getPhotos(user_id: login_id, per_page: per_page, page: page + 1).each do |thing|
      original = flickr.photos.getSizes(photo_id: thing.id).find { |size|
        size.label == "Original"
      }
      queue << original.source
    end
  end
end.map(&:join)
SIZE.times { queue << nil }
puts "DONE"

downloaders.map(&:join)

Anyway, just wanted to say thanks!

hanklords commented 6 years ago

Thank you @tenderlove , this is very kind. I am very happy it was useful for you.

nicolasleander commented 6 years ago

Thanks, @tenderlove, your issue basically saved me hours of life.