Skip to content

Commit

Permalink
Merge pull request #38 from Udhayarajan/patch-fix-1
Browse files Browse the repository at this point in the history
patch for last version
  • Loading branch information
Udhayarajan authored Jul 23, 2023
2 parents 1aa9b0e + e75f933 commit e57f875
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ abstract class Extractor(
protected var headers: Hashtable<String, String> = Hashtable()
private val store = AcceptAllCookiesStorage()

protected var httpRequestService = HttpRequestService.create(storage = store)
protected var httpRequestService = run {
val str = if (inputUrl.contains(Regex("/reels/audio/|tiktok"))) store else null
HttpRequestService.create(storage = str)
}

/**
* If media is private just pass valid cookies to
Expand All @@ -127,7 +130,8 @@ abstract class Extractor(
*/
fun setCustomClient(httpClient: HttpClient) {
httpRequestService.close()
httpRequestService = HttpRequestService.create(httpClient, store)
val str = if (inputUrl.contains(Regex("/reels/audio/|tiktok"))) store else null
httpRequestService = HttpRequestService.create(httpClient, str)
}

/**
Expand Down Expand Up @@ -245,7 +249,7 @@ abstract class Extractor(
return sizes.awaitAll()
}

protected fun clientRequestError(msg: String = "The request video page missing. If you find it as false kindly contact us") {
protected fun clientRequestError(msg: String = "error making request") {
onProgress(Result.Failed(Error.NonFatalError(msg)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Facebook internal constructor(url: String) : Extractor(url) {
} ?: apply {
val uuid = "fb." + UUID.randomUUID().toString() + ".html"
File(uuid).writeText(webPage)
onProgress(Result.Failed(Error.NonFatalError("This video can't be Downloaded, refer=$uuid")))
onProgress(Result.Failed(Error.NonFatalError("Sorry! we can't see the page, refer=$uuid")))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ class Instagram internal constructor(url: String) : Extractor(url) {
tempHeader.remove("User-Agent")
tempHeader["X-Ig-App-Id"] = appId
val res = httpRequestService.postRequest(AUDIO_API, tempHeader, audioPayload)
val metadata = res.toJSONObject().getJSONObject("metadata")
metadata.run {
val metadata = res?.toJSONObject()?.getJSONObject("metadata")
metadata?.run {
getNullableJSONObject("original_sound_info")?.let { extractFromOriginalAudioInfo(it) }
?: getNullableJSONObject("music_info")
?.getJSONObject("music_asset_info")
Expand All @@ -293,6 +293,9 @@ class Instagram internal constructor(url: String) : Extractor(url) {
missingLogic()
return
}
} ?: run {
clientRequestError()
return
}
} else {
// possibly user url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ class Likee internal constructor(url: String) : Extractor(url) {
}
val response = httpRequestService.postRequest(GET_VIDEO_INFO, hashMapOf("postIds" to postId).toHashtable())
val responseData =
response.toJSONObject()
.getJSONObject("data")
response?.toJSONObject()
?.getJSONObject("data") ?: run {
clientRequestError()
return
}
responseData.getJSONArray("videoList")?.let {
extractVideoList(it)
} ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class Twitch internal constructor(url: String) : Extractor(url) {
"{\"query\":\"{clip(slug:\\\"%s\\\"){broadcaster{displayName}createdAt curator{displayName id}durationSeconds id tiny:thumbnailURL(width:86,height:45)small:thumbnailURL(width:260,height:147)medium:thumbnailURL(width:480,height:272)title videoQualities{frameRate quality sourceURL}viewCount}}\"}",
id
)
val response = httpRequestService.postRequest("https://gql.twitch.tv/gql", headers = headers)
val response = httpRequestService.postRequest("https://gql.twitch.tv/gql", headers = headers) ?: run {
clientRequestError()
return localFormats
}
val clip = response.toJSONObject().getJSONObject("data").getJSONObject("clip")
localFormats.imageData.add(ImageResource(clip.getString("medium"), "medium"))
val videoQualities = clip.getJSONArray("videoQualities")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class Twitter internal constructor(url: String) : Extractor(url) {
}

private suspend fun getToken() {
val response = httpRequestService.postRequest(base_url + "guest/activate.json", headers = headers)
val response = httpRequestService.postRequest(base_url + "guest/activate.json", headers = headers) ?: run {
clientRequestError("post request fails")
return
}
headers["x-guest-token"] = response.toJSONObject().getString("guest_token")
if (inputUrl.contains("status")) extractVideo()
if (inputUrl.contains("broadcasts")) extractBroadcasts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface HttpRequestService {
url: String,
headers: Hashtable<String, String>? = null,
postData: Hashtable<String, Any>? = null,
): String
): String?

suspend fun postRawResponse(
url: String,
Expand All @@ -85,11 +85,9 @@ interface HttpRequestService {
client.config {
install(HttpTimeout) {
socketTimeoutMillis = 13_000
requestTimeoutMillis = 13_000
connectTimeoutMillis = 13_000
}
install(HttpCookies) {
if (storage != null) {
if (storage != null) {
install(HttpCookies) {
this.storage = storage
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,66 +53,69 @@ class HttpRequestServiceImpl(private val client: HttpClient) : HttpRequestServic
override suspend fun getResponse(
url: String,
headers: Hashtable<String, String>?,
): String? = try {
client.get {
url(url)
headers?.let {
if (it.isNotEmpty()) {
headers {
for ((key, value) in it) append(key, value)
): String? {
return try {
client.get {
url(url)
headers?.let {
if (it.isNotEmpty()) {
headers {
for ((key, value) in it) append(key, value)
}
}
}
}.run {
if (status == HttpStatusCode.OK) {
bodyAsText()
} else if (status in redirectionStatusCode) {
getLastPossibleRedirectedResponse(this, headers).bodyAsText()
} else if (url.contains("instagram") && status == HttpStatusCode.InternalServerError) {
"{error:\"Invalid Cookies\"}"
} else if (status == HttpStatusCode.TooManyRequests) {
logger.warn("Unhandled in getData() TooManyRequest for url=$url with headers=$headers & response=${bodyAsText()}")
"429"
} else {
val body = bodyAsText()
logger.warn(
"Unhandled in getData() status code=$status for url=$url with headers=$headers &\n response=${
body.substring(
min(body.length, 2000)
)
}"
)
null
}
}
}.run {
if (status == HttpStatusCode.OK) {
bodyAsText()
} else if (status in redirectionStatusCode) {
getLastPossibleRedirectedResponse(this, headers).bodyAsText()
} else if (url.contains("instagram") && status == HttpStatusCode.InternalServerError) {
} catch (e: ClientRequestException) {
logger.error("getData() url=$url header=$headers ClientRequestException:", e)
null
} catch (e: SSLPeerUnverifiedException) {
logger.error("getData() url=$url header=$headers SSLPeerUnverifiedException ${e.message}")
throw ProxyException(e)
} catch (e: SocketTimeoutException) {
logger.error("getData() url=$url header=$headers SocketTimeoutException ${e.message}")
throw ProxyException(e)
} catch (e: SocketException) {
logger.error("getData() url=$url header=$headers SocketException ${e.message}")
throw ProxyException(e)
} catch (e: SSLHandshakeException) {
logger.error("getData() url=$url header=$headers SLLHandShakeException ${e.message}")
throw ProxyException(e)
} catch (e: SendCountExceedException) {
if (url.contains("instagram") && headers?.containsKey("Cookie") == true) {
"{error:\"Invalid Cookies\"}"
} else if (status == HttpStatusCode.TooManyRequests) {
logger.warn("Unhandled in getData() TooManyRequest for url=$url with headers=$headers & response=${bodyAsText()}")
"429"
} else {
val body = bodyAsText()
logger.warn(
"Unhandled in getData() status code=$status for url=$url with headers=$headers &\n response=${
body.substring(
min(body.length, 2000)
)
}"
)
null
logger.error("getData() url=$url header=$headers SendCountExceedException:", e)
throw e
}
}
} catch (e: ClientRequestException) {
logger.error("getData() url=$url header=$headers ClientRequestException:", e)
null
} catch (e: SSLPeerUnverifiedException) {
logger.error("getData() url=$url header=$headers SSLPeerUnverifiedException ${e.message}")
throw ProxyException(e)
} catch (e: SocketTimeoutException) {
logger.error("getData() url=$url header=$headers SocketTimeoutException ${e.message}")
throw ProxyException(e)
} catch (e: SocketException) {
logger.error("getData() url=$url header=$headers SocketException ${e.message}")
throw ProxyException(e)
} catch (e: SSLHandshakeException) {
logger.error("getData() url=$url header=$headers SLLHandShakeException ${e.message}")
throw ProxyException(e)
} catch (e: SendCountExceedException) {
if (url.contains("instagram") && headers?.containsKey("Cookie") == true) {
"{error:\"Invalid Cookies\"}"
} else {
logger.error("getData() url=$url header=$headers SendCountExceedException:", e)
} catch (e: Exception) {
if (e is TimeoutCancellationException || e is CancellationException) {
logger.error("getData() url=$url header=$headers Cancellation exception: ${e.message}")
return null
} else logger.error("getData() url=$url header=$headers Generic exception:", e)
throw e
}
} catch (e: Exception) {
if (e is TimeoutCancellationException || e is CancellationException) logger.error("getData() url=$url header=$headers Cancellation exception: ${e.message}")
else logger.error("getData() url=$url header=$headers Generic exception:", e)
throw e
}

override suspend fun getRawResponse(
url: String,
headers: Hashtable<String, String>?,
Expand Down Expand Up @@ -196,33 +199,35 @@ class HttpRequestServiceImpl(private val client: HttpClient) : HttpRequestServic
url: String,
headers: Hashtable<String, String>?,
postData: Hashtable<String, Any>?,
) = try {
client.post {
url(url)
headers?.let {
if (it.isNotEmpty()) headers {
for ((key, value) in it) append(key, value)
): String? {
return try {
client.post {
url(url)
headers?.let {
if (it.isNotEmpty()) headers {
for ((key, value) in it) append(key, value)
}
}
}
postData?.let {
setBody(TextContent(it.toJsonString(), ContentType.Application.Json))
}
}.bodyAsText()
} catch (e: SocketTimeoutException) {
logger.error("postRequest() url=$url header=$headers SocketTimeoutException ${e.message}")
throw ProxyException(e)
} catch (e: SocketException) {
logger.error("postRequest() url=$url header=$headers SocketException ${e.message}")
throw ProxyException(e)
} catch (e: SSLHandshakeException) {
logger.error("postRequest() url=$url header=$headers SSLPeerUnverifiedException ${e.message}")
throw ProxyException(e)
} catch (e: SSLPeerUnverifiedException) {
logger.error("postRequest() url=$url header=$headers SSLPeerUnverifiedException ${e.message}")
throw ProxyException(e)
} catch (e: Exception) {
logger.error("postRequest() url=$url header=$headers & postRequest=$postData Error:", e)
throw e
postData?.let {
setBody(TextContent(it.toJsonString(), ContentType.Application.Json))
}
}.bodyAsText()
} catch (e: SocketTimeoutException) {
logger.error("postRequest() url=$url header=$headers SocketTimeoutException ${e.message}")
throw ProxyException(e)
} catch (e: SocketException) {
logger.error("postRequest() url=$url header=$headers SocketException ${e.message}")
throw ProxyException(e)
} catch (e: SSLHandshakeException) {
logger.error("postRequest() url=$url header=$headers SSLPeerUnverifiedException ${e.message}")
throw ProxyException(e)
} catch (e: SSLPeerUnverifiedException) {
logger.error("postRequest() url=$url header=$headers SSLPeerUnverifiedException ${e.message}")
throw ProxyException(e)
} catch (e: Exception) {
logger.error("postRequest() url=$url header=$headers & postRequest=$postData Error:", e)
return null
}
}

override suspend fun postRawResponse(
Expand Down

0 comments on commit e57f875

Please sign in to comment.