Skip to content

Commit

Permalink
Init NewPipe before use in NewPipeVideoFormatsEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed May 13, 2024
1 parent 9386a0b commit 37c24b6
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ import org.schabi.newpipe.extractor.stream.AudioStream
import org.schabi.newpipe.extractor.stream.StreamExtractor
import org.schabi.newpipe.extractor.stream.StreamInfo
import org.schabi.newpipe.extractor.stream.VideoStream
import org.schabi.newpipe.extractor.downloader.Downloader
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.client.request.request
import io.ktor.client.request.setBody
import io.ktor.http.HttpMethod
import io.ktor.http.takeFrom
import io.ktor.http.HttpStatusCode
import io.ktor.util.toMap
import kotlinx.coroutines.runBlocking

class NewPipeVideoFormatsEndpoint(override val api: YtmApi): VideoFormatsEndpoint() {
override suspend fun getVideoFormats(
id: String,
include_non_default: Boolean,
filter: ((YoutubeVideoFormat) -> Boolean)?
): Result<List<YoutubeVideoFormat>> = runCatching {
init(api)

val link_handler: LinkHandler = YoutubeStreamLinkHandlerFactory.getInstance().fromId(id)
val youtube_stream_extractor: StreamExtractor = NewPipe.getService(ServiceList.YouTube.serviceId).getStreamExtractor(link_handler)

Expand All @@ -33,8 +46,57 @@ class NewPipeVideoFormatsEndpoint(override val api: YtmApi): VideoFormatsEndpoin

return@runCatching audio_streams + video_streams
}

companion object {
private var initialised: Boolean = false

private fun init(api: YtmApi) {
if (initialised) {
return
}

NewPipe.init(object : Downloader() {
override fun execute(request: org.schabi.newpipe.extractor.downloader.Request): org.schabi.newpipe.extractor.downloader.Response =
runBlocking {
val response: HttpResponse =
api.client.request {
url.takeFrom(request.url())

method = HttpMethod.parse(request.httpMethod())

val body: ByteArray? = request.dataToSend()
if (body != null) {
setBody(body)
}

for ((name, values) in request.headers()) {
headers.appendAll(name, values)
}
headers.append("User-Agent", getUserAgent())
}

if (response.status == HttpStatusCode.TooManyRequests) {
throw ReCaptchaException("reCaptcha Challenge requested", request.url())
}

return@runBlocking org.schabi.newpipe.extractor.downloader.Response(
response.status.value,
response.status.description,
response.headers.toMap(),
response.bodyAsText(),
request.url()
)
}
})

initialised = true
}
}
}

private fun getUserAgent(): String =
"Mozilla/5.0 (X11; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0"

private fun VideoStream.toYoutubeVideoFormat(): YoutubeVideoFormat {
return YoutubeVideoFormat(itag, format!!.mimeType, bitrate, url = content)
}
Expand Down

0 comments on commit 37c24b6

Please sign in to comment.