Skip to content

Commit 12a8ead

Browse files
committed
Added way to test HTML & forcing all Instagram request with header
1 parent cccfc89 commit 12a8ead

14 files changed

Lines changed: 61 additions & 27 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.9"
30+
version = "5.3.10"
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/DailyMotion.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class DailyMotion(url: String) : Extractor(url) {
107107
}
108108
}
109109

110+
override suspend fun testWebpage(string: String) {
111+
TODO("Not yet implemented")
112+
}
113+
110114
private suspend fun extractFromM3U8(response: String) {
111115
fun valueForKey(key: String, line: String): String? {
112116
val matcher = Pattern.compile("$key=(?:\"(.*?)\"|(.*?),)").matcher(line)

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ abstract class Extractor(
119119

120120
private suspend fun safeAnalyze() {
121121
try {
122-
if (inputUrl.contains("instagram"))
122+
if (inputUrl.contains("instagram")) {
123123
inputUrl = if (cookies == null) {
124124
inputUrl.replace("/reels/", "/reel/")
125125
} else inputUrl.replace("/reel/", "/reels/")
126-
126+
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)"
127+
}
127128
if (HttpRequest(inputUrl, headers).isAvailable())
128129
analyze()
129130
else if (inputUrl.contains("instagram") && cookies != null) {
@@ -180,7 +181,7 @@ abstract class Extractor(
180181
val sizes = mutableListOf<Deferred<Long>>()
181182
coroutineScope {
182183
for (videoData in format.videoData) {
183-
sizes.add(async { HttpRequest(videoData.url).getSize() })
184+
sizes.add(async { HttpRequest(videoData.url, headers).getSize() })
184185
}
185186
}
186187
return sizes.awaitAll()
@@ -190,7 +191,7 @@ abstract class Extractor(
190191
val sizes = mutableListOf<Deferred<Long>>()
191192
coroutineScope {
192193
for (audioData in format.audioData) {
193-
sizes.add(async { HttpRequest(audioData.url).getSize() })
194+
sizes.add(async { HttpRequest(audioData.url, headers).getSize() })
194195
}
195196
}
196197
return sizes.awaitAll()
@@ -200,7 +201,7 @@ abstract class Extractor(
200201
val sizes = mutableListOf<Deferred<Long>>()
201202
coroutineScope {
202203
for (imageData in format.imageData) {
203-
sizes.add(async { HttpRequest(imageData.url).getSize() })
204+
sizes.add(async { HttpRequest(imageData.url, headers).getSize() })
204205
}
205206
}
206207
return sizes.awaitAll()
@@ -213,4 +214,6 @@ abstract class Extractor(
213214
protected inline fun internalError(msg: String, e: Exception? = null) {
214215
onProgress(Result.Failed(Error.InternalError(msg, e)))
215216
}
217+
218+
abstract suspend fun testWebpage(string: String)
216219
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ class Facebook internal constructor(url: String) : Extractor(url) {
523523
return null
524524
}
525525

526-
suspend fun testWebpage(string: String) {
526+
override suspend fun testWebpage(string: String) {
527527
onProgress = {
528528

529529
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ class Instagram internal constructor(url: String) : Extractor(url) {
156156
inputUrl = inputUrl.replace("https://instagram.com", "https://www.instagram.com")
157157
logger.info("The new url is $inputUrl&__a=1&__d=dis")
158158
val items =
159-
HttpRequest(inputUrl.plus("&__a=1&__d=dis"), getHeadersWithUserAgent()).getResponse(true) ?: run {
159+
HttpRequest(inputUrl.plus("&__a=1&__d=dis"), headers).getResponse(true) ?: run {
160160
clientRequestError("unable to get post event with __a=1&__d=dis")
161161
return
162162
}
163163
extractFromItems(items.toJSONObject().getJSONArray("items"))
164164
return
165165
}
166-
extractInfoShared(HttpRequest(inputUrl, getHeadersWithUserAgent()).getResponse() ?: run {
166+
extractInfoShared(HttpRequest(inputUrl, headers).getResponse() ?: run {
167167
clientRequestError()
168168
return
169169
})
@@ -182,7 +182,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
182182
}
183183

184184
private suspend fun extractHighlights(highlightsId: String) {
185-
val highlights = HttpRequest(HIGHLIGHTS_API.format("%3A$highlightsId"), getHeadersWithUserAgent()).getResponse()
185+
val highlights = HttpRequest(HIGHLIGHTS_API.format("%3A$highlightsId"), headers).getResponse()
186186
?.toJSONObjectOrNull()
187187
highlights?.let {
188188
if (it.getNullable("login_required") == "true") {
@@ -197,8 +197,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
197197

198198
@Suppress("UNCHECKED_CAST")
199199
private suspend fun extractStories(userId: String) {
200-
val dupHeader = getHeadersWithUserAgent()
201-
val stories = HttpRequest(STORIES_API.format(userId), dupHeader).getResponse()
200+
val stories = HttpRequest(STORIES_API.format(userId), headers).getResponse()
202201
val reel = JSONObject(stories).getNullableJSONObject("reel")
203202
reel?.let { extractFromItems(it.getJSONArray("items")) } ?: onProgress(
204203
Result.Failed(
@@ -209,12 +208,6 @@ class Instagram internal constructor(url: String) : Extractor(url) {
209208
)
210209
}
211210

212-
private fun getHeadersWithUserAgent(): Hashtable<String, String> {
213-
val dupHeader: Hashtable<String, String> = headers.clone() as Hashtable<String, String>
214-
dupHeader["User-Agent"] =
215-
"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)"
216-
return dupHeader
217-
}
218211

219212

220213
private suspend fun extractInfoShared(page: String) {
@@ -228,7 +221,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
228221
if (mediaId == null) throw JSONException("mediaId is null purposely thrown wrong error")
229222
val url = POST_API.format(mediaId)
230223
extractFromItems(
231-
HttpRequest(url, getHeadersWithUserAgent())
224+
HttpRequest(url, headers)
232225
.getResponse()
233226
.toString()
234227
.toJSONObject()
@@ -378,7 +371,7 @@ class Instagram internal constructor(url: String) : Extractor(url) {
378371
val matcher = Pattern.compile("<link rel=\"preload\" href=\"(.*?)\" as=\"script\"").matcher(page)
379372
while (matcher.find()) {
380373
ids.addAll(
381-
getQueryHash(HttpRequest(matcher.group(1)).getResponse() ?: run {
374+
getQueryHash(HttpRequest(matcher.group(1), headers).getResponse() ?: run {
382375
return null
383376
})
384377
)
@@ -560,4 +553,12 @@ class Instagram internal constructor(url: String) : Extractor(url) {
560553
} else
561554
finalize()
562555
}
556+
557+
override suspend fun testWebpage(string: String) {
558+
onProgress = {
559+
println(it)
560+
}
561+
extractInfoShared(string)
562+
}
563+
563564
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class Likee internal constructor(url: String) : Extractor(url) {
5757
}
5858
}
5959

60+
override suspend fun testWebpage(string: String) {
61+
TODO("Not yet implemented")
62+
}
63+
6064
private suspend fun extractVideoList(jsonArray: JSONArray) {
6165
for (i in 0 until jsonArray.length()) {
6266
val localFormats = formats.copy(title = "", videoData = mutableListOf(), imageData = mutableListOf())

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class LinkedIn internal constructor(url: String) : Extractor(url) {
4242
})
4343
}
4444

45+
override suspend fun testWebpage(string: String) {
46+
TODO("Not yet implemented")
47+
}
48+
4549
private suspend fun scratchWebpage(page: String) {
4650
val matcher = Pattern.compile("data-sources=\"(.*?)\"").matcher(page)
4751
if (matcher.find()) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class Periscope internal constructor(url: String) : Extractor(url) {
4343
TODO("Not yet implemented")
4444
}
4545

46+
override suspend fun testWebpage(string: String) {
47+
TODO("Not yet implemented")
48+
}
49+
4650
var manifest: ArrayList<ArrayList<String>>? = ArrayList()
4751

4852

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class ShareChat internal constructor(url: String) : Extractor(url) {
4242
})
4343
}
4444

45+
override suspend fun testWebpage(string: String) {
46+
TODO("Not yet implemented")
47+
}
48+
4549
private suspend fun scratchWebPage(response: String) {
4650
val matcher =
4751
Pattern.compile("""<script data-rh="true" type="application\/ld\+json">(\{"@context":"http:\/\/schema\.org","@type":"(?:Image|Video)Object".*?\})<\/script>""")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class TikTok internal constructor(url: String) : Extractor(url) {
8080
onProgress(Result.Failed(Error.MethodMissingLogic))
8181
}
8282

83-
suspend fun testWithWebPage(string: String){
83+
override suspend fun testWebpage(string: String) {
8484
onProgress = {
8585
println(it)
8686
}

0 commit comments

Comments
 (0)