-
-
Save drh-stanford/d8e0080c47f0d37acc8456eca57a279f to your computer and use it in GitHub Desktop.
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 'json' | |
require 'faraday' | |
require 'logger' | |
USER_ID = '3802090' | |
ENDPOINT = "https://api.zotero.org/users/#{USER_ID}" | |
conn = Faraday.new(url: ENDPOINT, params: { v: 3, format: :json }) do |faraday| | |
faraday.response :logger, ::Logger.new('zotero.log') | |
faraday.adapter Faraday.default_adapter | |
end | |
### Fetching tags | |
tags = [] | |
response = conn.get 'tags' # TODO: Assumes /tags always returns all tags without pagination | |
tags += JSON.parse(response.body).collect {|i| i['tag']}.flatten | |
puts "TAGS" | |
puts tags.join("\n") | |
### Map all exhibit druids to a tag | |
druid2tag = {} | |
%w(aa111bb2222 cc333dd4444 ee555ff6666 yy999zz0000).each do |druid| | |
druid2tag[druid] = nil | |
tags.each do |t| | |
druid2tag[druid] = t if t.include?(druid) | |
end | |
end | |
puts "DRUID -> TAG" | |
puts JSON.pretty_generate(druid2tag) | |
### Fetching all items by tag | |
tag = tags.sample | |
bibliography = [] | |
done = false | |
until done do | |
response = conn.get 'items', { | |
tag: tag, # the exhibit item's druid | |
include: :bib, | |
sort: 'creator', # use creator so that `meta` has Author-Date | |
start: bibliography.length, | |
limit: 100 | |
} | |
items = JSON.parse(response.body) | |
done = true if items.empty? | |
bibliography += items | |
end | |
puts "SELECTED TAG: '#{tag}'" | |
puts "UNSORTED" | |
bibliography.shuffle! # ensure a randomized array | |
puts bibliography.collect {|i| i['bib'] }.join("\n") | |
### Sort bibliography by Author-Date using `meta` data from Zotero | |
def author_date(item) | |
[item['meta']['creatorSummary'], item['meta']['parsedDate']].join(' ') | |
end | |
bibliography.sort! { |x, y| author_date(x) <=> author_date(y) } | |
puts "SORTED" | |
puts bibliography.collect {|i| i['bib'] }.join("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: