Skip to content

Commit

Permalink
Merge pull request #51 from fleeksoft/develop
Browse files Browse the repository at this point in the history
Release 0.1.6-alpha1
  • Loading branch information
itboy87 authored Aug 24, 2024
2 parents bb49133 + 6c1a46d commit dca8d13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ agp = "8.2.2" #kmp max gradle support 8.2 right now
kotlin = "2.0.10"
compileSdk = "34"
minSdk = "21"
libraryVersion = "0.1.5"
libraryVersion = "0.1.6-alpha1"
ktor = "3.0.0-beta-2"
coroutines = "1.8.1"
kotlinxDatetime = "0.6.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.fleeksoft.ksoup.network

import com.fleeksoft.ksoup.Ksoup
import com.fleeksoft.ksoup.io.SourceReaderImpl
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.parser.Parser
import korlibs.io.async.CIO
import korlibs.io.net.http.HttpBodyContent
import korlibs.io.net.http.HttpClient
import korlibs.io.stream.openSync
import korlibs.io.stream.toAsyncStream
import korlibs.io.stream.toSyncOrNull
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

Expand Down Expand Up @@ -33,8 +37,8 @@ public suspend fun Ksoup.parseGetRequest(
)
// url can be changed after redirection
val finalUrl = httpResponse.headers["location"] ?: url
val response = httpResponse.readAllString()
return@withContext parse(html = response, parser = parser, baseUri = finalUrl)
val sourceReader = SourceReaderImpl(httpResponse.content.toAsyncStream().toSyncOrNull() ?: httpResponse.readAllString().openSync())
return@withContext parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl)
}

/**
Expand Down Expand Up @@ -63,8 +67,8 @@ public suspend fun Ksoup.parseSubmitRequest(
)
// url can be changed after redirection
val finalUrl = httpResponse.headers["location"] ?: url
val response = httpResponse.readAllString()
return@withContext parse(html = response, parser = parser, baseUri = finalUrl)
val sourceReader = SourceReaderImpl(httpResponse.content.toAsyncStream().toSyncOrNull() ?: httpResponse.readAllString().openSync())
return@withContext parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl)
}

/**
Expand Down Expand Up @@ -93,6 +97,6 @@ public suspend fun Ksoup.parsePostRequest(
)
// url can be changed after redirection
val finalUrl = httpResponse.headers["location"] ?: url
val response = httpResponse.readAllString()
return@withContext parse(html = response, parser = parser, baseUri = finalUrl)
val sourceReader = SourceReaderImpl(httpResponse.content.toAsyncStream().toSyncOrNull() ?: httpResponse.readAllString().openSync())
return@withContext parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl)
}
19 changes: 11 additions & 8 deletions ksoup-network/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
@file:OptIn(InternalAPI::class)

package com.fleeksoft.ksoup.network

import com.fleeksoft.ksoup.Ksoup
import com.fleeksoft.ksoup.io.SourceReaderImpl
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.parser.Parser
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.utils.io.*

/**
* Use to fetch and parse a HTML page.
Expand All @@ -25,8 +29,8 @@ public suspend fun Ksoup.parseGetRequest(
val httpResponse = NetworkHelperKtor.instance.get(url, httpRequestBuilder = httpRequestBuilder)
// url can be changed after redirection
val finalUrl = httpResponse.request.url.toString()
val response = httpResponse.bodyAsText()
return parse(html = response, parser = parser, baseUri = finalUrl)
val sourceReader = SourceReaderImpl(httpResponse.bodyAsChannel().readBuffer)
return parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl)
}

/**
Expand Down Expand Up @@ -54,8 +58,8 @@ public suspend fun Ksoup.parseSubmitRequest(
)
// url can be changed after redirection
val finalUrl = httpResponse.request.url.toString()
val result: String = httpResponse.bodyAsText()
return parse(html = result, parser = parser, baseUri = finalUrl)
val sourceReader = SourceReaderImpl(httpResponse.bodyAsChannel().readBuffer)
return parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl)
}

/**
Expand All @@ -74,13 +78,12 @@ public suspend fun Ksoup.parsePostRequest(
httpRequestBuilder: HttpRequestBuilder.() -> Unit = {},
parser: Parser = Parser.htmlParser(),
): Document {
val httpResponse =
NetworkHelperKtor.instance.post(
val httpResponse = NetworkHelperKtor.instance.post(
url = url,
httpRequestBuilder = httpRequestBuilder,
)
// url can be changed after redirection
val finalUrl = httpResponse.request.url.toString()
val result: String = httpResponse.bodyAsText()
return parse(html = result, parser = parser, baseUri = finalUrl)
val sourceReader = SourceReaderImpl(httpResponse.bodyAsChannel().readBuffer)
return parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl)
}

0 comments on commit dca8d13

Please sign in to comment.