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. * * { isMatch, url, platform, channel, displayName, date } * * @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.url example: https://fansly.com/projektmelody * @returns {String} result.platform example: fansly * @returns {String} result.channel example: projektmelody * @returns {String} result.displayName example: ProjektMelody * @returns {String} result.date example: 2024-05-31T01:02:00.000Z */ 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 }