Skip to content

Instantly share code, notes, and snippets.

@ckdake ckdake/gallery2flickr.rb
Last active Apr 30, 2019

Embed
What would you like to do?
Import your Gallery 2 g2data dir into Flickr
# 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/')
@BenjaminHCCarr

This comment has been minimized.

Copy link

commented Nov 6, 2014

Sorry to go on a tangent, but first thank your for this script and your years of support of gallery.
This is a great script for those on Gallery2, is there any hope of a script to move Gallery 3 users to flickr either through the route you took with G2 or through the API?
I just discovered Gallery is "hibernating" and have spent two hours googling and reading the (locked) forums for a way to migrate G3 to flickr.

@ckdake

This comment has been minimized.

Copy link
Owner Author

commented Apr 15, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.