-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NewPipeExtractor for stream extraction
- Loading branch information
Showing
6 changed files
with
148 additions
and
1 deletion.
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
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
64 changes: 64 additions & 0 deletions
64
innertube/src/main/java/com/zionhuang/innertube/NewPipeDownloaderImpl.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,64 @@ | ||
package com.zionhuang.innertube | ||
|
||
import okhttp3.OkHttpClient | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
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.ReCaptchaException | ||
import java.io.IOException | ||
|
||
object NewPipeDownloaderImpl : Downloader() { | ||
|
||
/** | ||
* Should be the latest Firefox ESR version. | ||
*/ | ||
private const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" | ||
|
||
private val client = OkHttpClient.Builder().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 dataToSend = request.dataToSend() | ||
|
||
val requestBuilder = okhttp3.Request.Builder() | ||
.method(httpMethod, dataToSend?.toRequestBody()) | ||
.url(url) | ||
.addHeader("User-Agent", USER_AGENT) | ||
|
||
YouTube.cookie?.let { | ||
requestBuilder.addHeader("Cookie", it) | ||
} | ||
|
||
headers.forEach { (headerName, headerValueList) -> | ||
if (headerName == "Cookie" && YouTube.cookie != null) { | ||
// Add YouTube login cookie from InnerTune to the other cookies from NewPipeExtractor | ||
requestBuilder.header(headerName, (headerValueList + YouTube.cookie).joinToString("; ")) | ||
} else if (headerValueList.size > 1) { | ||
requestBuilder.removeHeader(headerName) | ||
headerValueList.forEach { headerValue -> | ||
requestBuilder.addHeader(headerName, headerValue) | ||
} | ||
} else if (headerValueList.size == 1) { | ||
requestBuilder.header(headerName, headerValueList[0]) | ||
} | ||
} | ||
|
||
val response = client.newCall(requestBuilder.build()).execute() | ||
|
||
if (response.code == 429) { | ||
response.close() | ||
|
||
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) | ||
} | ||
|
||
} |
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