-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
256 lines (212 loc) · 7.07 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const express = require('express')
const app = express()
const port = 3000
const Zotero = require('libzotero');
const { exec } = require("child_process");
const fs = require('fs');
const glob = require("glob")
const globp = require("glob-promise")
let cred = JSON.parse(fs.readFileSync('zotero_credential.json'));
console.log(cred.key, cred.userid);
app.use(express.static("./"));
app.use("/pdf_slim_viewer", express.static("pdf_slim_viewer/"));
// Function to check if the real database has been updated
function needsUpdate(dbfile, copyDbFile) {
if (!fs.existsSync(copyDbFile)) return true;
const origStats = fs.statSync(dbfile);
const copyStats = fs.statSync(copyDbFile);
return origStats.mtime > copyStats.mtime || origStats.size !== copyStats.size;
}
app.get('/readdb', async (req, res) => {
let dbfile = "/Users/supasorn/Zotero/zotero.sqlite";
const copyDbFile = "/Users/supasorn/Zotero/zotero_copy.sqlite";
const sqlite3 = require('sqlite3').verbose();
// Make a copy if the original file has been updated
if (needsUpdate(dbfile, copyDbFile)) {
fs.copyFileSync(dbfile, copyDbFile);
console.log("Database copied to avoid lock issues.");
}
let db = new sqlite3.Database(copyDbFile, sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
});
const query = `
SELECT i.dateAdded, COALESCE(subitem.key, i.key) AS key, i.itemID,
(SELECT idv.value
FROM itemData id
JOIN itemDataValues idv ON id.valueID = idv.valueID
WHERE id.itemID = i.itemID
AND id.fieldID = 1
LIMIT 1
) AS title,
(
SELECT GROUP_CONCAT(fullName, '; ')
FROM (
SELECT c.firstName || ' ' || c.lastName AS fullName
FROM creators c
JOIN itemCreators ci ON c.creatorID = ci.creatorID
WHERE ci.itemID = i.itemID
ORDER BY ci.orderIndex
)
) AS authors,
(SELECT idv.value
FROM itemData id
JOIN itemDataValues idv ON id.valueID = idv.valueID
WHERE id.itemID = i.itemID
AND id.fieldID = 6
LIMIT 1
) AS pubdate
FROM items i
LEFT JOIN itemAttachments ia ON i.itemID = ia.parentItemID AND ia.contentType = 'application/pdf'
LEFT JOIN items subitem ON ia.itemID = subitem.itemID
LEFT JOIN deletedItems di ON i.itemID = di.itemID
WHERE
di.itemID IS NULL
AND NOT EXISTS (
SELECT 1
FROM groupItems gi
WHERE gi.itemID = i.itemID
)
AND (
i.itemID IN (
SELECT parentItemID
FROM itemAttachments
)
OR
i.itemID IN (
SELECT itemID
FROM itemAttachments
WHERE parentItemID IS NULL AND contentType = 'application/pdf'
)
)
ORDER BY i.dateAdded DESC;
`;
let output = "";
db.all(query, [], (err, rows) => {
if (err) {
throw err;
}
let json = JSON.stringify(rows);
console.log(json);
// fs.writeFileSync('sql.json', json);
res.send(json);
db.close();
});
});
app.get('/fetch_preview/:id', async (req, res) => {
const id = req.params.id;
const send_list = () => glob(`preview/${id}/small*`, (er, files) => { res.json(files) });
console.log(id);
try { // Preview exists
console.log("exists");
await fs.promises.access(`preview/${id}/small-01.jpg`);
send_list();
} catch (e) { // Preview doesn't exist
console.log("not exist");
const files = await globp(`${cred.path}/${id}/*.pdf`);
if (files.length == 0)
return res.sendStatus(404);
if (!fs.existsSync(`preview/${id}`))
await fs.promises.mkdir(`preview/${id}`);
const pdf = files[0];
exec(`pdftoppm -jpeg -r 50 -l 10 "${pdf}" ./preview/${id}/small`, (error, stdout, stderr) => { send_list(); });
}
})
app.get('/papers/:id/:name?', async (req, res) => {
const id = req.params.id;
//console.log(globp);
const files = await globp.promise(`${cred.path}${id}/*.pdf`);
if (files.length == 0)
return res.sendStatus(404);
try {
const data = await fs.promises.readFile(files[0]);
res.writeHead(200, { "Content-Type": "application/pdf" });
res.write(data);
res.end();
} catch (e) {
return res.sendStatus(404);
}
});
app.get('/test', (req, res) => {
console.log("in")
res.write("<iframe height='300px' src='http://localhost:3000/pdf_slim_viewer/web/viewer.html?file=http:/papers/975CNYDP/Object-Centric%20Learning%20with%20Slot%20Attention'>");
res.end();
});
app.get('/arxiv', (req, res) => {
const query = req.params.query;
console.log(query)
res.write("yes");
res.end();
});
app.get('/sync', async (req, res) => {
var config = new Zotero.RequestConfig();
config.Key(cred.key);
config.LibraryType('user').LibraryID(cred.userid);
config.Target('items').Limit(100);
const obj = await fs.promises.readFile('./results.json');
const json = JSON.parse(obj);
let maxversion = 0;
for (let item of json) {
if (item.version > maxversion)
maxversion = item.version;
}
console.log(maxversion)
config.config["since"] = maxversion;
var fetcher = new Zotero.Fetcher(config.config);
fetcher.fetchAll().then(() => {
let results = fetcher.results;
console.log(`\nthere are ${fetcher.totalResults} results available, and we've already gotten ${results.length}\n`);
for (let item of results) {
const found = json.findIndex(element => element.key == item.key)
if (found == -1) {
console.log(item.key, "not found");
json.push(item);
} else {
console.log(item.key, "found", found);
json[found] = item;
}
}
let data = JSON.stringify(json);
fs.writeFileSync('results.json', data);
res.redirect('/');
});
});
app.get('/fsync', async (req, res) => {
var config = new Zotero.RequestConfig();
config.Key(cred.key);
config.LibraryType('user').LibraryID(cred.userid);
config.Target('items').Limit(100);
var fetcher = new Zotero.Fetcher(config.config);
fetcher.fetchAll().then(() => {
let results = fetcher.results;
console.log(`\nthere are ${fetcher.totalResults} results available, and we've already gotten ${results.length}\n`);
let data = JSON.stringify(results);
fs.writeFileSync('results.json', data);
res.redirect('/');
});
});
app.get('/delete', async (req, res) => {
var config = new Zotero.RequestConfig();
config.Key(cred.key);
config.LibraryType('user').LibraryID(cred.userid);
//config.Target('items').TargetModifier("top").Limit(100);
config.Target('deleted');
config.config["since"] = 1000;
var fetcher = new Zotero.Fetcher(config.config);
fetcher.fetchAll().then(() => {
let results = fetcher.results;
console.log(`\nthere are ${fetcher.totalResults} results available, and we've already gotten ${results.length}\n`);
console.log(results)
}).catch(e => {
console.log(e);
});
});
app.get('/api/data', (req, res) => {
fs.readFile('./results.json', (err, json) => {
let obj = JSON.parse(json);
res.json(obj);
});
});
app.listen(port);