Fix failing unit test

Attempt to get the video URL from the first response if the 'actual' url cannot be found within.
This commit is contained in:
Gergely Hegedus 2023-12-14 23:30:46 +02:00
parent 78606df4dd
commit 0849fdc318
4 changed files with 20 additions and 7 deletions

View file

@ -10,7 +10,7 @@ class ActualVideoPageUrlConverter(
) : ParsingExceptionThrowingConverter<ActualVideoPageUrl>() { ) : ParsingExceptionThrowingConverter<ActualVideoPageUrl>() {
@Throws(IndexOutOfBoundsException::class, CaptchaRequiredException::class) @Throws(IndexOutOfBoundsException::class, CaptchaRequiredException::class)
override fun convertSafely(responseBody: ResponseBody): ActualVideoPageUrl? { override fun convertSafely(responseBody: ResponseBody): ActualVideoPageUrl {
val responseBodyAsString =responseBody.string() val responseBodyAsString =responseBody.string()
return try { return try {
val actualVideoPageUrl = responseBodyAsString val actualVideoPageUrl = responseBodyAsString

View file

@ -9,13 +9,20 @@ abstract class ParsingExceptionThrowingConverter<T> : Converter<ResponseBody, T>
@Throws(ParsingException::class, CaptchaRequiredException::class) @Throws(ParsingException::class, CaptchaRequiredException::class)
final override fun convert(value: ResponseBody): T? = final override fun convert(value: ResponseBody): T? =
try { doActionSafely {
convertSafely(value) convertSafely(value)
}
@Throws(ParsingException::class, CaptchaRequiredException::class)
fun doActionSafely(action: () -> T): T {
try {
return action()
} catch (captchaRequiredException: CaptchaRequiredException) { } catch (captchaRequiredException: CaptchaRequiredException) {
throw captchaRequiredException throw captchaRequiredException
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
throw ParsingException(cause = throwable) throw ParsingException(cause = throwable)
} }
}
abstract fun convertSafely(responseBody: ResponseBody): T? abstract fun convertSafely(responseBody: ResponseBody): T
} }

View file

@ -3,6 +3,7 @@ package org.fnives.tiktokdownloader.data.network.parsing.converter
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.fnives.tiktokdownloader.Logger import org.fnives.tiktokdownloader.Logger
import org.fnives.tiktokdownloader.data.network.exceptions.CaptchaRequiredException import org.fnives.tiktokdownloader.data.network.exceptions.CaptchaRequiredException
import org.fnives.tiktokdownloader.data.network.exceptions.ParsingException
import org.fnives.tiktokdownloader.data.network.parsing.response.VideoFileUrl import org.fnives.tiktokdownloader.data.network.parsing.response.VideoFileUrl
class VideoFileUrlConverter( class VideoFileUrlConverter(
@ -10,12 +11,17 @@ class VideoFileUrlConverter(
) : ParsingExceptionThrowingConverter<VideoFileUrl>() { ) : ParsingExceptionThrowingConverter<VideoFileUrl>() {
@Throws(IllegalArgumentException::class, IndexOutOfBoundsException::class, CaptchaRequiredException::class) @Throws(IllegalArgumentException::class, IndexOutOfBoundsException::class, CaptchaRequiredException::class)
override fun convertSafely(responseBody: ResponseBody): VideoFileUrl? { override fun convertSafely(responseBody: ResponseBody): VideoFileUrl {
return convertSafely(responseBody.string()) return convert(responseBody.string())
}
@Throws(ParsingException::class, CaptchaRequiredException::class)
fun convertSafely(responseBody: String): VideoFileUrl {
return doActionSafely { convert(responseBody) }
} }
@Throws(IllegalArgumentException::class, IndexOutOfBoundsException::class, CaptchaRequiredException::class) @Throws(IllegalArgumentException::class, IndexOutOfBoundsException::class, CaptchaRequiredException::class)
fun convertSafely(responseBody: String): VideoFileUrl { private fun convert(responseBody: String): VideoFileUrl {
val html = responseBody.also(throwIfIsCaptchaResponse::invoke) val html = responseBody.also(throwIfIsCaptchaResponse::invoke)
val url = tryToParseDownloadLink(html).also { Logger.logMessage("parsed download link = $it") } val url = tryToParseDownloadLink(html).also { Logger.logMessage("parsed download link = $it") }
?: tryToParseVideoSrc(html).also { Logger.logMessage("parsed video src = $it") } ?: tryToParseVideoSrc(html).also { Logger.logMessage("parsed video src = $it") }

View file

@ -5,6 +5,6 @@ import org.fnives.tiktokdownloader.data.network.parsing.response.VideoResponse
class VideoResponseConverter : ParsingExceptionThrowingConverter<VideoResponse>() { class VideoResponseConverter : ParsingExceptionThrowingConverter<VideoResponse>() {
override fun convertSafely(responseBody: ResponseBody): VideoResponse? = override fun convertSafely(responseBody: ResponseBody): VideoResponse =
VideoResponse(responseBody.contentType(), responseBody.byteStream()) VideoResponse(responseBody.contentType(), responseBody.byteStream())
} }