-
-
Save christophernarciso/4c611c037df60d2cecb4998e7a2e2a9b to your computer and use it in GitHub Desktop.
const configData = require('./config'); | |
const webhook = require('webhook-discord'); | |
const cloudscraper = require('cloudscraper'); | |
const JSSoup = require('jssoup').default; | |
const Hook = new webhook.Webhook(configData.webhook); | |
const ENDING = 'page/5000/'; | |
const THREADS = configData.threads; | |
const DATE_CACHE = configData.cache; | |
const getRandomColor = () => { | |
return '#' + Math.floor(Math.random() * 16777215).toString(16); | |
}; | |
const timeout = millis => new Promise(resolve => setTimeout(resolve, millis)); | |
const sendMessage = (script, thread, date, author, message) => { | |
if (date === null) { | |
const messageBuilder = new webhook.MessageBuilder() | |
.setName(configData.botname) | |
.setColor(getRandomColor()) | |
.addField('Date', new Date().toString()) | |
.addField('Author', author) | |
.addField('Message', message); | |
return Hook.send(messageBuilder); | |
} | |
const messageBuilder = new webhook.MessageBuilder() | |
.setName(configData.botname) | |
.setColor(getRandomColor()) | |
.addField('Topic', script) | |
.addField('Thread Link', thread) | |
.addField('Author', author) | |
.addField('Comment', message) | |
.addField('Date', new Date(date).toString()); | |
Hook.send(messageBuilder); | |
}; | |
(async function main() { | |
sendMessage(null, null, null, configData.name, configData.start.concat(` version: ${configData.version}`, ` timeout(ms): ${configData.timeout}`)); | |
while (true) { | |
for (idx in THREADS) { | |
const url = THREADS[idx].concat(ENDING); | |
const source = await cloudscraper.get(url); | |
if (source) { | |
const soup = new JSSoup(source); | |
const t = soup.find('script', {'type': 'application/ld+json'}).text; | |
const json = JSON.parse(t); | |
const scriptName = json['name']; | |
const comments = json['comment']; | |
console.log(`Loading comments for ${scriptName}`); | |
comments.forEach(function (data) { | |
const date = new Date(data['dateCreated']).getTime(); | |
const author = data['author'].name; | |
const thread = data['url']; | |
const message = data['text']; | |
if (author === configData.name) | |
return; | |
if (date > DATE_CACHE[scriptName]) { | |
console.log(`Found a recent comment by user ${author} on topic [${scriptName}][${date}].`); | |
DATE_CACHE[scriptName] = date; | |
sendMessage(scriptName, thread, date, author, message); | |
} | |
}); | |
} | |
} | |
// Delay next thread check | |
console.log('Waiting a bit for next cycle'); | |
await timeout(120000); | |
} | |
})(); |
{
"_comment0": "Please edit name, botname, start, timeout, webook",
"name": "Chris",
"botname": "CWS",
"about": "Decided to create this with inspiration from [email protected] python src.",
"start": "Starting comment watcher support",
"timeout": 120000,
"version": 1,
"webhook": "WEBHOOK_URL_HERE",
"_comment1": "threads... The list of threads to use",
"threads": [
"https://osbot.org/forum/topic/157064-excellent-vorkath/",
"https://osbot.org/forum/topic/104463-excellent-dragons/",
"https://osbot.org/forum/topic/125680-excellent-agility/",
"https://osbot.org/forum/topic/155717-legendary-lava-dragons/"
],
"_comment2": "cache key:value... The key should be the thread title. The value will default to today at 10am ish EST unless you change it by new Date().getTime() in your browser console",
"cache": {
"Excellent Vorkath": 1588170081901,
"Excellent Agility": 1588170081901,
"Excellent Dragons": 1588170081901,
"Legendary Lava Dragons": 1588170081901
}
}
config.json
{ "_comment0": "Please edit name, botname, start, timeout, webook", "name": "Chris", "botname": "CWS", "about": "Decided to create this with inspiration from [email protected] python src.", "start": "Starting comment watcher support", "timeout": 120000, "version": 1, "webhook": "WEBHOOK_URL_HERE", "_comment1": "threads... The list of threads to use", "threads": [ "https://osbot.org/forum/topic/157064-excellent-vorkath/", "https://osbot.org/forum/topic/104463-excellent-dragons/", "https://osbot.org/forum/topic/125680-excellent-agility/", "https://osbot.org/forum/topic/155717-legendary-lava-dragons/" ], "_comment2": "cache key:value... The key should be the thread title. The value will default to today at 10am ish EST unless you change it by new Date().getTime() in your browser console", "cache": { "Excellent Vorkath": 1588170081901, "Excellent Agility": 1588170081901, "Excellent Dragons": 1588170081901, "Legendary Lava Dragons": 1588170081901 } }