-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomNote.js
More file actions
103 lines (93 loc) · 3.9 KB
/
randomNote.js
File metadata and controls
103 lines (93 loc) · 3.9 KB
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
var Evernote = require('evernote').Evernote;
var config = require('./config.json');
var userConfig = require('./userConfig.json');
const opn = require('opn');
process.env.NODE_CONFIG_DIR = "./";
const configHelper = require('config');
var openRandomNote = function (userData) {
var token = userData.oauthAccessToken;
var shard = userData.edamShard;
var userId = userData.edamUserId;
var client = new Evernote.Client({
token: token,
sandbox: config.SANDBOX,
china: config.CHINA
});
client.getNoteStore().listNotebooks(function (err, notebooks) {
if (err) {
console.log(err);
throw err;
}
if (configHelper.has("stacks")) {
userConfig.stacks = configHelper.get("stacks");
}
var notebooksFromConfiguredStacks = notebooks.filter(function (notebook) {
for (var i = 0; i < userConfig.stacks.length; ++i) {
if (notebook.stack === userConfig.stacks[i]) {
return true;
}
}
return false;
});
client.getNoteStore().findNoteCounts(new Evernote.NoteFilter({}), false, function (err, noteCollectionCounts) {
if (err) {
console.log(err);
throw err;
}
var totalNoteCount = 0;
var nonEmptyNotebooks = notebooksFromConfiguredStacks.filter(function (notebook) {
var notebookCount = noteCollectionCounts.notebookCounts[notebook.guid];
if (notebookCount && notebookCount > 0) {
notebook.noteCount = notebookCount;
totalNoteCount += notebookCount;
return true;
}
return false;
});
var randomNoteIndex = getRandomInt(0, totalNoteCount);
var correctNotebook = null;
var noteCounterToFindCorrectNotebook = 0;
for (var i = 0; i < nonEmptyNotebooks.length; ++i) {
noteCounterToFindCorrectNotebook += nonEmptyNotebooks[i].noteCount;
if (noteCounterToFindCorrectNotebook >= randomNoteIndex) {
correctNotebook = nonEmptyNotebooks[i];
break;
}
}
if (correctNotebook == null) {
var err = "Not able to find a non-empty notebook within those stacks";
throw err;
}
var spec = new Evernote.NotesMetadataResultSpec({
includeTitle: false,
includeContentLength: false,
includeCreated: false,
includeUpdated: false,
includeDeleted: false,
includeUpdateSequenceNum: false,
includeNotebookGuid: false,
includeTagGuids: false,
includeAttributes: false,
includeLargestResourceMime: false,
includeLargestResourceSize: false,
});
var randomNoteWithinCorrectNotebookIndex = getRandomInt(0, noteCounterToFindCorrectNotebook-randomNoteIndex);
var filter = new Evernote.NoteFilter({
notebookGuid: correctNotebook.guid
});
client.getNoteStore().findNotesMetadata(filter, randomNoteWithinCorrectNotebookIndex, 1, spec, function (err, notesMetaDataList) {
if (err) {
console.log(err);
throw err;
}
opn("evernote:///view/" + userId + "/" + shard + "/" + notesMetaDataList.notes[0].guid + "/" + notesMetaDataList.notes[0].guid + "/");
});
});
});
}
module.exports = openRandomNote;
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}