-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slow progress, get XMLFeed to return valid
- Loading branch information
Showing
30 changed files
with
292 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
feedfinder/src/main/java/com/jocmp/feedfinder/DefaultRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.jocmp.feedfinder | ||
|
||
import java.net.URL | ||
|
||
internal class DefaultRequest: Request { | ||
override suspend fun fetch(url: URL): Response { | ||
TODO("Not yet implemented") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
77 changes: 40 additions & 37 deletions
77
feedfinder/src/main/java/com/jocmp/feedfinder/FeedFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,62 @@ | ||
package com.jocmp.feedfinder | ||
|
||
import com.jocmp.feedfinder.source.BaseSource | ||
import com.jocmp.feedfinder.source.MetaLink | ||
import org.jsoup.Jsoup | ||
import org.jsoup.nodes.Document | ||
import java.io.IOException | ||
import com.jocmp.feedfinder.parser.Feed | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.net.MalformedURLException | ||
import java.net.URI | ||
import java.net.URL | ||
|
||
class FeedFinder(val url: String) { | ||
// Parser | ||
// - XMLFeed | ||
// - JSONFeed | ||
// - HTML | ||
// - Meta Links | ||
// - Body Links | ||
// - XMLFeedGuess (second pass) | ||
|
||
// XML can be parsed directly if XML feed | ||
// HTML takes response body | ||
|
||
class FeedFinder( | ||
val url: String, | ||
private val request: Request = DefaultRequest() | ||
) { | ||
// Convert URL to HTTPS if missing | ||
// iterate through sources | ||
// return best matching XML | ||
suspend fun find(): Result { | ||
// 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) { | ||
try { | ||
val parsedURL = URI(url) | ||
// TODO: | ||
// normalize URL via | ||
// https://github.com/Ranchero-Software/RSCore/blob/a2f711d64af8f1baefdf0092f57a7f0df7f0e5e8/Sources/RSCore/Shared/String+RSCore.swift#L114 | ||
val parsedURL = URI(url).toURL() | ||
// val response = request.fetch(url = parsedURL).parse() | ||
|
||
// val urls = listOf(parsedURL) + variations.map { variation -> | ||
// parsedURL.resolve(variation) | ||
// } | ||
// | ||
// val documents = coroutineScope { | ||
// urls.map { async { fetchDocument(it) } } | ||
// .awaitAll() | ||
// .filterNotNull() | ||
// } | ||
|
||
// return find(documents = documents) | ||
// XMLFeed.parse() | ||
// val rssChannel = RssParser().parse(response.body) | ||
// val feeds = XML(source = BaseSource(response)).find() | ||
|
||
val feeds = MetaLink(BaseSource(url = parsedURL)).find() | ||
|
||
return Result.Success(title = "", feedURL = URL("")) | ||
// if (feeds.isNotEmpty()) { | ||
// return@withContext Result.Success(feeds.first()) | ||
// } | ||
// | ||
Result.Failure(error = FeedError.IO_FAILURE) | ||
} catch (e: MalformedURLException) { | ||
return Result.Failure(error = FeedError.IO_FAILURE) | ||
Result.Failure(error = FeedError.IO_FAILURE) | ||
} | ||
} | ||
|
||
private fun fetchDocument(url: URI): Document? { | ||
return try { | ||
Jsoup.connect(url.toString()).get() | ||
} catch (e: IOException) { | ||
return null | ||
} | ||
sealed class Result { | ||
class Success(val feed: Feed) : Result() | ||
|
||
class Failure(val error: FeedError) : Result() | ||
} | ||
|
||
companion object { | ||
suspend fun find(feedURL: String): Result { | ||
return FeedFinder(url = feedURL).find() | ||
} | ||
|
||
|
||
private val variations = listOf( | ||
"feed", | ||
"rss" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.jocmp.feedfinder | ||
|
||
import java.net.URL | ||
|
||
interface Request { | ||
suspend fun fetch(url: URL): Response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.jocmp.feedfinder | ||
|
||
import com.jocmp.feedfinder.parser.FakeFeed | ||
import com.jocmp.feedfinder.parser.Feed | ||
|
||
class Response(val body: String?) { | ||
suspend fun parse(): Feed { | ||
return FakeFeed() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
feedfinder/src/main/java/com/jocmp/feedfinder/parser/FakeFeed.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.jocmp.feedfinder.parser | ||
|
||
class FakeFeed: Feed { | ||
override fun isValid(): Boolean { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.jocmp.feedfinder.parser | ||
|
||
interface Feed { | ||
fun isValid(): Boolean | ||
} |
35 changes: 35 additions & 0 deletions
35
feedfinder/src/main/java/com/jocmp/feedfinder/parser/Parser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.jocmp.feedfinder.parser | ||
|
||
internal object Parser { | ||
class NoFeedFoundError: Throwable() | ||
|
||
// Parse as XML | ||
// return result if feed is valid | ||
// if result is not valid, attempt to detect encoding | ||
// if encoding is present and encoding detection confidence is high, | ||
// reparse XML | ||
// return result if feed is valid | ||
// if result is not present, parse as JSON | ||
// return result if feed is valid | ||
// if no result, raise a NotFeed error | ||
|
||
// Parser | ||
// - XMLFeed | ||
// - JSONFeed | ||
// - HTML | ||
suspend fun parse(body: String): Feed { | ||
val xmlFeed = XMLFeed.from(body) | ||
|
||
if (xmlFeed.isValid()) { | ||
return xmlFeed | ||
} | ||
|
||
throw NoFeedFoundError() | ||
} | ||
|
||
// sealed class Document { | ||
// class XMLDocument | ||
// class HTMLDocument | ||
// class JSONDocument | ||
// } | ||
} |
22 changes: 22 additions & 0 deletions
22
feedfinder/src/main/java/com/jocmp/feedfinder/parser/XMLFeed.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.jocmp.feedfinder.parser | ||
|
||
import com.prof18.rssparser.RssParser | ||
import com.prof18.rssparser.model.RssChannel | ||
|
||
internal class XMLFeed(private val channel: RssChannel) : Feed { | ||
override fun isValid(): Boolean { | ||
return !channel.link.isNullOrBlank() && | ||
!channel.title.isNullOrBlank() && | ||
hasEntries() | ||
} | ||
|
||
private fun hasEntries(): Boolean { | ||
return channel.items.isNotEmpty() | ||
} | ||
|
||
companion object { | ||
suspend fun from(body: String): XMLFeed { | ||
return XMLFeed(RssParser().parse(body)) | ||
} | ||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
feedfinder/src/main/java/com/jocmp/feedfinder/source/BodyLink.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
feedfinder/src/main/java/com/jocmp/feedfinder/source/Source.kt
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
feedfinder/src/main/java/com/jocmp/feedfinder/sources/BodyLink.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.jocmp.feedfinder.sources | ||
|
||
import com.jocmp.feedfinder.parser.Feed | ||
|
||
//internal class BodyLink(source: Source): Source by source { | ||
// override fun find(): List<Feed> { | ||
// return emptyList() | ||
// } | ||
//} |
18 changes: 18 additions & 0 deletions
18
feedfinder/src/main/java/com/jocmp/feedfinder/sources/Guess.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jocmp.feedfinder.sources | ||
|
||
// val urls = listOf(parsedURL) + variations.map { variation -> | ||
// parsedURL.resolve(variation) | ||
// } | ||
// | ||
// val documents = coroutineScope { | ||
// urls.map { async { fetchDocument(it) } } | ||
// .awaitAll() | ||
// .filterNotNull() | ||
// } | ||
|
||
// return find(documents = documents) | ||
|
||
private val variations = listOf( | ||
"feed", | ||
"rss" | ||
) |
24 changes: 12 additions & 12 deletions
24
...a/com/jocmp/feedfinder/source/MetaLink.kt → .../com/jocmp/feedfinder/sources/MetaLink.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
feedfinder/src/main/java/com/jocmp/feedfinder/sources/ResponseSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.jocmp.feedfinder.sources | ||
|
||
//import com.jocmp.feedfinder.Response | ||
// | ||
//internal class ResponseSource(response: Response): Source { | ||
// fun createFromRequest() | ||
//} |
7 changes: 7 additions & 0 deletions
7
feedfinder/src/main/java/com/jocmp/feedfinder/sources/Source.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.jocmp.feedfinder.sources | ||
|
||
import com.jocmp.feedfinder.parser.Feed | ||
|
||
sealed interface Source { | ||
suspend fun find(): List<Feed> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.jocmp.feedfinder.sources | ||
|
||
//internal class XML(source: BaseSource): Source by source { | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.jocmp.feedfinder | ||
|
||
import java.net.URL | ||
|
||
class TestRequest(private val response: Response): Request { | ||
override suspend fun fetch(url: URL): Response { | ||
return response | ||
} | ||
} |
Oops, something went wrong.