Created
November 22, 2020 03:12
-
-
Save bruce/07a497e9765fcb4589e02e6233a6f563 to your computer and use it in GitHub Desktop.
A hack to build references in Obsidian for Zotero items
This file contains hidden or 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 "net/http" | |
require "uri" | |
require "yaml" | |
require "json" | |
require "fileutils" | |
class String | |
def dasherize | |
self. | |
gsub(/([A-Z]+)([A-Z][a-z])/,'\1-\2'). | |
gsub(/([a-z\d])([A-Z])/,'\1-\2'). | |
downcase | |
end | |
end | |
CONFIG = YAML.load_file(File.expand_path("~/.config/zettlekasten.yaml")) | |
host = CONFIG.dig("zotero", "debug-bridge", "host") || "127.0.0.1" | |
password = CONFIG.dig("zotero", "debug-bridge", "password") | |
port = CONFIG.dig("zotero", "debug-bridge", "port") || 23119 | |
vault = File.expand_path(CONFIG.dig("obsidian", "vault")) | |
SCRIPT =<<EOS | |
await Zotero.Schema.schemaUpdatePromise; | |
var s = new Zotero.Search(); | |
s.libraryID = Zotero.Libraries.userLibraryID; | |
var results = await s.search(); | |
var items = await Zotero.Items.getAsync(results); | |
return JSON.stringify(items); | |
EOS | |
url = URI.parse("http://#{host}:#{port}/debug-bridge/execute?password=#{password}") | |
resp, data = Net::HTTP.post(url, SCRIPT, {"Content-Type" => "application/javascript"}) | |
items = JSON.parse(JSON.parse(resp.body)) | |
keys = items.map { |i| i["key"] } | |
rpc = JSON.dump( | |
jsonrpc: "2.0", | |
method: "item.citationkey", | |
params: [keys] | |
) | |
url = URI.parse("http://#{host}:#{port}/better-bibtex/json-rpc") | |
resp, data = Net::HTTP.post(url, rpc, {"Content-Type" => "application/json", "Accept" => "application/json"}) | |
mapping = JSON.parse(resp.body)["result"] | |
items.each do |item| | |
cite_key = mapping[item["key"]] | |
if cite_key && !cite_key.start_with?("zotero-") | |
literature_note = File.join(vault, "references", "#{cite_key}.md") | |
unless File.exist?(literature_note) | |
FileUtils.mkdir_p(File.dirname(literature_note)) | |
File.open(literature_note, "w") do |f| | |
f.puts "# #{item["title"]}" | |
f.puts | |
if item["itemType"] == "webpage" | |
f.puts "#webpage #{item["url"]}" | |
else | |
f.puts "##{item["itemType"].dasherize}" | |
end | |
f.puts | |
if item["abstractNote"] | |
f.puts item["abstractNote"] | |
f.puts | |
end | |
if item["date"] | |
f.puts "## Date" | |
f.puts | |
f.puts item["date"] | |
f.puts | |
end | |
authors = item["creators"].select { |c| c["creatorType"] == "author" } | |
unless authors.empty? | |
f.puts "## Authors" | |
f.puts | |
authors.each do |author| | |
f.puts "- #{author["lastName"]}, #{author["firstName"]}" | |
end | |
f.puts | |
end | |
f.puts "## Notes" | |
f.puts | |
f.puts "#TK" | |
f.puts | |
end | |
puts literature_note | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment