76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
|
|
||
|
import { simpleParser } from 'mailparser';
|
||
|
import { load } from 'cheerio'
|
||
|
|
||
|
const definitions = [
|
||
|
{
|
||
|
platform: 'Chaturbate',
|
||
|
selectors: {
|
||
|
channel: 'td[id*="onlinemessage"] a:nth-child(1)'
|
||
|
},
|
||
|
from: 'follownotify@bk.chaturbate.com',
|
||
|
template: 'https://chaturbate.com/:channel'
|
||
|
},
|
||
|
{
|
||
|
platform: 'Fansly',
|
||
|
selectors: {
|
||
|
url: ($) => $("a[href*='/live/']").attr('href'),
|
||
|
displayName: 'div[class*="message-col"] div:nth-child(5)'
|
||
|
},
|
||
|
from: 'no-reply@fansly.com',
|
||
|
template: 'https://fansly.com/live/:channel',
|
||
|
regex: /https:\/\/fansly.com\/live\/([a-zA-Z0-9_]+)/
|
||
|
}
|
||
|
]
|
||
|
|
||
|
function render(template, values) {
|
||
|
// console.log(`values=${values}`)
|
||
|
// console.log(values)
|
||
|
return template.replace(/:([a-zA-Z0-9_]+)/g, (match, key) => {
|
||
|
// Replace :channel with the corresponding property from the values object
|
||
|
return values[key] || match;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* checkEmail
|
||
|
*
|
||
|
* Check an e-mail for go-live notification content.
|
||
|
*
|
||
|
* @param {String} body -- raw mail body
|
||
|
* @returns {Object} result
|
||
|
* @returns {Boolean} result.isMatch true if e-mail contains a go-live notification
|
||
|
* @returns {String} result.channel
|
||
|
*/
|
||
|
export async function checkEmail (body) {
|
||
|
|
||
|
const mail = await simpleParser(body)
|
||
|
|
||
|
|
||
|
let res = {}
|
||
|
let def = definitions.find((def) => def.from === mail.from.value[0].address)
|
||
|
if (!def) return { isMatch: false, channel: null, platform: null, url: null };
|
||
|
res.isMatch = true
|
||
|
|
||
|
// Step 0, get values from e-mail metadata
|
||
|
res.platform = def.platform
|
||
|
res.date = new Date(mail.date).toISOString()
|
||
|
|
||
|
// Step 1, get values using CSS selectors
|
||
|
const $ = load(mail.html)
|
||
|
for (const s in def.selectors) {
|
||
|
res[s] = (def.selectors[s] instanceof Object) ? def.selectors[s]($) : $(def.selectors[s]).text()
|
||
|
}
|
||
|
|
||
|
// Step 2, get values using regex & templates
|
||
|
res.channel = (() => {
|
||
|
if (res.channel) return res.channel;
|
||
|
if (def.regex && res.url) return def.regex.exec(res.url).at(1);
|
||
|
})()
|
||
|
|
||
|
res.url = res.url || render(def.template, {channel: res.channel})
|
||
|
|
||
|
|
||
|
return res
|
||
|
}
|