Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Built for Mars newsletter #47

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package fr.nicopico.n2rss.config

import fr.nicopico.n2rss.mail.newsletter.AndroidWeeklyNewsletterHandler
import fr.nicopico.n2rss.mail.newsletter.BuiltForMarsNewsletterHandler
import fr.nicopico.n2rss.mail.newsletter.KotlinWeeklyNewsletterHandler
import fr.nicopico.n2rss.mail.newsletter.NewsletterHandler
import fr.nicopico.n2rss.mail.newsletter.PointerNewsletterHandler
Expand All @@ -33,5 +34,6 @@ class NewsletterConfiguration {
KotlinWeeklyNewsletterHandler(),
PointerNewsletterHandler(),
QuickBirdNewsletterHandler(),
BuiltForMarsNewsletterHandler(),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024 Nicolas PICON
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package fr.nicopico.n2rss.mail.newsletter

import fr.nicopico.n2rss.models.Article
import fr.nicopico.n2rss.models.Email
import fr.nicopico.n2rss.models.Newsletter
import org.jsoup.Jsoup
import org.jsoup.safety.Safelist
import java.net.URL

class BuiltForMarsNewsletterHandler : NewsletterHandler {
override val newsletter: Newsletter = Newsletter(
code = "builtformars",
name = "Built for Mars",
websiteUrl = "https://builtformars.com",
)

override fun canHandle(email: Email): Boolean {
return email.sender.email.contains("[email protected]")
}

override fun extractArticles(email: Email): List<Article> {
val cleanedHtml = Jsoup.clean(
email.content,
Safelist.basic()
.addTags("table", "tr", "td")
.addAttributes("tr", "id"),
)

val document = Jsoup.clean(
Jsoup.parseBodyFragment(cleanedHtml)
.selectFirst("tr#content-blocks")
?.html()
?: throw NewsletterParsingException("Unable to find content-blocks"),
Safelist.basic()
).let {
Jsoup.parseBodyFragment(it)
}

val title = email.subject.substringAfter(":").trim()
val link = document.select("a[href]")
.firstOrNull { it.text().isNotBlank() }
?.let { URL(it.attr("href")) }
?: throw NewsletterParsingException("Unable to find article link")
val description = document.select("p")
.takeWhile { it.select("span").isEmpty() }
.joinToString(separator = "\n\n") { it.text() }

return listOf(
Article(
title = title,
link = link,
description = description,
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2024 Nicolas PICON
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package fr.nicopico.n2rss.mail.newsletter

import fr.nicopico.n2rss.models.Email
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.withClue
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.kotlinx.datetime.shouldHaveSameDayAs
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URL

class BuiltForMarsNewsletterHandlerTest {

private lateinit var handler: BuiltForMarsNewsletterHandler

@BeforeEach
fun setUp() {
handler = BuiltForMarsNewsletterHandler()
}

@Nested
inner class CanHandleTest {
@Test
fun `should handle all emails from Built for Mars`() {
// GIVEN
val emails = loadEmails("stubs/emails/Built for Mars")

// WHEN - THEN
emails.all { handler.canHandle(it) } shouldBe true
}

@Test
fun `should ignore all emails from another newsletters`() {
// GIVEN
val emails = loadEmails("stubs/emails/Android Weekly")

// WHEN - THEN
emails.all { handler.canHandle(it) } shouldBe false
}
}

@Nested
inner class ProcessTest {
@Test
fun `should extract an articles from an email (1)`() {
// GIVEN
val email: Email =
loadEmail("stubs/emails/Built for Mars/BFM #72 Why giving away free stocks isn't easy.eml")

// WHEN
val publication = handler.process(email)

// THEN
assertSoftly(publication) {
withClue("title") {
title shouldBe "BFM #72: Why giving away free stocks isn't easy \uD83D\uDCC8"
}
withClue("date") {
date shouldHaveSameDayAs (email.date)
}
withClue("newsletter") {
newsletter.name shouldBe "Built for Mars"
}
}

publication.articles shouldHaveSize 1
assertSoftly(publication.articles[0]) {
withClue("title") {
title shouldBe "Why giving away free stocks isn't easy \uD83D\uDCC8"
}
withClue("link") {
link shouldBe URL("https://link.mail.beehiiv.com/ss/c/u001.IvR796000sSoCuC84EAfml9aijFF9Py_qFz_4rcpI0eKyy7lMMq9JlJa5WIULczOai9fDIIg4Nar_zOXSruEqw/460/vdO90HyzTomWFLK4lA7zDw/h2/h001.VjqhHF95LKAUUesIfc6_N5ycfCTeJwcSQDkHHhiKvts")
}
withClue("description") {
description shouldBe """Hey 👋

“And we’ll launch our start-up with a viral referral scheme”.

Despite being the daring plan of almost every start-up, it’s got a success rate so low that it’s usually just a large distraction.

Well, Robinhood are one of the exceptions.

Of course, luck and branding plays a role. But they’re enabled by very predictable psychological biases, nudges, hooks and design cues.

In this study, I’ve tried to break down these subtleties, and explain exactly why people are so drawn to their ${'$'}7.08 welcome bonus.""".trimIndent()
}

}
}
}
}
Loading