Skip to content

Instantly share code, notes, and snippets.

@drh-stanford
Last active March 13, 2017 20:57
Show Gist options
  • Save drh-stanford/d8e0080c47f0d37acc8456eca57a279f to your computer and use it in GitHub Desktop.
Save drh-stanford/d8e0080c47f0d37acc8456eca57a279f to your computer and use it in GitHub Desktop.
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")
@drh-stanford
Copy link
Author

drh-stanford commented Mar 13, 2017

Sample output:

TAGS
http://purl.stanford.edu/aa111bb2222
cc333dd4444
My Favorite Book (ee555ff6666)
DRUID -> TAG
{
  "aa111bb2222": "http://purl.stanford.edu/aa111bb2222",
  "cc333dd4444": "cc333dd4444",
  "ee555ff6666": "My Favorite Book (ee555ff6666)",
  "yy999zz0000": null
}
SELECTED TAG: 'http://purl.stanford.edu/aa111bb2222'
UNSORTED
<div class="csl-bib-body" style="line-height: 1.35; padding-left: 2em; text-indent:-2em;">
  <div class="csl-entry">Abby, Jane. &#x201C;My Thesis,&#x201D; 2010.</div>
</div>
<div class="csl-bib-body" style="line-height: 1.35; padding-left: 2em; text-indent:-2em;">
  <div class="csl-entry">Doe, Jane. <i>My Book</i>, 2017.</div>
</div>
SORTED
<div class="csl-bib-body" style="line-height: 1.35; padding-left: 2em; text-indent:-2em;">
  <div class="csl-entry">Abby, Jane. &#x201C;My Thesis,&#x201D; 2010.</div>
</div>
<div class="csl-bib-body" style="line-height: 1.35; padding-left: 2em; text-indent:-2em;">
  <div class="csl-entry">Doe, Jane. <i>My Book</i>, 2017.</div>
</div>

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