Skip to content

Instantly share code, notes, and snippets.

@janosrusiczki
Created November 25, 2020 14:13
Show Gist options
  • Save janosrusiczki/9b8437508a6c2915bac29dc640f9199b to your computer and use it in GitHub Desktop.
Save janosrusiczki/9b8437508a6c2915bac29dc640f9199b to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
namespace :flickr do
desc "Imports a Flickr export (from 2018)"
task import: :environment do
photo_files = []
Dir.glob("#{Rails.root}/flickr/photos/*.{jpg,png}").each do |file_name|
photo_files << file_name
end
not_found = []
Dir.glob("#{Rails.root}/flickr/json/photo_*.json").each do |file_name|
file = File.read(file_name)
photo_hash = JSON.parse(file)
id = photo_hash['id']
original_filename = %r{[^\/]+$}.match(photo_hash['original'])[0]
photo_file_matches = photo_files.select { |i| i[/#{id}/] }
break if photo_file_matches.size > 1
photo_file = photo_file_matches[0]
if photo_file
photo = Photo.find_or_create_by(
name: photo_hash['name'],
description: photo_hash['description'],
date_taken: photo_hash['date_taken'],
license: photo_hash['license'],
flickr_id: photo_hash['id'],
flickr_views: photo_hash['count_views'],
flickr_faves: photo_hash['count_faves'],
flickr_date_imported: photo_hash['date_imported'],
flickr_photopage: photo_hash['photopage'],
flickr_original: photo_hash['original']
)
else
not_found << id + ' ' + original_filename
end
putc '.'
end
puts ''
puts not_found
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment