-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore NewPipeExtractor as fallback (#208)
- Loading branch information
1 parent
6aafd2d
commit 0295f05
Showing
7 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...mmonMain/kotlin/com/toasterofbread/spmp/youtubeapi/formats/NewPipeVideoFormatsEndpoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.toasterofbread.spmp.youtubeapi.formats | ||
|
||
import com.toasterofbread.spmp.youtubeapi.YoutubeApi | ||
import com.toasterofbread.spmp.youtubeapi.YoutubeVideoFormat | ||
import okhttp3.OkHttpClient | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import org.schabi.newpipe.extractor.NewPipe | ||
import org.schabi.newpipe.extractor.ServiceList | ||
import org.schabi.newpipe.extractor.downloader.Downloader | ||
import org.schabi.newpipe.extractor.downloader.Request | ||
import org.schabi.newpipe.extractor.downloader.Response | ||
import org.schabi.newpipe.extractor.exceptions.ParsingException | ||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException | ||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory | ||
import org.schabi.newpipe.extractor.stream.AudioStream | ||
import org.schabi.newpipe.extractor.stream.StreamInfo | ||
import org.schabi.newpipe.extractor.stream.VideoStream | ||
import java.io.IOException | ||
import java.util.concurrent.TimeUnit | ||
|
||
class NewPipeVideoFormatsEndpoint(override val api: YoutubeApi): VideoFormatsEndpoint() { | ||
|
||
private fun VideoStream.toYoutubeVideoFormat(): YoutubeVideoFormat { | ||
return YoutubeVideoFormat(itag, format!!.mimeType, bitrate, url = content) | ||
} | ||
|
||
private fun AudioStream.toYoutubeVideoFormat(): YoutubeVideoFormat { | ||
return YoutubeVideoFormat(itag, format!!.mimeType, bitrate, url = content) | ||
} | ||
|
||
|
||
override suspend fun getVideoFormats(id: String, filter: ((YoutubeVideoFormat) -> Boolean)?): Result<List<YoutubeVideoFormat>> { | ||
|
||
val linkHandler = YoutubeStreamLinkHandlerFactory.getInstance().fromId(id) | ||
val youtubeStreamExtractor = NewPipe.getService(ServiceList.YouTube.serviceId).getStreamExtractor(linkHandler) | ||
|
||
val streamInfo = try { | ||
StreamInfo.getInfo(youtubeStreamExtractor) | ||
} catch (e: ParsingException) { | ||
return Result.failure(e) | ||
} | ||
|
||
val filteredAudio = streamInfo.audioStreams | ||
.map { it.toYoutubeVideoFormat() } | ||
.filter { filter?.invoke(it) ?: true } | ||
|
||
val filteredVideo = streamInfo.videoStreams | ||
.map { it.toYoutubeVideoFormat() } | ||
.filter { filter?.invoke(it) ?: true } | ||
|
||
return Result.success(filteredAudio + filteredVideo) | ||
} | ||
} | ||
|
||
|
||
class CustomDownloader private constructor(builder: OkHttpClient.Builder) : Downloader() { | ||
private val client: OkHttpClient = builder.readTimeout(30, TimeUnit.SECONDS).build() | ||
|
||
@Throws(IOException::class, ReCaptchaException::class) | ||
override fun execute(request: Request): Response { | ||
val httpMethod = request.httpMethod() | ||
val url = request.url() | ||
val headers = request.headers() | ||
val requestBody = request.dataToSend()?.toRequestBody(null, 0) | ||
|
||
val requestBuilder = okhttp3.Request.Builder() | ||
.url(url) | ||
.method(httpMethod, requestBody) | ||
.addHeader("User-Agent", USER_AGENT) | ||
|
||
for ((headerName, headerValueList) in headers) { | ||
if (headerValueList.size > 1) { | ||
requestBuilder.removeHeader(headerName) | ||
for (headerValue in headerValueList) { | ||
requestBuilder.addHeader(headerName, headerValue!!) | ||
} | ||
} else if (headerValueList.size == 1) { | ||
requestBuilder.header(headerName, headerValueList[0]) | ||
} | ||
} | ||
|
||
client.newCall(requestBuilder.build()).execute().use { response -> | ||
if (response.code == 429) { | ||
throw ReCaptchaException("reCaptcha Challenge requested", url) | ||
} | ||
|
||
val responseBodyToReturn = response.body?.string() | ||
val latestUrl = response.request.url.toString() | ||
|
||
return Response( | ||
response.code, response.message, response.headers.toMultimap(), | ||
responseBodyToReturn, latestUrl | ||
) | ||
} | ||
} | ||
|
||
|
||
companion object { | ||
private lateinit var _instance: CustomDownloader | ||
|
||
private fun init(builder: OkHttpClient.Builder = OkHttpClient.Builder()) { | ||
_instance = CustomDownloader(builder) | ||
} | ||
|
||
fun getInstance(): CustomDownloader { | ||
if (!::_instance.isInitialized) { init() } | ||
return _instance | ||
} | ||
|
||
private const val USER_AGENT = | ||
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters