Last active
April 21, 2020 12:08
-
-
Save inukshuk/3340397a1789d5c44358daeae5f55d47 to your computer and use it in GitHub Desktop.
Import Annotated Images Into Tropy
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
#!/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: ' ') |
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
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this via
ruby convert.rb input.txt > output.json
(replacinginput.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.).