forked from shaoshing/alfred-evernote
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
93 lines (77 loc) · 2.92 KB
/
app.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
var MAX_RESULTS = 20;
var AlfredNode = require('alfred-workflow-nodejs'),
actionHandler = AlfredNode.actionHandler,
workflow = AlfredNode.workflow,
Item = AlfredNode.Item,
storage = AlfredNode.storage;
workflow.setName("Alfred/Evernote");
actionHandler.onAction("search", function(query) {
var fs = require('fs'),
devToken = storage.get("devToken");
if (!devToken) {
workflow.addItem(new Item({
title: 'Please run "es-token" to setup your Evernote Token.',
valid: false
}));
workflow.feedback();
return;
}
var Evernote = require('evernote').Evernote,
client = new Evernote.Client({token: devToken, sandbox: false, china: ture}),
userID = storage.get('userID'),
shardID = storage.get('shardID');
// Support Evernote Advanced Search Syntax
// https://help.evernote.com/hc/en-us/articles/208313828-How-to-use-Evernote-s-advanced-search-syntax
var filter = new Evernote.NoteFilter({ words: query, order: Evernote.NoteSortOrder.UPDATED });
client.getNoteStore().findNotes(devToken, filter, 0, MAX_RESULTS, function (err, result) {
if (err) {
workflow.addItem(new Item({
title: 'Hmm, unable to perform a search request. You may need a new token.',
valid: false
}));
workflow.feedback();
return;
}
result.notes.forEach(function (note) {
workflow.addItem(new Item({
uid: note.guid,
title: note.title,
arg: note.guid,
valid: true,
icon: 'note.png'
}));
})
workflow.feedback();
});
});
actionHandler.onAction("token", function(query) {
var Evernote = require('evernote').Evernote,
fs = require('fs'),
token = query,
client = new Evernote.Client({token: token, sandbox: false, china: true});
client.getUserStore().getUser(function (err, user) {
if (err) {
console.log("Oops, your token seems invalid.");
return;
}
storage.set('devToken', token);
storage.set('userID', user.id);
storage.set('shardID', user.shardId);
console.log("Hooray, your token has been accepted! Your user ID is %s.", storage.get('userID'));
});
});
actionHandler.onAction("get-link", function(linkType) {
var userID = storage.get('userID'),
shardID = storage.get('shardID'),
noteGUID = process.argv[process.argv.length - 1];
// https://dev.evernote.com/doc/articles/note_links.php
switch (linkType) {
case "app":
console.log("evernote:///view/%s/%s/%s/%s/", userID, shardID, noteGUID, noteGUID);
break;
case "www":
console.log("https://www.evernote.com/shard/%s/nl/%s/%s/", shardID, userID, noteGUID);
break;
}
});
AlfredNode.run();