diff --git a/commands/feed-predef.js b/commands/feed-predef.js index f76aab0..d255437 100644 --- a/commands/feed-predef.js +++ b/commands/feed-predef.js @@ -25,19 +25,21 @@ function consoleLog(log) { async function sendUpstream(content) { var output = content.join("\n") - consoleLog('[twitter] All done.') + consoleLog('[feed-predef] All done.') parentPort.postMessage(output); process.exit() } function errorMessage(error, code, extra) { - consoleLog('[twitter.errorMessage] '+error.code) + consoleLog('[feed-predef.errorMessage] '+error.code) if (code == "404") { var error = errorMsg+" 404: " + extra + " not found" } else if (error.code == "ECONNREFUSED") { var error = errorMsg+" Connection Refused" } else if (error.code == "ERR_UNESCAPED_CHARACTERS"){ var error = errorMsg+" Unescaped Characters" + } else if (error.code == "INVALIDTYPE") { + var error = errorMsg+' \"'+extra+'\" is not a valid type' } else { var error = errorMsg+" Unknown error" } @@ -48,10 +50,10 @@ function errorMessage(error, code, extra) { async function twitter(feedURL, n) { var content = []; - consoleLog('[twitter.fetchFeed] fetching @'+feedURL) + consoleLog('[feed-predef.twitter] fetching @'+feedURL) if (feedURL.startsWith('@') == true) { - consoleLog('[twitter.fetchFeed] User passed @ in input, removing') + consoleLog('[feed-predef.twitter] User passed @ in input, removing') var feedURL = feedURL.substring(1,feedURL.length); } var randomNitter = config.twitter.nitter_instances[Math.floor(Math.random() * config.twitter.nitter_instances.length)]; @@ -114,9 +116,82 @@ async function twitter(feedURL, n) { sendUpstream(content); } +async function github(user, repo, type, n) { + var content = []; + var validTypes = ['commits', 'releases'] + if ( validTypes.includes(type) == false ) { + errorMessage(e, "INVALIDTYPE", type); + } + consoleLog('[feed-predef.github] fetching '+user+'/'+repo+' '+type) + + var feedURL = "https://github.com/"+user+"/"+repo+"/"+type+".atom" + try { + var newFeed = await parser.parseURL(feedURL); + } catch (e) { + if (e.code !== undefined) { + errorMessage(e) + } else { + errorMessage(e, "404", feedURL); + } + } + console.log(newFeed.items[1]) + if (n > newFeed.items.length) { + var n = newFeed.items.length; + content.push(warningMsg+" Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length); + } else if (n < 1) { + var n = config.twitter.default_amount + content.push(warningMsg+" You requested a number less than 1. Reverting to default ("+config.twitter.default_amount+")"); + } + + for (let i = 0; i < n; i++) { + + await timer(50); + + var data = newFeed.items[i] + var title = data.title.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks + .replace(/\s{2,}/g, ' ') //idk + var title = striptags(title); + var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks + .replace(/\s{2,}/g, ' ') //idk + var body = striptags(body); + + if (data.isoDate !== undefined) { + var date = moment(data.isoDate) + var syncDate = date.tz(config.feed.timezone) + console.log(syncDate.format()) + var date = syncDate.format(config.feed.time_format) + } else { + var date = data.pubDate + } + + if (body.length >= config.feed.body_max_chars) { + var truncatedString = body.substring(0,config.feed.body_max_chars); + var body = truncatedString + "..." + } + + var date = ''+config.colours.brackets+'['+config.colours.date+date+''+config.colours.brackets+'] ' + var author = ''+config.colours.author+data.author+' ' + var body = ''+config.colours.body+title.substring(1)+' ' + var link = ''+config.colours.link+data.link+' ' + + //console.log(data); + //var string = "15[11" + date + "15] 08" + data.creator + " " + body + " " + data.link; + var string = date+author+body+link + content.push(string) + } + sendUpstream(content); +} + var provfeed = provfeed.toLowerCase().split("/") if (provfeed[0] == "twitter") { twitter(provfeed[1], n); +} else if (provfeed[0] == "github") { + if (provfeed[3] == undefined) { + var type = "commits" + } else { + var type = provfeed[3] + } + github(provfeed[1], provfeed[2], type, n) }