Created
March 22, 2015 19:19
-
-
Save wsams/19fa53da811abe490c77 to your computer and use it in GitHub Desktop.
I have an image gallery application backed by MySQL. Each album is a directory on disk full of images. This is a Ruby script that pulls the data from the database and writes it as JSON to text files in each individual album directory. This is mainly for use when backing up and storing in my lock box at the bank. It could also be used for regener…
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
require "rubygems" | |
require "mysql" | |
require "json" | |
db = Mysql.new("localhost", "foobar", "ll33h5235kj", "images") | |
metadata = db.query("select * from metadata") | |
metadata.each_hash do |row| | |
full_album_path = "/path/to/images/#{row['album_dir']}" | |
if File.exists?(full_album_path) | |
meta = row | |
meta['items'] = {} | |
items = db.query("select * from items where mid=#{row['mid']}") | |
items.each_hash do |items_row| | |
meta['items'][items_row['iid']] = items_row | |
meta['items'][items_row['iid']]['comments'] = {} | |
comments = db.query("select * from comments where iid=#{items_row['iid']}") | |
comments.each_hash do |comments_row| | |
meta['items'][items_row['iid']]['comments'][comments_row['cid']] = comments_row | |
end | |
end | |
else | |
puts "#{full_album_path} DNE" | |
end | |
#puts JSON.pretty_generate(meta) | |
puts "Writing #{full_album_path}/metadata.json" | |
f = File.new("#{full_album_path}/metadata.json", "w") | |
f.write(JSON.pretty_generate(meta)) | |
f.close | |
end | |
users_obj = {} | |
users = db.query("select * from users") | |
users.each_hash do |users_row| | |
users_obj[users_row['uid']] = users_row | |
users_obj[users_row['uid']]['usersmetadata'] = {} | |
usersmetadata = db.query("select * from usersmetadata where uid=#{users_row['uid']}") | |
usersmetadata.each_hash do |usersmetadata_row| | |
users_obj[users_row['uid']]['usersmetadata'][usersmetadata_row['umid']] = usersmetadata_row | |
end | |
end | |
#puts JSON.pretty_generate(users_obj) | |
db.close | |
f = File.new("users.json", "w") | |
f.write(JSON.pretty_generate(users_obj)) | |
f.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
are You done with your code?or still facing problem?