forked from eknkc/alfred-pinboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
232 lines (200 loc) · 6.87 KB
/
search.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
var spawn = require("child_process").spawn;
var argv = require('optimist').argv;
argv.q = argv.q || "";
var async = require('async');
var request = require('request');
var fs = require('fs');
var natural = require('natural');
natural.PorterStemmer.attach();
var mkdirp = require('mkdirp');
var path = require('path');
var xml = require("xml-writer");
var out = new xml(false, function (str, encoding) {
process.stdout.write(str, encoding);
});
async.auto({
configFile: function (next) {
var filename = path.join(process.env.HOME, "/Library/Application Support/Alfred 2/Workflow Data/eknkc.pinboard/config.json");
mkdirp(path.dirname(filename), function (err) {
if (err) return next(err);
next(null, filename);
});
},
cacheFile: function(next) {
var filename = path.join(process.env.HOME, "/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/eknkc.pinboard/bookmarks.json");
mkdirp(path.dirname(filename), function (err) {
if (err) return next(err);
next(null, filename);
});
},
config: ["configFile", function (next, data) {
fs.exists(data.configFile, function (exists) {
if (!exists)
return process.nextTick(function() { next(null, {}); });
fs.readFile(data.configFile, "utf8", function (err, filedata) {
if (err) return next(err);
next(null, JSON.parse(filedata));
});
})
}],
cache: ["cacheFile", function (next, data) {
fs.exists(data.cacheFile, function (exists) {
if (!exists)
return process.nextTick(function() { next(null, {}); });
fs.readFile(data.cacheFile, function (err, data) {
if (err) return next(err);
next(null, JSON.parse(data));
});
})
}],
checkAuth: ["config", "cache", function (next, data) {
if (argv.pbtoken && /^[^:]+:[0-9A-Z]+$/.test(argv.pbtoken)) {
data.config.token = argv.pbtoken;
data.config.dirty = true;
data.cache = {
dirty: true
};
return process.nextTick(function() { next(null, "new"); });
}
if (!data.config.token)
return process.nextTick(function() { next(new Error("NoAuthentication")); });
return process.nextTick(function() { next(null, true); });
}],
ensureData: ["checkAuth", "cache", function (next, data) {
if (argv.reindex) {
data.cache = { dirty: true };
}
if (data.cache && data.cache.entries)
return process.nextTick(next);
request({
method: "GET",
url: "https://api.pinboard.in/v1/posts/all",
qs: { auth_token: data.config.token, format: "json" }
}, function (err, res, body) {
if (err) return next(err);
if (res.statusCode == 401) return next(new Error("NoAuthentication"));
if (res.statusCode != 200) return next(new Error("UnknownError"));
data.cache = {
entries: JSON.parse(body),
dirty: true,
date: Date.now()
};
data.cache.entries.forEach(function (data) {
data.keywords = [data.description, data.extended, data.tags].join(" ").tokenizeAndStem();
});
next();
});
}],
search: ["ensureData", function (next, data) {
var keywords = argv.q.tokenizeAndStem().map(function (kw) {
return new RegExp(kw.replace(/(?=[\\^$*+?.()|{}[\]])/g, "\\"), "gi");
});
if (!keywords.length)
return process.nextTick(function() { next(null, []); });
var results = data.cache.entries
.map(function (entry) {
var matches = 1;
keywords.forEach(function (kw) {
matches *= entry.keywords.filter(function (ekw) {
return kw.test(ekw);
}).length;
});
return {
entry: entry,
matches: matches
}
})
.filter(function (data) {
return data.matches;
});
results.sort(function (a, b) {
return b.matches - a.matches;
});
next(null, results.slice(0, 8));
}],
unread: ["ensureData", function(next, data) {
if (!argv.toread)
return process.nextTick(function() { next(null, []); });
var results = data.cache.entries
.filter(function (data) {
return data.toread == "yes";
});
results.sort(function (a, b) {
return b.date - a.date;
});
next(null, results.slice(0, 8).map(function (data) {
return {
entry: data
}
}));
}],
results: ["unread", "search", function (next, data) {
return process.nextTick(function() { next(null, data.unread.concat(data.search)); });
}]
}, function (err, data) {
if (!data.checkReindex && data.cache && data.cache.date && data.cache.date < (Date.now() - 1000 * 60 * 10)) {
data.spawn = true;
data.cache = data.cache || {};
data.cache.date = Date.now();
data.cache.dirty = true;
}
if (data.config && data.configFile && data.config.dirty) {
delete data.config.dirty;
fs.writeFileSync(data.configFile, JSON.stringify(data.config), "utf8");
}
if (data.cache && data.cacheFile && data.cache.dirty) {
delete data.cache.dirty;
fs.writeFileSync(data.cacheFile, JSON.stringify(data.cache), "utf8");
}
out.startDocument();
out.startElement("items");
if (err && err.message == "NoAuthentication") {
out.startElement("item")
.writeAttribute('uid', "notoken")
.writeAttribute('valid', "no")
.writeElement('icon', "icon.png")
.writeElement("title", "Please set your authentication token")
.writeElement("subtitle", "You can use <pinboardauth TOKEN> to set your auth token.");
out.endElement();
} else if (err) {
out.startElement("item")
.writeAttribute('uid', "error")
.writeAttribute('valid', "no")
.writeElement('icon', "icon.png")
.writeElement("title", "Pinboard Search Error:")
.writeElement("subtitle", err.message);
out.endElement();
} else if (!data.results || !data.results.length) {
out.startElement("item")
.writeAttribute('uid', "noresults");
if (!argv.toread)
out.writeAttribute('arg', "https://pinboard.in/search/?query=" + encodeURIComponent(argv.q));
else
out.writeAttribute('valid', "no");
out.writeElement('icon', "icon.png")
.writeElement("title", "No Results");
if (!argv.toread)
out.writeElement("subtitle", "No bookmarks found. Search Pinboard wesite for: " + argv.q);
else
out.writeElement("subtitle", "You have no unread bookmarks.");
out.endElement();
} else {
data.results.forEach(function (data) {
var entry = data.entry;
out.startElement("item")
.writeAttribute('uid', entry.hash)
.writeAttribute('arg', entry.href)
.writeElement('icon', "icon.png")
.writeElement("title", entry.description)
.writeElement("subtitle", entry.extended || entry.href);
out.endElement();
});
}
out.endElement("items");
out.endDocument();
if (data.spawn) {
console.log("REINDEX")
spawn(process.execPath, [__filename, "--reindex"], { detached: true, stdio: "ignore" });
}
process.exit(0);
});