Skip to content

Instantly share code, notes, and snippets.

@bsag
Created June 20, 2021 11:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bsag/638f3380ac7fba12cb7e1abb5e5f272f to your computer and use it in GitHub Desktop.
A system for exporting references from Zotero and importing them to Tiddlywiki.
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
)
type Fields struct {
Caption string `json:"caption"`
Publication string `json:"publication"`
Zlink string `json:"zlink"`
Year string `json:"year"`
Authors string `json:"authors"`
Medium string `json:"medium"`
}
type Source struct {
Type string `json:"type"`
Title string `json:"title"`
Tags string `json:"tags"`
Fields Fields `json:"fields"`
}
func parseTitleAndBody(s *Source) (title string, body []byte) {
// re-encode the json body
// return the body and title
body, _ = json.Marshal(s)
title = s.Title
return title, body
}
func sendTwPut(title string, body []byte) {
// TW Put (PUT http://127.0.0.1:8080/recipes/default/tiddlers/TIDDLER_TITLE)
// json := []byte(myjson)
newBody := bytes.NewBuffer(body)
// Create client
client := &http.Client{}
// Create URI
uri := "http://127.0.0.1:8080/recipes/default/tiddlers/" + title
// Create request
req, _ := http.NewRequest("PUT", uri, newBody)
// Headers
req.Header.Add("x-requested-with", "TiddlyWiki")
req.Header.Add("Content-Type", "application/json; charset=utf-8")
// Execute the Request
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == 204 {
fmt.Printf("Tiddler '%s' created successfully.\n", title)
} else {
fmt.Println("Something went wrong. Tiddler not created.")
}
resp.Body.Close()
}
func main() {
// pipe
// Use the compiled binary command like so:
// pbpaste | twimport
var buf []byte
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
buf = append(buf, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
var sources []Source
json.Unmarshal([]byte(buf), &sources)
// for each source in sources, save title and json
for _, s := range sources {
title, body := parseTitleAndBody(&s)
// returns body and title, send on to sendTwPut()
sendTwPut(title, body)
}
}
{
"translatorID": "3c2ea00ee65f4f53913810dc83362eff",
"label": "TiddlyWiki Source JSON",
"creator": "bsag",
"target": "json",
"minVersion": "1.0",
"maxVersion": "",
"priority": 100,
"displayOptions": {
"exportCharset": "UTF-8xBOM",
"exportNotes": false
},
"inRepository": false,
"translatorType": 2,
"lastUpdated": "2021-05-16 16:43:20"
}
function doExport() {
const tiddlers = [];
let item;
while (item = Zotero.nextItem()) {
let auths = [];
for (i = 0; i < item.creators.length; i++) {
auths.push((item.creators[i].firstName ? item.creators[i].firstName + ' ' : '') + item.creators[i].lastName);
}
var library_id = item.libraryID ? item.libraryID : 0;
let zot_link = "zotero://select/items/"+library_id+"_"+item.key
let tiddler = {
type : "text/vnd.tiddlywiki",
title: item.citekey,
tags : "Source",
fields : {
caption : item.title,
publication : item.publicationTitle,
zlink : zot_link,
year : ZU.strToDate(item.date).year,
authors : auths.join(", "),
medium : item.itemType
}
};
tiddlers.push(tiddler)
}
Zotero.write(JSON.stringify(tiddlers));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment