Skip to content

Commit

Permalink
fix(Instagram & FB): debug build
Browse files Browse the repository at this point in the history
  • Loading branch information
Udhayarajan committed Jul 1, 2023
1 parent 54d0d7d commit d322b62
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<NEW_FEATURE_ADDED>.<WORKED_ON_BUG>.<BETA_VERSION_COUNT>"
//Priority on incrementing Feature > BugFix > Beta

Expand Down
8 changes: 0 additions & 8 deletions src/commonMain/kotlin/com/mugames/vidsnapkit/JsonExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down
59 changes: 47 additions & 12 deletions src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Instagram.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
}


Expand Down Expand Up @@ -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() == "{}") {
Expand All @@ -147,7 +158,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
}
return null
} ?: run {
onProgress(Result.Failed(Error.LoginRequired))
loginRequired()
null
}

Expand Down Expand Up @@ -203,27 +214,37 @@ 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") {
if (isReel) {
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
})
}
Expand All @@ -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")
Expand Down Expand Up @@ -275,7 +296,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
.getJSONArray("items")
)
} catch (e: JSONException) {
onProgress(Result.Failed(Error.LoginRequired))
loginRequired()
return
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d322b62

Please sign in to comment.