Skip to content

Commit

Permalink
Merge pull request #41 from Udhayarajan/clinet-optimisation
Browse files Browse the repository at this point in the history
Optimisation
  • Loading branch information
Udhayarajan authored Jul 24, 2023
2 parents 8e7a0b4 + 964f2e8 commit 807a549
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plugins {
}

group = "io.github.udhayarajan"
version = "5.6.11"
version = "5.6.12"
// Version Naming incremented if "<NEW_FEATURE_ADDED>.<WORKED_ON_BUG>.<BETA_VERSION_COUNT_OR_PRE_RELEASE>"
// Priority on incrementing Feature > BugFix > Beta

Expand Down
4 changes: 3 additions & 1 deletion src/commonMain/kotlin/com/mugames/vidsnapkit/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class Util {
*/
fun decodeHTML(text: String?): String? {
if (text == null) return null
var data = text.replace("%(?![0-9a-fA-F]{2})".toRegex(), "%25")
data = data.replace("\\+".toRegex(), "%2B")
val decoder = HtmlDecoderFactory.createDecoderFactory()
val decoded = decoder.decodeHtml(text)
val decoded = decoder.decodeHtml(data)
// decoded = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY).toString()
try {
return URLDecoder.decode(decoded, "UTF-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ abstract class Extractor(
AcceptAllCookiesStorage()
}

private var closeClient = true

protected var httpRequestService = run {
val str = if (inputUrl.contains(Regex("/reels/audio/|tiktok"))) store else null
HttpRequestService.create(storage = str)
Expand Down Expand Up @@ -130,8 +132,10 @@ abstract class Extractor(
* @see HttpRequestService.create
* For more about timeouts, [refer](https://ktor.io/docs/timeout.html#configure_plugin)
*/
fun setCustomClient(httpClient: HttpClient) {
httpRequestService.close()
fun setCustomClient(httpClient: HttpClient, autoCloseClient: Boolean = true) {
if (closeClient)
httpRequestService.close()
closeClient = autoCloseClient
val str = if (inputUrl.contains(Regex("/reels/audio/|tiktok"))) store else null
httpRequestService = HttpRequestService.create(httpClient, str)
}
Expand Down Expand Up @@ -217,7 +221,8 @@ abstract class Extractor(
filteredFormats.forEach {
it.cookies.addAll(store.get(Url(inputUrl)))
}
httpRequestService.close()
if (closeClient)
httpRequestService.close()
onProgress(Result.Success(filteredFormats))
}
}
Expand Down Expand Up @@ -253,27 +258,32 @@ abstract class Extractor(
}

protected fun clientRequestError(msg: String = "error making request") {
httpRequestService.close()
if (closeClient)
httpRequestService.close()
onProgress(Result.Failed(Error.NonFatalError(msg)))
}

public fun failed(error: Error) {
httpRequestService.close()
fun failed(error: Error) {
if (closeClient)
httpRequestService.close()
onProgress(Result.Failed(error))
}

protected fun loginRequired() {
httpRequestService.close()
if (closeClient)
httpRequestService.close()
onProgress(Result.Failed(Error.LoginRequired))
}

protected fun internalError(msg: String, e: Exception? = null) {
httpRequestService.close()
if (closeClient)
httpRequestService.close()
onProgress(Result.Failed(Error.InternalError(msg, e)))
}

protected fun missingLogic() {
httpRequestService.close()
if (closeClient)
httpRequestService.close()
onProgress(Result.Failed(Error.MethodMissingLogic))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,14 @@ class Facebook internal constructor(url: String) : Extractor(url) {
)
)

val blurredImage = media.getJSONObject("blurred_image")
scopedFormats.imageData.add(
val blurredImage = media.getNullableJSONObject("blurred_image")
blurredImage?.getString("uri")?.let {
ImageResource(
blurredImage.getString("uri"), resolution = Util.getResolutionFromUrl(blurredImage.getString("uri"))
it, resolution = Util.getResolutionFromUrl(blurredImage.getString("uri"))
)
)
}?.let {
scopedFormats.imageData.add(it)
}

val previewImage = media.getJSONObject("previewImage")
scopedFormats.imageData.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.mugames.vidsnapkit.extractor

import com.mugames.vidsnapkit.*
import com.mugames.vidsnapkit.dataholders.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.withTimeoutOrNull
import org.json.JSONArray
Expand Down Expand Up @@ -136,6 +137,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
}

private fun isHighlightsPost(): Boolean {
if (inputUrl.contains("/s/")) return true
return inputUrl.contains("stories/highlights/".toRegex())
}

Expand Down Expand Up @@ -254,6 +256,10 @@ class Instagram internal constructor(url: String) : Extractor(url) {
} else if (isHighlightsPost()) {
headers["User-Agent"] =
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)"
if (!isCookieValid()) {
loginRequired()
return
}
val highlightsId = getHighlightsId()
highlightsId?.let {
extractHighlights(it)
Expand Down Expand Up @@ -305,6 +311,10 @@ class Instagram internal constructor(url: String) : Extractor(url) {
// possibly user url
headers["User-Agent"] =
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)"
if (!isCookieValid()) {
loginRequired()
return
}
val userId = getUserID()
userId?.let {
extractStories(it)
Expand Down Expand Up @@ -678,7 +688,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
val appID = getAppID(page)
headers["X-Ig-App-Id"] = appID
val res = httpRequestService.getResponse(GRAPHQL_URL.format(queryHash, getShortcode()), headers)
logger.info("graphQL response = $res")
logger.info("graphQL response = ${res.toString().substring(0, 80)}")
val shortcodeMedia =
res?.toJSONObject()?.getJSONObject("data")?.getNullableJSONObject("shortcode_media") ?: run {
logger.info("unable to even get from graphQL so trying direct ex")
Expand Down

0 comments on commit 807a549

Please sign in to comment.