Skip to content

Commit 8167b20

Browse files
committed
fix(Instagram): NonFatal error with Insta & add a few code to diagnosis the error
1 parent 946ff0b commit 8167b20

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ plugins {
2727
}
2828

2929
group = "io.github.udhayarajan"
30-
version = "5.3.6"
30+
version = "5.3.7"
3131
//Version Naming incremented if "<NEW_FEATURE_ADDED>.<WORKED_ON_BUG>.<BETA_VERSION_COUNT>"
3232
//Priority on incrementing Feature > BugFix > Beta
3333

src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Extractor.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.mugames.vidsnapkit.sanitizeAsHeaderValue
2727
import io.ktor.client.plugins.*
2828
import kotlinx.coroutines.*
2929
import kotlinx.coroutines.future.future
30+
import org.slf4j.LoggerFactory
3031
import java.util.*
3132
import javax.net.ssl.SSLHandshakeException
3233

@@ -67,6 +68,8 @@ abstract class Extractor(
6768
else -> null
6869
}
6970
}
71+
72+
private val logger = LoggerFactory.getLogger(Extractor::class.java)
7073
}
7174

7275
protected var inputUrl: String = url
@@ -83,7 +86,7 @@ abstract class Extractor(
8386
set(value) {
8487
value?.let {
8588
headers["Cookie"] = it.sanitizeAsHeaderValue()
86-
}?:run {
89+
} ?: run {
8790
headers.remove("Cookie")
8891
}
8992
field = value
@@ -117,15 +120,16 @@ abstract class Extractor(
117120
private suspend fun safeAnalyze() {
118121
try {
119122
if (inputUrl.contains("instagram"))
120-
inputUrl = if (cookies != null) {
123+
inputUrl = if (cookies == null) {
121124
inputUrl.replace("/reels/", "/reel/")
122125
} else inputUrl.replace("/reel/", "/reels/")
123126

124127
if (HttpRequest(inputUrl, headers).isAvailable())
125128
analyze()
126-
else if (inputUrl.contains("instagram") && cookies != null)
129+
else if (inputUrl.contains("instagram") && cookies != null) {
130+
logger.info("Forcing instagram")
127131
analyze(hashMapOf("forced" to true))
128-
else clientRequestError()
132+
} else clientRequestError()
129133
} catch (e: Exception) {
130134
if (e is SSLHandshakeException)
131135
clientRequestError()

src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Instagram.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.ktor.http.*
2424
import org.json.JSONArray
2525
import org.json.JSONException
2626
import org.json.JSONObject
27+
import org.slf4j.LoggerFactory
2728
import java.util.*
2829
import java.util.regex.Pattern
2930

@@ -43,6 +44,8 @@ class Instagram internal constructor(url: String) : Extractor(url) {
4344
const val HIGHLIGHTS_API = "https://www.instagram.com/api/v1/feed/reels_media/?reel_ids=highlight%s"
4445
const val GRAPHQL_URL =
4546
"https://www.instagram.com/graphql/query/?query_hash=b3055c01b4b222b8a47dc12b090e4e64&variables={\"shortcode\":\"%s\"}"
47+
48+
private val logger = LoggerFactory.getLogger(Instagram::class.java)
4649
}
4750

4851
private val formats = Formats()
@@ -57,10 +60,11 @@ class Instagram internal constructor(url: String) : Extractor(url) {
5760
newLoc.contains(keyword, ignoreCase = true)
5861
}
5962

60-
if (newLoc == "https://www.instagram.com/" || !containsRestrictedKeyword){
63+
if (newLoc == "https://www.instagram.com/" || !containsRestrictedKeyword) {
6164
return true
6265
}
6366
}
67+
logger.info("Oops!, Cookie is invalid so removing it from header")
6468
return false
6569
}
6670

@@ -143,8 +147,13 @@ class Instagram internal constructor(url: String) : Extractor(url) {
143147
if (!isCookieValid())
144148
cookies = null
145149
if (load?.get("forced") == true) {
150+
if (cookies == null) {
151+
onProgress(Result.Failed(Error.LoginRequired))
152+
return
153+
}
146154
inputUrl = inputUrl.replace("/reel/", "/p/")
147155
inputUrl = inputUrl.replace("https://instagram.com", "https://www.instagram.com")
156+
logger.info("The new url is $inputUrl&__a=1&__d=dis")
148157
val items =
149158
HttpRequest(inputUrl.plus("&__a=1&__d=dis"), getHeadersWithUserAgent()).getResponse(true) ?: run {
150159
clientRequestError("unable to get post event with __a=1&__d=dis")

src/commonMain/kotlin/com/mugames/vidsnapkit/network/HttpRequestHelper.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ class HttpInterfaceImpl(
160160
} catch (e: SendCountExceedException) {
161161
if (url.contains("instagram") && headers?.containsKey("Cookie") == true)
162162
"{error:\"Invalid Cookies\"}"
163-
else throw e
163+
else{
164+
logger.error("getData() url=${url} header=${headers.toString()} SendCountExceedException:", e)
165+
throw e
166+
}
164167
} catch (e: Exception) {
168+
logger.error("getData() url=${url} header=${headers.toString()} Generic exception:", e)
165169
throw e
166170
}
167171
}

0 commit comments

Comments
 (0)