add support for bloomberg newsletters

This commit is contained in:
Hongbo Wu 2022-02-17 13:19:58 +08:00
parent 89ae114820
commit bd8178a464
3 changed files with 46 additions and 3 deletions

View file

@ -0,0 +1,28 @@
import { DOMWindow } from 'jsdom'
export class SubstackHandler {
name = 'bloomberg'
shouldPrehandle = (url: URL, dom: DOMWindow): boolean => {
const host = this.name + '.com'
// check if url ends with bloomberg.com
return url.hostname.endsWith(host)
}
prehandle = (url: URL, dom: DOMWindow): Promise<DOMWindow> => {
const body = dom.document.querySelector('.wrapper')
// this removes header
body?.querySelector('.sailthru-variables')?.remove()
body?.querySelector('.preview-text')?.remove()
body?.querySelector('.logo-wrapper')?.remove()
body?.querySelector('.by-the-number-wrapper')?.remove()
// this removes footer
body?.querySelector('.quote-box-wrapper')?.remove()
body?.querySelector('.header-wrapper')?.remove()
body?.querySelector('.component-wrapper')?.remove()
body?.querySelector('.footer')?.remove()
return Promise.resolve(dom)
}
}

View file

@ -6,9 +6,11 @@ const EMAIL_CONFIRMATION_CODE_RECEIVED_TOPIC = 'emailConfirmationCodeReceived'
const EMAIL_FORWARDING_SENDER_ADDRESSES = [
'Gmail Team <forwarding-noreply@google.com>',
]
const NEWSLETTER_SENDER_REGEX = '<.+@axios.com>'
const NEWSLETTER_SENDER_REGEX =
'<.+@((axios.com)|(mail.bloombergbusiness.com))>'
const CONFIRMATION_CODE_PATTERN = '^\\(#\\d+\\)'
const AXIOS_URL_PATTERN = 'View in browser at <.+>'
const BLOOMBERG_URL_PATTERN = '<a class="view-in-browser__url".+>'
export const handleConfirmation = async (email: string, subject: string) => {
console.log('confirmation email')
@ -114,11 +116,19 @@ export const getNewsletterUrl = (
}
// axios newsletter url from html
const re = new RegExp(AXIOS_URL_PATTERN)
const matches = html.match(re)
let re = new RegExp(AXIOS_URL_PATTERN)
let matches = html.match(re)
if (matches) {
const match = matches[0]
return match.slice(match.indexOf('>') + 1, match.lastIndexOf('<'))
}
// bloomberg newsletter url from html
re = new RegExp(BLOOMBERG_URL_PATTERN)
matches = html.match(re)
if (matches) {
const match = matches[0]
return match.slice(match.indexOf('href=') + 1, match.lastIndexOf('style'))
}
return undefined
}

View file

@ -28,6 +28,11 @@ describe('Newsletter email test', () => {
expect(isNewsletter('', from)).to.be.true
})
it('should return true when email is from bloomberg', () => {
const from = 'From: Bloomberg <noreply@mail.bloombergbusiness.com>'
expect(isNewsletter('', from)).to.be.true
})
})
describe('#getNewsletterUrl()', () => {