Fix issue#2 by replacing \u0026 with & character as the url is encoded

This commit is contained in:
Gergely Hegedus 2022-04-21 14:18:10 +03:00
parent b256cb9bf2
commit 0d5e1a73d2
5 changed files with 48 additions and 11 deletions

View file

@ -0,0 +1,12 @@
package org.fnives.tiktokdownloader
object Logger {
private const val TAG = "TTDTag"
fun logMessage(message: String) {
if (BuildConfig.DEBUG) {
System.err.println("TTDTag $message")
}
}
}

View file

@ -3,6 +3,7 @@ package org.fnives.tiktokdownloader.data.network
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import org.fnives.tiktokdownloader.Logger
import org.fnives.tiktokdownloader.data.model.VideoInPending
import org.fnives.tiktokdownloader.data.model.VideoInSavingIntoFile
import org.fnives.tiktokdownloader.data.network.exceptions.CaptchaRequiredException
@ -22,8 +23,10 @@ class TikTokDownloadRemoteSource(
wrapIntoProperException {
delay(delayBeforeRequest) // added just so captcha trigger may not happen
val actualUrl = service.getContentActualUrlAndCookie(videoInPending.url)
Logger.logMessage("actualUrl found = ${actualUrl.url}")
delay(delayBeforeRequest) // added just so captcha trigger may not happen
val videoUrl = service.getVideoUrl(actualUrl.url)
Logger.logMessage("videoFileUrl found = ${videoUrl.videoFileUrl}")
delay(delayBeforeRequest) // added just so captcha trigger may not happen
val response = service.getVideo(videoUrl.videoFileUrl)

View file

@ -1,9 +1,9 @@
package org.fnives.tiktokdownloader.data.network.parsing.converter
import okhttp3.ResponseBody
import org.fnives.tiktokdownloader.Logger
import org.fnives.tiktokdownloader.data.network.exceptions.CaptchaRequiredException
import org.fnives.tiktokdownloader.data.network.parsing.response.VideoFileUrl
import kotlin.jvm.Throws
class VideoFileUrlConverter(
private val throwIfIsCaptchaResponse: ThrowIfIsCaptchaResponse
@ -12,8 +12,8 @@ class VideoFileUrlConverter(
@Throws(IllegalArgumentException::class, IndexOutOfBoundsException::class, CaptchaRequiredException::class)
override fun convertSafely(responseBody: ResponseBody): VideoFileUrl? {
val html = responseBody.string().also(throwIfIsCaptchaResponse::invoke)
val url = tryToParseDownloadLink(html)
?: tryToParseVideoSrc(html)
val url = tryToParseDownloadLink(html).also { Logger.logMessage("parsed download link = $it") }
?: tryToParseVideoSrc(html).also { Logger.logMessage("parsed video src = $it") }
?: throw IllegalArgumentException("Couldn't parse url from HTML: $html")
return VideoFileUrl(url)
@ -26,7 +26,7 @@ class VideoFileUrlConverter(
html.split("\"playAddr\"")[1]
.dropWhile { it != '\"' }.drop(1)
.takeWhile { it != '\"' }
.replace("\\u0026", "&")
.urlCharacterReplacements()
} else {
null
}
@ -39,10 +39,19 @@ class VideoFileUrlConverter(
.dropWhile { it != '=' }
.dropWhile { it != '\"' }.drop(1)
.takeWhile { it != '\"' }
.replace("\\u0026", "&")
.urlCharacterReplacements()
} else {
null
}
private val replacements = mutableMapOf(
"\\u002F" to "/",
"\\u0026" to "&"
)
private fun String.urlCharacterReplacements(): String =
replacements.entries.fold(this) { result, toReplaceEntry ->
result.replace(toReplaceEntry.key, toReplaceEntry.value)
}
}
}