Skip to content

Commit

Permalink
Check if feed is XML first
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 11, 2023
1 parent 2a49a6e commit 8d50d04
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 85 deletions.
2 changes: 2 additions & 0 deletions basil/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {
implementation(project(":feedbinclient"))
implementation(project(":feedfinder"))
testImplementation("junit:junit:4.13.2")
testImplementation("io.mockk:mockk-android:1.13.7")
testImplementation("io.mockk:mockk-agent:1.13.7")
testImplementation(kotlin("test"))
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down
23 changes: 19 additions & 4 deletions basil/src/main/java/com/jocmp/basil/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.jocmp.feedfinder.FeedFinder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.net.URI
import java.net.URL
import java.util.UUID

data class Account(
Expand Down Expand Up @@ -49,15 +50,17 @@ data class Account(

val feed = Feed(
id = UUID.randomUUID().toString(),
name = entry.name,
feedURL = found.feedURL.toString()
name = entryNameOrDefault(entry, found.name),
feedURL = found.feedURL.toString(),
siteURL = entrySiteURL(found.siteURL)
)

if (entry.folderTitles.isEmpty()) {
feeds.add(feed)
} else {
entry.folderTitles.forEach { folderTitle ->
val folder = folders.find { folder -> folder.title == folderTitle } ?: Folder(title = folderTitle)
val folder = folders.find { folder -> folder.title == folderTitle }
?: Folder(title = folderTitle)

folder.feeds.add(feed)

Expand All @@ -74,6 +77,18 @@ data class Account(
return feed
}

private fun entrySiteURL(url: URL?): String {
return url?.toString() ?: ""
}

private fun entryNameOrDefault(entry: FeedFormEntry, feedName: String): String {
if (entry.name.isBlank()) {
return feedName
}

return entry.name
}

private suspend fun saveOPMLFile() = withContext(Dispatchers.IO) {
opmlFile.save()
}
Expand All @@ -95,7 +110,7 @@ fun Account.asOPML(): String {
opml += feed.asOPML(indentLevel = 2)
}

folders.sortedBy { it.title } .forEach { folder ->
folders.sortedBy { it.title }.forEach { folder ->
opml += folder.asOPML(indentLevel = 2)
}

Expand Down
4 changes: 2 additions & 2 deletions basil/src/main/java/com/jocmp/basil/Feed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data class Feed(
val id: String,
val name: String,
val feedURL: String,
val homepageURL: String = ""
val siteURL: String = ""
) {
override fun equals(other: Any?): Boolean {
if (other is Feed) {
Expand All @@ -21,6 +21,6 @@ data class Feed(
}

fun Feed.asOPML(indentLevel: Int): String {
val opml = "<outline text=\"${name}\" title=\"${name}\" description=\"\" type=\"rss\" version=\"RSS\" htmlUrl=\"\" xmlUrl=\"${feedURL}\"/>\n"
val opml = "<outline text=\"${name}\" title=\"${name}\" description=\"\" type=\"rss\" version=\"RSS\" htmlUrl=\"${siteURL}\" xmlUrl=\"${feedURL}\"/>\n"
return opml.prepending(tabCount = indentLevel)
}
22 changes: 17 additions & 5 deletions basil/src/test/java/com/jocmp/basil/AccountTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.jocmp.basil

import com.jocmp.feedfinder.FeedFinder
import io.mockk.coEvery
import io.mockk.mockkConstructor
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.util.UUID
import kotlin.test.assertContains
import kotlin.test.assertEquals

Expand All @@ -13,6 +16,15 @@ class AccountTest {
@Rule
val folder = TemporaryFolder()

@Before
fun setup() {
mockkConstructor(FeedFinder::class)

coEvery {
anyConstructed<FeedFinder>().find()
} returns FeedFinder.Result.Success(listOf(FakeParserFeed()))
}

@Test
fun opmlFile_endsWithSubscriptions() {
val accountPath = folder.newFile().toURI()
Expand Down Expand Up @@ -50,7 +62,7 @@ class AccountTest {
val accountPath = folder.newFile().toURI()
val account = Account(id = "777", path = accountPath)
val entry = FeedFormEntry(
url = "https://www.theverge.com/rss/index.xml",
url = "https://theverge.com/rss/index.xml",
name = "The Verge",
folderTitles = listOf(),
)
Expand All @@ -70,7 +82,7 @@ class AccountTest {
val accountPath = folder.newFile().toURI()
val account = Account(id = "777", path = accountPath)
val entry = FeedFormEntry(
url = "https://www.theverge.com/rss/index.xml",
url = "https://theverge.com/rss/index.xml",
name = "The Verge",
folderTitles = listOf("Tech"),
)
Expand All @@ -92,7 +104,7 @@ class AccountTest {
runBlocking { account.addFolder("Tech") }

val entry = FeedFormEntry(
url = "https://www.theverge.com/rss/index.xml",
url = "https://theverge.com/rss/index.xml",
name = "The Verge",
folderTitles = listOf("Tech"),
)
Expand All @@ -114,7 +126,7 @@ class AccountTest {
runBlocking { account.addFolder("Tech") }

val entry = FeedFormEntry(
url = "https://www.theverge.com/rss/index.xml",
url = "https://theverge.com/rss/index.xml",
name = "The Verge",
folderTitles = listOf("Tech", "Culture"),
)
Expand Down
13 changes: 13 additions & 0 deletions basil/src/test/java/com/jocmp/basil/FakeParserFeed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jocmp.basil

import com.jocmp.feedfinder.parser.Feed
import java.net.URL

class FakeParserFeed(
override val name: String = "The Verge - All Posts",
override val feedURL: URL = URL("https://theverge.com/rss/index.xml"),
override val siteURL: URL? = null,
private val valid: Boolean = true
) : Feed {
override fun isValid() = valid
}
9 changes: 3 additions & 6 deletions feedfinder/src/main/java/com/jocmp/feedfinder/FeedFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ import java.net.URL
// HTML takes response body

class FeedFinder internal constructor(
val url: String,
private val url: String,
private val request: Request = DefaultRequest()
) {
// Convert URL to HTTPS if missing
// 1. Download the request using a Java HTTP connection
// 2. If the response is an XML Feed itself, return
// 3. If the response is HTML and th
internal suspend fun find(): Result = withContext(Dispatchers.IO) {
suspend fun find(): Result = withContext(Dispatchers.IO) {
try {
val parsedURL = URI(url.withProtocol).toURL()
val response = request.fetch(url = parsedURL)
Expand All @@ -52,6 +48,7 @@ class FeedFinder internal constructor(

private fun sources(response: Response): List<Source> {
return listOf(
XMLSource(response),
MetaLinkSource(response = response, request = request),
)
}
Expand Down
3 changes: 0 additions & 3 deletions feedfinder/src/main/java/com/jocmp/feedfinder/Response.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.jocmp.feedfinder

import com.jocmp.feedfinder.parser.FakeFeed
import com.jocmp.feedfinder.parser.Feed
import com.jocmp.feedfinder.parser.Parser
import com.jocmp.feedfinder.parser.XMLFeed
import java.net.URL

internal class Response(val url: URL, val body: String) {
Expand Down
12 changes: 0 additions & 12 deletions feedfinder/src/main/java/com/jocmp/feedfinder/parser/FakeFeed.kt

This file was deleted.

4 changes: 4 additions & 0 deletions feedfinder/src/main/java/com/jocmp/feedfinder/parser/Feed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ import java.net.URL
interface Feed {
fun isValid(): Boolean

val name: String

val feedURL: URL

val siteURL: URL?
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ internal class XMLFeed(
hasEntries()
}

override val name: String
get() = channel!!.title!!

override val siteURL: URL?
get() = channel?.link?.let {
URL(it)
}

private fun hasEntries(): Boolean {
return channel != null &&
channel.items.isNotEmpty()
Expand Down

This file was deleted.

18 changes: 0 additions & 18 deletions feedfinder/src/main/java/com/jocmp/feedfinder/sources/Guess.kt

This file was deleted.

17 changes: 0 additions & 17 deletions feedfinder/src/test/java/com/jocmp/feedfinder/FeedFinderTest.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.net.URL
import kotlin.math.exp
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class XMLSourceTest {
@Test
Expand Down

0 comments on commit 8d50d04

Please sign in to comment.