Skip to content

Commit

Permalink
Add support for nitter.net urls
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Nov 19, 2023
1 parent bf6436a commit 9c811d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ open class HtmlTagsBasedUnfurler : UnfurlerExtension {

private fun UnfurlerScope.downloadHtml(url: HttpUrl): Document? {
val request: Request = Request.Builder()
// Some websites will deny empty/unknown user agents,
// probably in an attempt to prevent scrapers?
// Some websites will deny empty/unknown user agents, probably in an
// attempt to prevent scrapers? Some goes for the following headers.
.header(
"User-Agent",
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36"
)
.header("Accept", "text/html")
.header("Accept-Language", "en-US,en;q=0.5")
.url(url)
.build()

Expand Down
13 changes: 13 additions & 0 deletions unfurl/src/test/kotlin/me/saket/unfurl/UnfurlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ class UnfurlerTest {
)
}

@Test fun `websites that deny requests without content type and language headers`() {
val result = unfurler.unfurl("https://nitter.net/saketme/status/1716330453311877183")
assertThat(result).isEqualTo(
UnfurlResult(
url = "https://nitter.net/saketme/status/1716330453311877183".toHttpUrl(),
title = "saket@androiddev.social (@saketme)",
description = "When the sole developer of a project starts using \"we\" instead of \"I\" in their code comments.",
favicon = "https://nitter.net/apple-touch-icon.png".toHttpUrl(),
thumbnail = "https://nitter.net/pic/media%2FF9GhXLmXYAAXIcb.png".toHttpUrl(),
)
)
}

@Test fun `follow redirects`() {
server.enqueue(
MockResponse()
Expand Down

0 comments on commit 9c811d9

Please sign in to comment.