Skip to content

Instantly share code, notes, and snippets.

@inukshuk
Last active Apr 21, 2020
Embed
What would you like to do?
Import Annotated Images Into Tropy
#!/usr/bin/env ruby
require 'json'
input = ARGV[0]
items = []
def create_item(title)
{
'template' => 'https://tropy.org/v1/templates/generic',
'http://purl.org/dc/elements/1.1/title' => title,
'photo' => [{
'template' => 'https://tropy.org/v1/templates/photo',
'mimetype' => 'unknown',
'checksum' => 'unknown',
'http://purl.org/dc/elements/1.1/title' => title,
'note' => [{
'html' => {
}
}]
}]
}
end
File.open input do |f|
item = nil
f.each do |line|
key, value = line.split(':')
value = JSON.parse(value.strip.delete_suffix(',')) unless value.nil?
case key
when 'title'
items << item unless item.nil?
item = create_item(value)
when 'path'
item['photo'][0]['path'] = value
when 'note'
item['photo'][0]['note'][0]['html']['@value'] = value
end
end
items << item unless item.nil?
end
puts JSON.pretty_generate({ '@graph' => items }, indent: ' ')
title: "This is the title",
path: "/Users/dupin/Desktop/a.png",
note: "This is the note"
title: "This is another title"
path: "/Users/dupin/Desktop/b.png"
note: "This is another note"
@inukshuk
Copy link
Author

inukshuk commented Apr 14, 2020

Run this via ruby convert.rb input.txt > output.json (replacing input.txt with the path to your input file). Note, this is currently extremely simple: assumes you really have title, path, and note lines for each item (and no multiple paths and or notes, etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment