Rename libFlayer to libCore
This commit is contained in:
@@ -2,10 +2,9 @@
|
||||
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');
|
||||
const storageHandler = require("./libStorage");
|
||||
|
||||
/* OpenAI config
|
||||
const { Configuration, OpenAIApi } = require('openai');
|
||||
const configuration = new Configuration({
|
||||
organization: process.env.OPENAI_ORG,
|
||||
@@ -13,38 +12,21 @@ const configuration = new Configuration({
|
||||
});
|
||||
|
||||
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 stockKey = process.env.STOCK_KEY;
|
||||
let rssFeedMap = [];
|
||||
let rssFeedCategories = [];
|
||||
|
||||
// Precess Vars
|
||||
var userTable = process.env.DB_TABLE
|
||||
token = process.env.TOKEN;
|
||||
|
||||
// Setup Storage handler
|
||||
var base = new storageHandler.Storage(userTable);
|
||||
|
||||
//DuckDuckGo related structure
|
||||
let answerData = {
|
||||
text: ``,
|
||||
source: ``
|
||||
}
|
||||
|
||||
//Local DB for quotes
|
||||
const {
|
||||
quotes
|
||||
} = require('./quotes.json');
|
||||
var storageSource = new storageHandler.Storage(userTable);
|
||||
|
||||
/**
|
||||
* Adds a new source url to configured Airtable
|
||||
* Adds a new source url to configured storage
|
||||
* @constructor
|
||||
* @param {string} title - Title/Name of the RSS feed.
|
||||
* @param {string} link - URL of RSS feed.
|
||||
@@ -58,7 +40,7 @@ exports.addSource = function (title, link, category, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
base.create([{
|
||||
storageSource.create([{
|
||||
"fields": {
|
||||
"title": title,
|
||||
"link": link,
|
||||
@@ -90,7 +72,7 @@ exports.addSource = function (title, link, category, callback) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a new source url to configured Airtable by title
|
||||
* Deletes a new source url by title
|
||||
* @constructor
|
||||
* @param {string} title - Title/Name of the RSS feed.
|
||||
*/
|
||||
@@ -101,7 +83,7 @@ exports.deleteSource = function (title, callback) {
|
||||
deleteRecord = feeds[i].id;
|
||||
}
|
||||
}
|
||||
base.destroy(deleteRecord, function (err, deletedRecord) {
|
||||
storageSource.destroy(deleteRecord, function (err, deletedRecord) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
callback(err, undefined);
|
||||
@@ -112,17 +94,17 @@ exports.deleteSource = function (title, callback) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new source url to configured Airtable
|
||||
* Adds a new source url to configured storage
|
||||
* @constructor
|
||||
* @param {string} feedType - Category to select Feed by.
|
||||
*/
|
||||
exports.getFeeds = function (feedType) {
|
||||
var linkFlayerFilteredMap = [];
|
||||
if (feedType == null || feedType == undefined || feedType == "") {
|
||||
return linkFlayerMap;
|
||||
return rssFeedMap;
|
||||
} else {
|
||||
|
||||
linkFlayerMap.forEach(linkFlay => {
|
||||
rssFeedMap.forEach(linkFlay => {
|
||||
if (linkFlay.category.toLowerCase().indexOf(feedType.toLowerCase()) > -1) {
|
||||
linkFlayerFilteredMap.push(linkFlay);
|
||||
}
|
||||
@@ -131,12 +113,15 @@ exports.getFeeds = function (feedType) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the RSS feeds
|
||||
*/
|
||||
exports.loadFeeds = function () {
|
||||
feeds = [];
|
||||
linkFlayerMap = [];
|
||||
linkFlayerCats = [];
|
||||
rssFeedMap = [];
|
||||
rssFeedCategories = [];
|
||||
|
||||
base.getAllRecords(function (err, records) {
|
||||
storageSource.getAllRecords(function (err, records) {
|
||||
records.forEach(function (record) {
|
||||
try {
|
||||
console.log('Retrieved title: ', record.get('title'));
|
||||
@@ -148,9 +133,7 @@ exports.loadFeeds = function () {
|
||||
link: `${unescape(record.get('link'))}`,
|
||||
category: `${unescape(record.get('category'))}`,
|
||||
id: record.getId()
|
||||
}
|
||||
|
||||
console.log("Feed Data:", feedData);
|
||||
}
|
||||
|
||||
var foundMatch = false;
|
||||
feeds.forEach(feedBlock => {
|
||||
@@ -164,14 +147,14 @@ exports.loadFeeds = function () {
|
||||
}
|
||||
|
||||
let foundCat = false;
|
||||
linkFlayerCats.forEach(cat => {
|
||||
rssFeedCategories.forEach(cat => {
|
||||
if (cat == record.get('category')) {
|
||||
foundCat = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!foundCat) {
|
||||
linkFlayerCats.push(record.get('category'));
|
||||
rssFeedCategories.push(record.get('category'));
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -186,12 +169,12 @@ exports.loadFeeds = function () {
|
||||
if (err) {
|
||||
console.log(err + " " + feedBlock.link);
|
||||
//return;
|
||||
}
|
||||
}
|
||||
|
||||
if (feed != undefined && feed.items != undefined) {
|
||||
feed.items.forEach(item => {
|
||||
var foundFeed = false;
|
||||
linkFlayerMap.forEach(linkFlay => {
|
||||
rssFeedMap.forEach(linkFlay => {
|
||||
if (linkFlay.link == item.link) {
|
||||
foundFeed = true;
|
||||
}
|
||||
@@ -203,7 +186,7 @@ exports.loadFeeds = function () {
|
||||
link: `${unescape(item.link)}`,
|
||||
category: `${unescape(feedBlock.category)}`
|
||||
}
|
||||
linkFlayerMap.push(linkData);
|
||||
rssFeedMap.push(linkData);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -224,7 +207,12 @@ exports.loadFeeds = function () {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Search a state for any weather alerts
|
||||
*
|
||||
* @param {*} state The state to search for any weather alerts in
|
||||
* @returns
|
||||
*/
|
||||
exports.weatherAlert = async function (state) {
|
||||
|
||||
var answerURL = `https://api.weather.gov/alerts/active?area=${state}`;
|
||||
@@ -245,6 +233,11 @@ exports.weatherAlert = async function (state) {
|
||||
return answerData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a random food recipe
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
exports.getFood = async function () {
|
||||
|
||||
var answerURL = `https://www.themealdb.com/api/json/v1/1/random.php`;
|
||||
@@ -286,38 +279,12 @@ exports.getFood = async function () {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Urban dictionary for a phrase
|
||||
*
|
||||
* @param {*} question The phrase to search urban dictionary for
|
||||
* @returns
|
||||
*/
|
||||
exports.getSlang = async function (question) {
|
||||
|
||||
var answerURL = `https://api.urbandictionary.com/v0/define?term=${question}`;
|
||||
@@ -345,106 +312,19 @@ exports.getSlang = async function (question) {
|
||||
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
|
||||
* Use ChatGPT to generate a response
|
||||
*
|
||||
* @param {*} _prompt The use submitted text prompt
|
||||
* @param {*} param1 Default parameters can be modified
|
||||
* @returns
|
||||
*/
|
||||
|
||||
exports.getChat = async function (question) {
|
||||
exports.getChat = async function (_prompt, { _model = "text-davinci-003", _temperature = 0, _max_tokens = 100 }) {
|
||||
const response = await openai.createCompletion({
|
||||
model: "text-davinci-003",
|
||||
prompt: question,
|
||||
temperature: 0,
|
||||
max_tokens: 100
|
||||
model: _model,
|
||||
prompt: _prompt,
|
||||
temperature: _temperature,
|
||||
max_tokens: _max_tokens
|
||||
});
|
||||
|
||||
|
||||
@@ -452,9 +332,8 @@ exports.getChat = async function (question) {
|
||||
return responseData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getSources - Returns libFlayer feed sources
|
||||
* getSources - Get the RSS sources currently in use
|
||||
* @constructor
|
||||
*/
|
||||
exports.getSources = function () {
|
||||
@@ -462,7 +341,7 @@ exports.getSources = function () {
|
||||
}
|
||||
|
||||
/**
|
||||
* getQuotes - Returns libFlayer feed quotes
|
||||
* getQuotes - Get a random quote from the local list
|
||||
* @constructor
|
||||
*/
|
||||
exports.getQuotes = async function (quote_url) {
|
||||
@@ -483,17 +362,9 @@ exports.getQuotes = async function (quote_url) {
|
||||
}
|
||||
|
||||
/**
|
||||
* getCategories - Returns libFlayer feed categories
|
||||
* getCategories - Returns feed categories
|
||||
* @constructor
|
||||
*/
|
||||
exports.getCategories = function () {
|
||||
return linkFlayerCats;
|
||||
}
|
||||
|
||||
/**
|
||||
* sleep - sleep/wait
|
||||
* @constructor
|
||||
*/
|
||||
exports.sleep = (ms) => new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
})
|
||||
return rssFeedCategories;
|
||||
}
|
||||
Reference in New Issue
Block a user