Created
July 21, 2014 17:20
Revisions
-
jacobo created this gist
Jul 21, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,128 @@ require 'flickraw' FlickRaw.api_key= "SECRET" FlickRaw.shared_secret="SECRET" def reauth token = flickr.get_request_token auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'write') puts auth_url verify = gets #"123-123-123" begin flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify) login = flickr.test.login puts "You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}" rescue FlickRaw::FailedResponse => e puts "Authentication failed : #{e.msg}" end end def accumulate(&block) fetched = [] page = 1 continues = 2 while continues > 0 this_fetch = block.call(page) next_set = this_fetch.to_a.map{|x| x["id"]} - fetched puts next_set.size page += 1 if next_set.size > 0 fetched += next_set else continues -= 1 end end fetched end def gettagged(tag) accumulate do |page| flickr.photos.search(user_id: "birdswell", page: page, per_page: 500, tags: tag) end end def getall accumulate do |page| flickr.photos.search(user_id: "birdswell", page: page, per_page: 500) end end def getset(photoset_id) accumulate do |page| flickr.photosets.getPhotos(photoset_id: photoset_id, page: page, per_page: 500)["photo"] end end def getmachinetagged(tag) accumulate do |page| flickr.photos.search(user_id: "birdswell", page: page, per_page: 500, machine_tags: tag) end end def refresh allphotos = getall.map{|x| x["id"]}.shuffle puts "allphotos #{allphotos.size}" setphotos = getset("72157645356290658").map{|x| x["id"]} puts "setphotos #{setphotos.size}" excludephotos = gettagged("noappletv").map{|x| x["id"]} puts "excludephotos #{excludephotos.size}" toadd = (allphotos - setphotos) - excludephotos puts "toadd #{toadd.size}" toremove = setphotos & excludephotos puts "toremove #{toremove.size}" toadd.each do |photo| flickr.photosets.addPhoto(photo_id: photo, photoset_id: "72157645356290658") end toremove.each do |photo| flickr.photosets.removePhoto(photoset_id: "72157645356290658", photo_id: photo) end flickr.photosets.reorderPhotos(photoset_id: "72157645356290658", photo_ids: (setphotos + toadd).shuffle.join(",")) end def upload_with_retry(path) photo = nil begin photo = flickr.upload_photo path rescue => e puts e.inspect sleep 2 retry end if photo md5 = `md5 -q #{path}`.strip puts "tag #{photo} - #{md5}" flickr.photos.addTags(photo_id: photo, tags: "hasmd5,hash:md5=#{md5}") end photo end def tagall allphotos = getall; allphotos.size taggedphotos = gettagged("hasmd5"); taggedphotos.size untaggedphotos = allphotos - taggedphotos; untaggedphotos.size puts "untaggedphotos #{untaggedphotos.size}" work_q = Queue.new untaggedphotos.each{|x| work_q.push x } workers = (0...4).map do Thread.new do begin while photo = work_q.pop(true) current_tags = flickr.photos.getInfo(photo_id: photo)["tags"] if current_tags.detect{|t| t["raw"][0,9] == "hash:md5="} unless current_tags.detect{|t| t["raw"] == "hasmd5"} flickr.photos.addTags(photo_id: photo, tags: "hasmd5") end else size = flickr.photos.getSizes(photo_id: photo).detect{|s| s["label"] == "Original"} source_url = size["source"] md5 = `curl #{source_url} | md5`.strip puts "tag #{photo} - #{md5}" flickr.photos.addTags(photo_id: photo, tags: "hasmd5,hash:md5=#{md5}") end end rescue ThreadError end end end workers.map(&:join) end