Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix 403 HTTP ERROR checking function, Add Retry Feature, Add Descript…
Browse files Browse the repository at this point in the history
…ion to VideoMeta
  • Loading branch information
maxrave-dev committed Jul 19, 2023
1 parent 414cc92 commit 1515f61
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 36 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Then, add dependencies in app level build.gradle:

```kotlin
dependencies {
implementation 'com.github.maxrave-dev:kotlin-youtubeExtractor:0.0.5'
implementation 'com.github.maxrave-dev:kotlin-youtubeExtractor:0.0.6'
}
```

Expand All @@ -51,8 +51,9 @@ Call YTExtractor inside your activity or fragment
```kotlin
//If your YouTube link is "https://www.youtube.com/watch?v=IDwytT0wFRM" so this videoId is "IDwytT0wFRM"
var videoId = "IDwytT0wFRM"
val yt = YTExtractor(con = context, CACHING = true, LOGGING = true)
val yt = YTExtractor(con = context, CACHING = true, LOGGING = true, retryCount = 3)
// CACHING and LOGGING are 2 optional params. LOGGING is for showing Log and CACHING is for saving SignatureCipher to optimize extracting time (not recommend CACHING to extract multiple videos because it causes HTTP 403 Error)
// retryCount is for retrying when extract fail (default is 1)
var ytFiles: SparseArray<YtFile>? = null
var videoMeta: VideoMeta? = null
GlobalScope.launch {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/maxrave/exampleApp/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//val videoId = "d40rzwlq8l4"
val listVideoId = listOf<String>("d40rzwlq8l4", "Q2T8-q9fGSI", "UzvbmzVDCQ4", "aaMv6SJafPA")
val listVideoId = listOf<String>("d40rzwlq8l4", "Q2T8-q9fGSI", "UzvbmzVDCQ4", "aaMv6SJafPA", "JvOg0TSvdGU")
val tv = findViewById<TextView>(R.id.textView)
val yt = YTExtractor(con = this@MainActivity, CACHING = false, LOGGING = true)
val yt = YTExtractor(con = this@MainActivity, CACHING = false, LOGGING = true, retryCount = 3)
var text = ""
GlobalScope.launch {
listVideoId.forEach { videoId ->
yt.extract(videoId)
if (yt.state == State.SUCCESS) {
yt.getYTFiles().let { it ->
var a = it?.get(251).let { data ->
val a = it?.get(251).let { data ->
data?.url.toString()
}
text += a + "\n"
Expand Down
4 changes: 2 additions & 2 deletions kotlinYoutubeExtractor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'maven-publish'
}
group = 'com.github.maxrave-dev'
version = '0.0.5'
version = '0.0.6'

android {
namespace 'com.maxrave.kotlinyoutubeextractor'
Expand Down Expand Up @@ -59,7 +59,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.maxrave-dev'
artifactId = 'kotlin-youtubeExtractor'
version = '0.0.5'
version = '0.0.6'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class VideoMeta(
* The video length in seconds.
*/
val videoLength: Long,
val viewCount: Long, val isLiveStream: Boolean, val shortDescription: String
val viewCount: Long, val isLiveStream: Boolean, val shortDescription: String?
) {

// 120 x 90
Expand Down Expand Up @@ -49,6 +49,7 @@ class VideoMeta(
var result = videoId?.hashCode() ?: 0
result = 31 * result + (title?.hashCode() ?: 0)
result = 31 * result + (author?.hashCode() ?: 0)
result = 31 * result + (shortDescription?.hashCode() ?: 0)
result = 31 * result + (channelId?.hashCode() ?: 0)
result = 31 * result + (videoLength xor (videoLength ushr 32)).toInt()
result = 31 * result + (viewCount xor (viewCount ushr 32)).toInt()
Expand All @@ -61,6 +62,7 @@ class VideoMeta(
"videoId='" + videoId + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", shortDescription='" + shortDescription + '\'' +
", channelId='" + channelId + '\'' +
", videoLength=" + videoLength +
", viewCount=" + viewCount +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.util.regex.Pattern
* @param LOGGING Enable logging, default is false.
*/

class YTExtractor(val con: Context, val CACHING: Boolean = false, val LOGGING: Boolean = false) {
class YTExtractor(val con: Context, val CACHING: Boolean = false, val LOGGING: Boolean = false, val retryCount: Int = 1) {
private val LOG_TAG = "Kotlin YouTube Extractor"
private val CACHE_FILE_NAME = "decipher_js_funct"

Expand Down Expand Up @@ -295,6 +295,7 @@ class YTExtractor(val con: Context, val CACHING: Boolean = false, val LOGGING: B
}
val videoDetails = ytPlayerResponse?.getJSONObject("videoDetails")
if (videoDetails != null) {
Log.d(LOG_TAG, "videoDetails: $videoDetails")
videoMeta = VideoMeta(
videoDetails.getString("videoId"),
videoDetails.getString("title"),
Expand Down Expand Up @@ -622,43 +623,59 @@ class YTExtractor(val con: Context, val CACHING: Boolean = false, val LOGGING: B
withContext(Dispatchers.IO) {
ytFiles = async {
state = State.LOADING
var mat = patYouTubePageLink.matcher(videoId)
if (mat.find()) {
videoID = mat.group(3)
} else {
mat = patYouTubeShortLink.matcher(videoId)
var retry: Int = 0
while (state != State.SUCCESS && retry < retryCount) {
if (LOGGING) Log.d(
LOG_TAG,
"Retry: $retry"
)
var mat = patYouTubePageLink.matcher(videoId)
if (mat.find()) {
videoID = mat.group(3)
} else if (videoId.matches("\\p{Graph}+?".toRegex())) {
videoID = videoId
} else {
mat = patYouTubeShortLink.matcher(videoId)
if (mat.find()) {
videoID = mat.group(3)
} else if (videoId.matches("\\p{Graph}+?".toRegex())) {
videoID = videoId
}
}
}
if (videoID != null) {
try {
state = State.SUCCESS
val temp = getStreamUrls()
if (videoID != null) {
try {
if (temp != null) {
if (!testHttp403Code(temp.getAudioOnly().bestQuality()?.url)) {
if (LOGGING) Log.d(
LOG_TAG,
"NO Error"
)
return@async getStreamUrls()
val temp = getStreamUrls()
try {
if (temp != null) {
val test = testHttp403Code(temp.getAudioOnly().bestQuality()?.url)
if (!test) {
if (LOGGING) Log.d(
LOG_TAG,
"NO Error"
)
state = State.SUCCESS
return@async temp
}
else {
retry++
state = State.ERROR
Log.e(LOG_TAG, "Extraction failed cause 403 HTTP Error")
}
}
}
}
catch (e: java.lang.Exception){
catch (e: IOException){
retry++
state = State.ERROR
Log.e(LOG_TAG, "Extraction failed cause 403 HTTP Error", e)
}
} catch (e: java.lang.Exception) {
retry++
state = State.ERROR
Log.e(LOG_TAG, "Extraction failed cause 403 HTTP Error", e)
Log.e(LOG_TAG, "Extraction failed", e)
}
} catch (e: java.lang.Exception) {
} else {
retry++
state = State.ERROR
Log.e(LOG_TAG, "Extraction failed", e)
Log.e(LOG_TAG, "Wrong YouTube link format")
}
} else {
state = State.ERROR
Log.e(LOG_TAG, "Wrong YouTube link format")
}
return@async null
}.await()
Expand Down

0 comments on commit 1515f61

Please sign in to comment.