From d322b62d68c4799a7dbe8e24eca114a655ff241e Mon Sep 17 00:00:00 2001 From: udhayarajan Date: Sat, 1 Jul 2023 20:11:01 +0530 Subject: [PATCH] fix(Instagram & FB): debug build --- build.gradle.kts | 2 +- .../kotlin/com/mugames/vidsnapkit/JsonExt.kt | 8 --- .../mugames/vidsnapkit/extractor/Extractor.kt | 4 ++ .../mugames/vidsnapkit/extractor/Instagram.kt | 59 +++++++++++++++---- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f7d22cf1..6d86b9d5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,7 @@ plugins { } group = "io.github.udhayarajan" -version = "5.5.9.0.1" +version = "5.5.9.0.2" //Version Naming incremented if ".." //Priority on incrementing Feature > BugFix > Beta diff --git a/src/commonMain/kotlin/com/mugames/vidsnapkit/JsonExt.kt b/src/commonMain/kotlin/com/mugames/vidsnapkit/JsonExt.kt index 3f369997..06e20c8d 100644 --- a/src/commonMain/kotlin/com/mugames/vidsnapkit/JsonExt.kt +++ b/src/commonMain/kotlin/com/mugames/vidsnapkit/JsonExt.kt @@ -27,47 +27,39 @@ import org.slf4j.LoggerFactory * Created on 22-01-2022 */ -private val logger = LoggerFactory.getLogger("JsonExt.kt") - fun JSONObject.getNullableJSONObject(name: String): JSONObject? = try { getJSONObject(name) } catch (e: JSONException) { - logger.warn("${toString()} \nis only available") null } fun JSONArray.getNullableJSONObject(index: Int): JSONObject? = try { getJSONObject(index) } catch (e: JSONException) { - logger.warn("keyIdx=$index ${toString()} \nis only available") null } fun JSONArray.getNullableJSONArray(index: Int): JSONArray? = try { getJSONArray(index) } catch (e: JSONException) { - logger.warn("${toString()} \nis only available") null } fun JSONObject.getNullableJSONArray(name: String): JSONArray? = try { getJSONArray(name) } catch (e: JSONException) { - logger.warn("key =${name} ${toString()} \nis only available") null } fun JSONObject.getNullableString(name: String): String? = try { getString(name) } catch (e: JSONException) { - logger.warn("key =${name} ${toString()} \nis only available") null } fun JSONObject.getNullable(name: String): String? = try { get(name).toString() } catch (e: JSONException) { - logger.warn("key =${name} ${toString()} \nis only available") null } diff --git a/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Extractor.kt b/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Extractor.kt index 609a28cf..b2cd7beb 100644 --- a/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Extractor.kt +++ b/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Extractor.kt @@ -233,6 +233,10 @@ abstract class Extractor( onProgress(Result.Failed(Error.NonFatalError(msg))) } + protected fun loginRequired() { + onProgress(Result.Failed(Error.LoginRequired)) + } + protected fun internalError(msg: String, e: Exception? = null) { onProgress(Result.Failed(Error.InternalError(msg, e))) } diff --git a/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Instagram.kt b/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Instagram.kt index 01d6ca1d..1a5d8754 100644 --- a/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Instagram.kt +++ b/src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Instagram.kt @@ -25,6 +25,7 @@ import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import org.slf4j.LoggerFactory +import java.math.BigInteger import java.util.* import java.util.regex.Pattern @@ -80,9 +81,19 @@ class Instagram internal constructor(url: String) : Extractor(url) { return false } + private fun getShortcode(): String? { + val matcher = Pattern.compile("(?:reel|reels|p)/(.*)[/?]").matcher(inputUrl) + return if (matcher.find()) matcher.group(1) else { + logger.error("unable to find shortcode from the url=${inputUrl}") + null + } + } + private fun getMediaId(page: String): String? { val matcher = Pattern.compile("\"media_id\":\"?(.*?)[\",_]").matcher(page) - return if (matcher.find()) matcher.group(1) else null + return if (matcher.find()) matcher.group(1) else getShortcode()?.run { + shortcodeToMediaID(this) + } } @@ -130,7 +141,7 @@ class Instagram internal constructor(url: String) : Extractor(url) { return null } val response = res.toJSONObjectOrNull() ?: run { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() return null } if (response.toString() == "{}") { @@ -147,7 +158,7 @@ class Instagram internal constructor(url: String) : Extractor(url) { } return null } ?: run { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() null } @@ -203,7 +214,7 @@ class Instagram internal constructor(url: String) : Extractor(url) { logger.info("The new url is $inputUrl&__a=1&__d=dis") var res = HttpRequest(inputUrl.plus("&__a=1&__d=dis"), headers).getResponse(true) if (res == null) { - clientRequestError("check the log") + loginRequired() return } if (res == "429") { @@ -211,19 +222,29 @@ class Instagram internal constructor(url: String) : Extractor(url) { var url = nonModURL.replace("/reel/", "/reels/") url = url.replace("/p/", "/reels/") - res = HttpRequest(url, headers).getResponse() - if (res == null) { - clientRequestError("check the log") + res = HttpRequest(url, headers).getResponse() ?: run { + loginRequired() return } + if (res == "429") { + res = HttpRequest(POST_API.format(getShortcode()), headers).getResponse() ?: run { + loginRequired() + return + } + val items = res.toJSONObjectOrNull()?.getNullableJSONArray("items") ?: run { + loginRequired() + return + } + extractFromItems(items) + } extractInfoShared(res) return } else { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() } } extractFromItems(res.toJSONObjectOrNull()?.getNullableJSONArray("items") ?: run { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() return }) } @@ -233,7 +254,7 @@ class Instagram internal constructor(url: String) : Extractor(url) { ?.toJSONObjectOrNull() highlights?.let { if (it.getNullable("login_required") == "true") { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() return } val highlight = it.getJSONObject("reels").getJSONObject("highlight:$highlightsId") @@ -275,7 +296,7 @@ class Instagram internal constructor(url: String) : Extractor(url) { .getJSONArray("items") ) } catch (e: JSONException) { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() return } } @@ -315,7 +336,7 @@ class Instagram internal constructor(url: String) : Extractor(url) { ?.getNullableJSONArray(objectName) != null } if (isObjectPresentInEntryData("LoginAndSignupPage")) { - onProgress(Result.Failed(Error.LoginRequired)) + loginRequired() } else if (isObjectPresentInEntryData("HttpErrorPage")) { onProgress(Result.Failed(Error.Instagram404Error(cookies != null))) } else { @@ -613,6 +634,20 @@ class Instagram internal constructor(url: String) : Extractor(url) { finalize() } + private fun shortcodeToMediaID(shortcode: String): String { + var id = BigInteger.ZERO + val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + + for (i in shortcode.indices) { + val char = shortcode[i] + val charIndex = alphabet.indexOf(char) + id = id * BigInteger.valueOf(64) + BigInteger.valueOf(charIndex.toLong()) + } + + return id.toString() + } + + override suspend fun testWebpage(string: String) { onProgress = { println(it)