mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
* change return value for new version of pubsub * add support for the other bloomberg newsletters not only business * add default fallback url if newsletter url not found * revert pubsub version change
This commit is contained in:
parent
2645566e18
commit
610c9c6348
6 changed files with 32 additions and 14 deletions
|
|
@ -5,5 +5,6 @@ export class AxiosHandler extends NewsletterHandler {
|
|||
super()
|
||||
this.senderRegex = /<.+@axios.com>/
|
||||
this.urlRegex = /View in browser at <a.*>(.*)<\/a>/
|
||||
this.defaultUrl = 'https://axios.com'
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@ import { NewsletterHandler } from './newsletter'
|
|||
export class BloombergHandler extends NewsletterHandler {
|
||||
constructor() {
|
||||
super()
|
||||
this.senderRegex = /<.+@mail.bloombergbusiness.com>/
|
||||
this.senderRegex = /<.+@mail.bloomberg.*.com>/
|
||||
this.urlRegex = /<a class="view-in-browser__url" href=["']([^"']*)["']/
|
||||
this.defaultUrl = 'https://www.bloomberg.com/'
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,5 +5,6 @@ export class GolangHandler extends NewsletterHandler {
|
|||
super()
|
||||
this.senderRegex = /<.+@golangweekly.com>/
|
||||
this.urlRegex = /<a href=["']([^"']*)["'].*>Read on the Web<\/a>/
|
||||
this.defaultUrl = 'https://golangweekly.com/'
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ const CONFIRMATION_CODE_PATTERN = /^\\(#\\d+\\)/
|
|||
export class NewsletterHandler {
|
||||
protected senderRegex = /NEWSLETTER_SENDER_REGEX/
|
||||
protected urlRegex = /NEWSLETTER_URL_REGEX/
|
||||
protected defaultUrl = 'NEWSLETTER_DEFAULT_URL'
|
||||
|
||||
isNewsletter(_rawUrl: string, from: string): boolean {
|
||||
// Axios newsletter is from <xx@axios.com>
|
||||
|
|
@ -19,7 +20,7 @@ export class NewsletterHandler {
|
|||
return re.test(from)
|
||||
}
|
||||
|
||||
getNewsletterUrl(rawUrl: string, html: string): string | undefined {
|
||||
getNewsletterUrl(_rawUrl: string, html: string): string | undefined {
|
||||
// get newsletter url from html
|
||||
const matches = html.match(this.urlRegex)
|
||||
if (matches) {
|
||||
|
|
@ -53,13 +54,8 @@ export class NewsletterHandler {
|
|||
throw new Error('invalid newsletter email')
|
||||
}
|
||||
|
||||
const url = this.getNewsletterUrl(rawUrl, html)
|
||||
console.log('url', url)
|
||||
if (!url) {
|
||||
console.log('invalid newsletter url', url)
|
||||
throw new Error('invalid newsletter url')
|
||||
}
|
||||
|
||||
// fallback to default url if newsletter url does not exist
|
||||
const url = this.getNewsletterUrl(rawUrl, html) || this.defaultUrl
|
||||
const author = this.getAuthor(from)
|
||||
|
||||
const message = {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,18 @@
|
|||
import { NewsletterHandler } from './newsletter'
|
||||
import addressparser from 'addressparser'
|
||||
|
||||
export class SubstackHandler extends NewsletterHandler {
|
||||
constructor() {
|
||||
super()
|
||||
this.defaultUrl = 'https://www.substack.com/'
|
||||
}
|
||||
|
||||
getNewsletterUrl(rawUrl: string, _html: string): string | undefined {
|
||||
// raw SubStack newsletter url is like <https://hongbo130.substack.com/p/tldr>
|
||||
// we need to get the real url
|
||||
return rawUrl.slice(1, -1)
|
||||
// we need to get the real url from the raw url
|
||||
return addressparser(rawUrl).length > 0
|
||||
? addressparser(rawUrl)[0].name
|
||||
: undefined
|
||||
}
|
||||
|
||||
isNewsletter(rawUrl: string, _from: string): boolean {
|
||||
|
|
|
|||
|
|
@ -30,9 +30,20 @@ describe('Newsletter email test', () => {
|
|||
expect(getNewsletterHandler('', from)).to.be.instanceof(AxiosHandler)
|
||||
})
|
||||
|
||||
it('should return BloombergHandler when email is from Bloomberg', () => {
|
||||
const from = 'From: Bloomberg <noreply@mail.bloombergbusiness.com>'
|
||||
expect(getNewsletterHandler('', from)).to.be.instanceof(BloombergHandler)
|
||||
context('when email is from Bloomberg', () => {
|
||||
it('should return BloombergHandler when email is from Bloomberg Business', () => {
|
||||
const from = 'From: Bloomberg <noreply@mail.bloombergbusiness.com>'
|
||||
expect(getNewsletterHandler('', from)).to.be.instanceof(
|
||||
BloombergHandler
|
||||
)
|
||||
})
|
||||
|
||||
it('should return BloombergHandler when email is from Bloomberg View', () => {
|
||||
const from = 'From: Bloomberg <noreply@mail.bloombergview.com>'
|
||||
expect(getNewsletterHandler('', from)).to.be.instanceof(
|
||||
BloombergHandler
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it('should return GolangHandler when email is from Golang Weekly', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue