Last active
April 30, 2019 21:51
-
-
Save ckdake/612bdaa1a8333ac37b17 to your computer and use it in GitHub Desktop.
Import your Gallery 2 g2data dir into Flickr
This file contains 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 characters
# This takes your g2data albums directory (run it from inside g2data) and makes sets on flickr containing all the photos | |
# They are set to private so you can make sure everything is looking good before sharing with the world | |
require 'flickraw' | |
FlickRaw.api_key=ENV['flickr-api-key'] | |
FlickRaw.shared_secret=ENV['flickr-api-secret'] | |
# token = flickr.get_request_token | |
# auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete') | |
# | |
# puts "Open this url in your process to complete the authication process : #{auth_url}" | |
# puts "Copy here the number given when you complete the process." | |
# verify = gets.strip | |
# | |
# 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 | |
flickr.access_token=ENV['flickr-access-token'] | |
flickr.access_secret=ENV['flickr-access-secret'] | |
@photosets = {} | |
@mappings = {} | |
def refresh_a_mapping(dir, name) | |
Dir.entries(dir).each do |f| | |
if f != '.' && f != '..' | |
if File.directory?("#{dir}/#{f}") | |
@mappings["#{dir}/#{f}".gsub('/','-')] = "#{dir}/#{f}" | |
refresh_a_mapping("#{dir}/#{f}", f) | |
end | |
end | |
end | |
end | |
def refresh_mappings(dir) | |
Dir.entries(dir).each do |f| | |
if f != '.' && f != '..' | |
refresh_a_mapping(dir + f, f) | |
end | |
end | |
end | |
def refresh_photosets | |
@photosets = flickr.photosets.getList | |
end | |
def upload_image_to_set(file, set) | |
puts "INFO: #{file} to #{set}" | |
#if flickr.photos.search(user_id: 'me', tags: "g2migrated", text: file).count > 0 | |
# puts "WARN: #{file} already uploaded to set #{set}!" | |
#else | |
photo_id = flickr.upload_photo file, | |
description: file, | |
tags: 'g2migrated', | |
is_public: 0 | |
if the_set = @photosets.find { |ps| ps.title == set } | |
puts "INFO: Existing set: #{set}" | |
flickr.photosets.addPhoto(photoset_id: the_set.id, photo_id: photo_id) | |
else | |
puts "INFO: New set: #{set}" | |
flickr.photosets.create(title: set, primary_photo_id: photo_id) | |
refresh_photosets | |
end | |
sleep 10 | |
#end | |
end | |
def process_album_for_set(set_name) | |
refresh_photosets | |
album_path = @mappings[set_name] | |
if album_path.nil? || !File.exists?(album_path) | |
puts "Not here: #{set_name}" | |
return | |
end | |
if set = @photosets.find { |ps| ps.title == set_name } | |
@full_photoset = flickr.photosets.getPhotos(photoset_id: set.id) | |
Dir.entries(album_path).sort.each do |f| | |
if f != '.' && f != '..' | |
if !File.directory?("#{album_path}/#{f}") | |
if !@full_photoset.photo.map(&:title).find { |p| p == f.gsub(/\.(.*)$/, '') } | |
puts "INFO: not found #{f} in #{set_name}" | |
upload_image_to_set("#{album_path}/#{f}",set_name) | |
end | |
end | |
end | |
end | |
else | |
puts "WARN: Can't find set #{set_name}" | |
return | |
end | |
end | |
def import_album(album_path, name) | |
refresh_photosets | |
set_name = "#{album_path}".gsub('/','-') | |
if set = @photosets.find { |ps| ps.title == set_name } | |
puts "WARN: Set #{set_name} already imported! Finding missing instead." | |
process_album_for_set(set_name) | |
else | |
puts "INFO: processing: #{set_name}" | |
Dir.entries(album_path).sort.each do |f| | |
if f != '.' && f != '..' | |
if File.directory?("#{album_path}/#{f}") | |
import_album("#{album_path}/#{f}", f) | |
else | |
upload_image_to_set("#{album_path}/#{f}",set_name) | |
end | |
end | |
end | |
end | |
end | |
def import(albums_path) | |
refresh_mappings(albums_path) | |
Dir.entries(albums_path).sort.each do |f| | |
if f != '.' && f != '..' | |
import_album(albums_path + f, f) | |
end | |
end | |
end | |
def find_missing(albums_path) | |
refresh_mappings(albums_path) | |
refresh_photosets | |
@photosets.each do |ps| | |
process_album_for_set(ps.title) | |
end | |
end | |
import('albums/') | |
# find_missing('albums/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never got an e-mail notification of your comment! Sorry for 1000x delay.
You could easily adapt this to do Gallery 3, just change it to use var/albums/ instead of g2data/albums/ and it should work.