Skip to content

Instantly share code, notes, and snippets.

@sheepeeh
Created April 10, 2014 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheepeeh/10417852 to your computer and use it in GitHub Desktop.
Save sheepeeh/10417852 to your computer and use it in GitHub Desktop.
For a given TXT file with Omeka item IDs, generate HTML for a text-only version of a Neatline Timeline.
# Using the same query used for your Neatline Timeline, generate a list of Omeka IDs with https://gist.github.com/sheepeeh/10415207
# When script is finished, you will have an HTML file with the same name as your TXT file. It will be ugly, you should probably pretty-print it.
require 'nokogiri'
require 'open-uri'
Item = Struct.new(:id, :title, :date, :link, :year, :flag)
def get_metadata(from_file)
items = []
years = []
# Read lines from target file
File.readlines(from_file).each do |line|
begin
flag = false
id = line.gsub("\n","")
# NOTE: Subsitute appropriate address for YOUR_OMEKA_URL
doc = Nokogiri::HTML(open("YOUR_OMEKA_URL/items/show/#{id}"))
title = doc.css('#show-item-title').text
date = doc.css('#dublin-core-date .element-text').text
date = date.gsub("\n","").gsub(" ","")
year, month, day = date.split('-')
flag = true if day.nil?
# Convert date strings to date objects for sorting
day = "01" if day.nil?
date = [year, month, day].join('-')
date = DateTime.parse(date)
date = date.to_date
datei = date.strftime("%s").to_i
link = "/items/show/#{id}"
year = date.strftime('%Y').to_s
time_item = Item.new(id, title, date, link, year, flag)
items << time_item
years << year
rescue => e
puts "#{e}: Skipping item #{line}"
next
end
end
years = years.compact
years = years.uniq
years.sort!
items.sort_by! { |e| e[:date] }
puts "Items created. Now generating HTML..."
html = "<div id='text-only'>"
years.each do |y|
begin
html += "<h3>#{y}</h3>"
html += "<ul>"
itemy = items.select {|e| e[:year] =~ /#{y}/ }
itemy.each do |e|
# Revert flagged dates to year + month only.
if e[:flag] == true
e[:date] = e[:date].to_s
year, month, day = e[:date].split ('-')
e[:date] = [year, month].join('-')
end
html += "<li>#{e[:date]}: <a href='#{e[:link]}'>#{e[:title]}</a></li>"
end
html += "</ul>"
rescue => e
puts "#{e}: Moving to next item."
next
end
end
html += "</div>"
# Print HTML to file
fseed = File.basename(from_file,'txt')
hf = File.open("#{fseed}.html",'a')
hf.puts html
puts "Done."
end
# Change to desired file
get_metadata('omeka_ids.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment