-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
libFlayer.js
494 lines (418 loc) · 12.2 KB
/
libFlayer.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// JD Requires
let Parser = require('rss-parser');
const axios = require('axios');
let parser = new Parser();
var jsonfile = require('jsonfile');
var fs = require('fs');
var file = ('./feeds.json');
var Airtable = require('airtable');
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
organization: process.env.OPENAI_ORG,
apiKey: process.env.OPENAI_API
});
const openai = new OpenAIApi(configuration);
// Data Structures
var feeds = [];
let linkFlayerMap = [];
let linkFlayerCats = [];
//Aitrable Setup
var apiKey = process.env.KEY;
var userBase = process.env.BASE;
var userTable = process.env.TABLE
var stockKey = process.env.STOCK_KEY;
token = process.env.TOKEN;
var base = new Airtable({
apiKey: apiKey
}).base(userBase);
//DuckDuckGo related structure
let answerData = {
text: ``,
source: ``
}
//Local DB for quotes
const {
quotes
} = require('./quotes.json');
/**
* Adds a new source url to configured Airtable
* @constructor
* @param {string} title - Title/Name of the RSS feed.
* @param {string} link - URL of RSS feed.
* @param {string} category - Category of RSS feed.
*/
exports.addSource = function (title, link, category) {
for (i = 0; i < feeds.length; i++) {
if (feeds[i].link == link) {
return;
}
}
base(userTable).create([{
"fields": {
"title": title,
"link": link,
"category": category
}
}], function (err, record) {
if (err) {
console.error(err);
return;
}
console.log(record.getId());
var linkData = {
title: `${title}`,
link: `${link}`,
category: `${category}`,
id: record.getId()
}
feeds.push(linkData);
});
}
/**
* Deletes a new source url to configured Airtable by title
* @constructor
* @param {string} title - Title/Name of the RSS feed.
*/
exports.deleteSource = function (title) {
var deleteRecord = "";
for (i = 0; i < feeds.length; i++) {
if (feeds[i].title == title) {
deleteRecord = feeds[i].id;
}
}
base(userTable).destroy(deleteRecord, function (err, deletedRecord) {
if (err) {
console.error(err);
return;
}
console.log(deletedRecord.id);
});
}
/**
* Adds a new source url to configured Airtable
* @constructor
* @param {string} feedType - Category to select Feed by.
*/
exports.getFeeds = function (feedType) {
var linkFlayerFilteredMap = [];
if (feedType == null || feedType == undefined || feedType == "") {
return linkFlayerMap;
} else {
linkFlayerMap.forEach(linkFlay => {
if (linkFlay.category.toLowerCase().indexOf(feedType.toLowerCase()) > -1) {
linkFlayerFilteredMap.push(linkFlay);
}
});
return linkFlayerFilteredMap;
}
}
exports.loadFeeds = function () {
feeds = [];
linkFlayerMap = [];
linkFlayerCats = [];
base(userTable)
.select().firstPage(function (err, records) {
records.forEach(function (record) {
try {
console.log('Retrieved title: ', record.get('title'));
console.log('Retrieved link:', record.get('link'));
console.log('Retrieved category:', record.get('category'));
var feedData = {
title: `${unescape(record.get('title'))}`,
link: `${unescape(record.get('link'))}`,
category: `${unescape(record.get('category'))}`,
id: record.getId()
}
var foundMatch = false;
feeds.forEach(feedBlock => {
if (feedBlock.link == feedData.link) {
foundMatch = true;
}
});
if (!foundMatch) {
feeds.push(feedData);
}
let foundCat = false;
linkFlayerCats.forEach(cat => {
if (cat == record.get('category')) {
foundCat = true;
}
});
if (!foundCat) {
linkFlayerCats.push(record.get('category'));
}
} catch (error) {
console.log(error);
}
});
feeds.forEach(feedBlock => {
(async () => {
try {
const feed = parser.parseURL(feedBlock.link, function (err, feed) {
if (err) {
console.log(err + " " + feedBlock.link);
//return;
}
if (feed != undefined && feed.items != undefined) {
feed.items.forEach(item => {
var foundFeed = false;
linkFlayerMap.forEach(linkFlay => {
if (linkFlay.link == item.link) {
foundFeed = true;
}
});
if (!foundFeed) {
var linkData = {
title: `${unescape(item.title)}`,
link: `${unescape(item.link)}`,
category: `${unescape(feedBlock.category)}`
}
linkFlayerMap.push(linkData);
}
});
} else {
console.log('error parsing :' + feedBlock.link);
}
})
} catch (error) {
console.log(error);
}
})().then();
});
return;
//fetchNextPage();
}, function done(error) {
console.log(error);
});
}
exports.weatherAlert = async function (state) {
var answerURL = `https://api.weather.gov/alerts/active?area=${state}`;
console.log(answerURL);
answerData = [];
await axios.get(answerURL)
.then(response => {
response.data.features.forEach(feature => {
answerData.push(feature);
})
return answerData;
})
.catch(error => {
console.log(error);
});
return answerData;
}
exports.getFood = async function () {
var answerURL = `https://www.themealdb.com/api/json/v1/1/random.php`;
console.log(answerURL);
answerData = {
text: `No answer found try using a simpler search term`,
source: ``
}
await axios.get(answerURL)
.then(response => {
//console.log(response.data.RelatedTopics[0].Text);
//console.log(response.data.RelatedTopics[0].FirstURL);
// if (response.data.meals.length != 0) {
answerData = {
strMeal: `No Data`,
strSource: `-`,
strInstructions: `-`,
strMealThumb: `-`,
strCategory: `-`
}
answerData = {
strMeal: `${unescape(response.data.meals[0].strMeal)}`,
strSource: `${unescape(response.data.meals[0].strSource)}`,
strInstructions: `${unescape(response.data.meals[0].strInstructions)}`,
strMealThumb: `${unescape(response.data.meals[0].strMealThumb)}`,
strCategory: `${unescape(response.data.meals[0].strCategory)}`
}
// } else {
//}
return answerData;
})
.catch(error => {
console.log(error);
});
return answerData;
}
exports.search = async function (question) {
var answerURL = `https://api.duckduckgo.com/?q=${question}&format=json&pretty=1`;
console.log(answerURL);
answerData = {
text: `No answer found try using a simpler search term`,
source: ``
}
await axios.get(answerURL)
.then(response => {
//console.log(response.data.RelatedTopics[0].Text);
//console.log(response.data.RelatedTopics[0].FirstURL);
if (response.data.Abstract != "") {
answerData = {
text: `${unescape(response.data.Abstract)}`,
source: `${unescape(response.data.AbstractSource)}`
}
} else {
answerData = {
text: `${unescape(response.data.RelatedTopics[0].Text)}`,
source: `${unescape(response.data.RelatedTopics[0].FirstURL)}`
}
}
return answerData;
})
.catch(error => {
console.log(error);
});
return answerData;
}
exports.getSlang = async function (question) {
var answerURL = `https://api.urbandictionary.com/v0/define?term=${question}`;
console.log(answerURL);
slangData = {
definition: `No answer found try using a simpler search term`,
example: ``
}
await axios.get(answerURL)
.then(response => {
console.log(response.data.list[0]);
slangData = {
definition: `${unescape(response.data.list[0].definition) ? unescape(response.data.list[0].definition) : ''}`,
example: `${unescape(response.data.list[0].example) ? unescape(response.data.list[0].example) : ''}`,
thumbs_down: `${unescape(response.data.list[0].thumbs_down)}`,
thumbs_up: `${unescape(response.data.list[0].thumbs_up)}`
}
return slangData;
})
.catch(error => {
console.log(error);
});
return slangData;
}
exports.getNPM = async function (question) {
var answerURL = `https://www.npmjs.com/search/suggestions?q=${question}`;
console.log(answerURL);
let returnData = [];
await axios.get(answerURL)
.then(response => {
console.log(response.data);
if (response.data.length != 0) {
response.data.forEach(npmResult => {
returnData.push(npmResult);
});
}
return returnData;
})
.catch(error => {
console.log(error);
});
return returnData;
}
exports.getCode = async function (question) {
var answerURL = `https://you.com/api/performSearch?q=${question}&page=1&count=10&safeSearch=Moderate&onShoppingPage=false&mkt=en-US&responseFilter=WebPages,Translations,TimeZone,Computation,RelatedSearches&domain=youcode`;
console.log(answerURL);
let returnData = [];
await axios.get(answerURL)
.then(response => {
console.log(response.data);
if (response.data.length != 0) {
response.data.searchResults.mainline.bing_search_results.forEach(codeResult => {
returnData.push(codeResult);
});
}
return returnData;
})
.catch(error => {
console.log(error);
});
return returnData;
}
exports.getStock = async function (stock) {
var answerURL = `https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${stock}&interval=5min&apikey=${stockKey}`;
console.log(answerURL);
stockData = {
symbol: `Not Found`,
open: ``,
high: ``,
low: ``,
price: ``,
volume: ``,
latest: ``,
previous: ``,
change: ``,
percent: ``
}
await axios.get(answerURL)
.then(response => {
//console.log(response.data.list[0]);
stockData = {
symbol: `${unescape(response.data["Global Quote"]['01. symbol'])}`,
open: `${unescape(response.data["Global Quote"]['02. open'])}`,
high: `${unescape(response.data["Global Quote"]['03. high'])}`,
low: `${unescape(response.data["Global Quote"]['04. low'])}`,
price: `${unescape(response.data["Global Quote"]['05. price'])}`,
volume: `${unescape(response.data["Global Quote"]['06. volume'])}`,
latest: `${unescape(response.data["Global Quote"]['07. latest trading day'])}`,
previous: `${unescape(response.data["Global Quote"]['08. previous close'])}`,
change: `${unescape(response.data["Global Quote"]['09. change'])}`,
percent: `${unescape(response.data["Global Quote"]['10. change percent'])}`
}
return stockData;
})
.catch(error => {
console.log(error);
});
return stockData;
}
/**
* getChat - Returns libFlayer feed sources
* @constructor
*/
exports.getChat = async function (question) {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: question,
temperature: 0,
max_tokens: 1000
});
var responseData = response.data.choices[0].text;
return responseData;
}
/**
* getSources - Returns libFlayer feed sources
* @constructor
*/
exports.getSources = function () {
return feeds;
}
/**
* getQuotes - Returns libFlayer feed quotes
* @constructor
*/
exports.getQuotes = async function (quote_url) {
var data = [];
await axios.get(quote_url)
.then(response => {
console.log(response.data[0].q);
console.log(response.data[0].a);
data = response.data;
return data;
})
.catch(error => {
console.log(error);
});
return data;
}
/**
* getCategories - Returns libFlayer feed categories
* @constructor
*/
exports.getCategories = function () {
return linkFlayerCats;
}
/**
* sleep - sleep/wait
* @constructor
*/
exports.sleep = (ms) => new Promise((resolve) => {
setTimeout(resolve, ms);
})