Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thanks for this library! #107

Closed
tenderlove opened this issue Jul 11, 2018 · 2 comments
Closed

Thanks for this library! #107

tenderlove opened this issue Jul 11, 2018 · 2 comments

Comments

@tenderlove
Copy link

tenderlove commented Jul 11, 2018

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
Copy link
Owner

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

@nickbuff
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants