This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (117 loc) · 3.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const {
rights,
prop,
chain,
map,
splitOn,
head,
fromMaybe,
pipe,
maybeToEither,
isLeft,
Nothing,
Just,
Left,
Right,
} = require ('sanctuary');
const fetch = require('node-fetch');
const fs = require('fs');
const maybe = ifBad => ifGood => m =>
isLeft (m) ? ifBad : ifGood (m.value);
const writeFilePromised = filePath => fileData =>
new Promise((res, rej) => {
fs.writeFile(filePath, fileData, (err) => {
if (err) res (Left (err.message));
res (Right (`wrote file ${filePath}`));
});
});
const writeFileIfDoesntExist = filePath => fileData =>
new Promise((res, rej) => {
fs.stat(filePath, (err, stat) => {
// ENOENT means the file exists
if (err?.code !== 'ENOENT') {
res (Left (err === null ? `file ${filePath} exists` : err.message));
return;
}
fs.writeFile(filePath, fileData, (err) => {
if (err) res (Left (err.message));
res (Right (`wrote file ${filePath}`));
});
});
});
const emptyFileWithTitle = title =>
`\
---
title: ${title}
---
##
`;
const fileTitleThing = title => links =>
`\
---
title: ${title}
---
${map (link => `\
## [[${link}]]
`) (links).join('')}
`
/**
* this takes the data property of https://gist.github.com/dstillman/f1030b9609aadc51ddec as its input
* @returns {Object} Maybe an object with keys title and fileName
*/
const getTitle = pipe([
prop ('title'),
splitOn (':'),
item => maybeToEither (`${item} didn't have a title`) (head (item))
]);
const parrergonTypes = ['attachment'];
const isParrergon = ({itemType}) => parrergonTypes.includes(itemType);
const makeName = pipe([
item => isParrergon (item) ? Left ('was a parrergon') : Right (item),
chain (getTitle),
map (title =>
title.replace(/(‘)|(’)/g, '\''),
),
map (title => ({
title,
fileName: `z_${title.replace(/\s+/g, '_').toLowerCase()}`
})),
]);
const writeFiles = ({outDir, bibFileTitle}) => fileStuffs => {
const titles = map (prop ('title')) (rights (fileStuffs));
const bibData = fileTitleThing (bibFileTitle) (titles);
return Promise.all([
...fileStuffs.map(
maybe (
Promise.resolve (Left ('someone didn\'t have a title'))
) (
({title, fileName}) =>
writeFileIfDoesntExist(`${outDir}/${fileName}.md`)(emptyFileWithTitle(title))
)
),
writeFilePromised (`${outDir}/${bibFileTitle.replace(/\s+/g, '_').toLowerCase()}.md`) (bibData)
]);
};
const main = ({outDir, apiKey, userId, bibFileTitle = 'Zotero Bibliography'}) =>
fetch(`https://api.zotero.org/users/${userId}/items`, {
method: 'GET',
headers: { 'Zotero-API-Key': apiKey },
})
.then(r => r.json())
.then(map (({data}) => makeName (data)))
.then(writeFiles ({outDir, bibFileTitle}))
;
const {API_KEY: apiKey, USER_ID: userId, BIB_FILE_TITLE: bibFileTitle} = process.env;
const outDir = process.argv.pop();
if (!apiKey || !userId || !outDir || outDir.includes('index')) {
console.error('something\'s wrong here.', {apiKey, userId, outDir});
return;
};
fs.stat(outDir, err => {
if (err) {
console.error(`${outDir} isn't a directory. please specify a directory to write to.`);
return;
}
main ({outDir: outDir.endsWith('/') ? outDir.slice(0, -1) : outDir, apiKey, userId, bibFileTitle})
.then(console.log);
});