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

Commit 1515f61

Browse files
committed
Fix 403 HTTP ERROR checking function, Add Retry Feature, Add Description to VideoMeta
1 parent 414cc92 commit 1515f61

File tree

5 files changed

+56
-36
lines changed

5 files changed

+56
-36
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Then, add dependencies in app level build.gradle:
3636

3737
```kotlin
3838
dependencies {
39-
implementation 'com.github.maxrave-dev:kotlin-youtubeExtractor:0.0.5'
39+
implementation 'com.github.maxrave-dev:kotlin-youtubeExtractor:0.0.6'
4040
}
4141
```
4242

@@ -51,8 +51,9 @@ Call YTExtractor inside your activity or fragment
5151
```kotlin
5252
//If your YouTube link is "https://www.youtube.com/watch?v=IDwytT0wFRM" so this videoId is "IDwytT0wFRM"
5353
var videoId = "IDwytT0wFRM"
54-
val yt = YTExtractor(con = context, CACHING = true, LOGGING = true)
54+
val yt = YTExtractor(con = context, CACHING = true, LOGGING = true, retryCount = 3)
5555
// 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)
56+
// retryCount is for retrying when extract fail (default is 1)
5657
var ytFiles: SparseArray<YtFile>? = null
5758
var videoMeta: VideoMeta? = null
5859
GlobalScope.launch {

app/src/main/java/com/maxrave/exampleApp/ui/MainActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ class MainActivity : AppCompatActivity() {
2222
super.onCreate(savedInstanceState)
2323
setContentView(R.layout.activity_main)
2424
//val videoId = "d40rzwlq8l4"
25-
val listVideoId = listOf<String>("d40rzwlq8l4", "Q2T8-q9fGSI", "UzvbmzVDCQ4", "aaMv6SJafPA")
25+
val listVideoId = listOf<String>("d40rzwlq8l4", "Q2T8-q9fGSI", "UzvbmzVDCQ4", "aaMv6SJafPA", "JvOg0TSvdGU")
2626
val tv = findViewById<TextView>(R.id.textView)
27-
val yt = YTExtractor(con = this@MainActivity, CACHING = false, LOGGING = true)
27+
val yt = YTExtractor(con = this@MainActivity, CACHING = false, LOGGING = true, retryCount = 3)
2828
var text = ""
2929
GlobalScope.launch {
3030
listVideoId.forEach { videoId ->
3131
yt.extract(videoId)
3232
if (yt.state == State.SUCCESS) {
3333
yt.getYTFiles().let { it ->
34-
var a = it?.get(251).let { data ->
34+
val a = it?.get(251).let { data ->
3535
data?.url.toString()
3636
}
3737
text += a + "\n"

kotlinYoutubeExtractor/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
id 'maven-publish'
55
}
66
group = 'com.github.maxrave-dev'
7-
version = '0.0.5'
7+
version = '0.0.6'
88

99
android {
1010
namespace 'com.maxrave.kotlinyoutubeextractor'
@@ -59,7 +59,7 @@ afterEvaluate {
5959
from components.release
6060
groupId = 'com.github.maxrave-dev'
6161
artifactId = 'kotlin-youtubeExtractor'
62-
version = '0.0.5'
62+
version = '0.0.6'
6363
}
6464
}
6565
}

kotlinYoutubeExtractor/src/main/java/com/maxrave/kotlinyoutubeextractor/VideoMeta.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class VideoMeta(
99
* The video length in seconds.
1010
*/
1111
val videoLength: Long,
12-
val viewCount: Long, val isLiveStream: Boolean, val shortDescription: String
12+
val viewCount: Long, val isLiveStream: Boolean, val shortDescription: String?
1313
) {
1414

1515
// 120 x 90
@@ -49,6 +49,7 @@ class VideoMeta(
4949
var result = videoId?.hashCode() ?: 0
5050
result = 31 * result + (title?.hashCode() ?: 0)
5151
result = 31 * result + (author?.hashCode() ?: 0)
52+
result = 31 * result + (shortDescription?.hashCode() ?: 0)
5253
result = 31 * result + (channelId?.hashCode() ?: 0)
5354
result = 31 * result + (videoLength xor (videoLength ushr 32)).toInt()
5455
result = 31 * result + (viewCount xor (viewCount ushr 32)).toInt()
@@ -61,6 +62,7 @@ class VideoMeta(
6162
"videoId='" + videoId + '\'' +
6263
", title='" + title + '\'' +
6364
", author='" + author + '\'' +
65+
", shortDescription='" + shortDescription + '\'' +
6466
", channelId='" + channelId + '\'' +
6567
", videoLength=" + videoLength +
6668
", viewCount=" + viewCount +

kotlinYoutubeExtractor/src/main/java/com/maxrave/kotlinyoutubeextractor/YTExtractor.kt

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import java.util.regex.Pattern
3838
* @param LOGGING Enable logging, default is false.
3939
*/
4040

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

@@ -295,6 +295,7 @@ class YTExtractor(val con: Context, val CACHING: Boolean = false, val LOGGING: B
295295
}
296296
val videoDetails = ytPlayerResponse?.getJSONObject("videoDetails")
297297
if (videoDetails != null) {
298+
Log.d(LOG_TAG, "videoDetails: $videoDetails")
298299
videoMeta = VideoMeta(
299300
videoDetails.getString("videoId"),
300301
videoDetails.getString("title"),
@@ -622,43 +623,59 @@ class YTExtractor(val con: Context, val CACHING: Boolean = false, val LOGGING: B
622623
withContext(Dispatchers.IO) {
623624
ytFiles = async {
624625
state = State.LOADING
625-
var mat = patYouTubePageLink.matcher(videoId)
626-
if (mat.find()) {
627-
videoID = mat.group(3)
628-
} else {
629-
mat = patYouTubeShortLink.matcher(videoId)
626+
var retry: Int = 0
627+
while (state != State.SUCCESS && retry < retryCount) {
628+
if (LOGGING) Log.d(
629+
LOG_TAG,
630+
"Retry: $retry"
631+
)
632+
var mat = patYouTubePageLink.matcher(videoId)
630633
if (mat.find()) {
631634
videoID = mat.group(3)
632-
} else if (videoId.matches("\\p{Graph}+?".toRegex())) {
633-
videoID = videoId
635+
} else {
636+
mat = patYouTubeShortLink.matcher(videoId)
637+
if (mat.find()) {
638+
videoID = mat.group(3)
639+
} else if (videoId.matches("\\p{Graph}+?".toRegex())) {
640+
videoID = videoId
641+
}
634642
}
635-
}
636-
if (videoID != null) {
637-
try {
638-
state = State.SUCCESS
639-
val temp = getStreamUrls()
643+
if (videoID != null) {
640644
try {
641-
if (temp != null) {
642-
if (!testHttp403Code(temp.getAudioOnly().bestQuality()?.url)) {
643-
if (LOGGING) Log.d(
644-
LOG_TAG,
645-
"NO Error"
646-
)
647-
return@async getStreamUrls()
645+
val temp = getStreamUrls()
646+
try {
647+
if (temp != null) {
648+
val test = testHttp403Code(temp.getAudioOnly().bestQuality()?.url)
649+
if (!test) {
650+
if (LOGGING) Log.d(
651+
LOG_TAG,
652+
"NO Error"
653+
)
654+
state = State.SUCCESS
655+
return@async temp
656+
}
657+
else {
658+
retry++
659+
state = State.ERROR
660+
Log.e(LOG_TAG, "Extraction failed cause 403 HTTP Error")
661+
}
648662
}
649663
}
650-
}
651-
catch (e: java.lang.Exception){
664+
catch (e: IOException){
665+
retry++
666+
state = State.ERROR
667+
Log.e(LOG_TAG, "Extraction failed cause 403 HTTP Error", e)
668+
}
669+
} catch (e: java.lang.Exception) {
670+
retry++
652671
state = State.ERROR
653-
Log.e(LOG_TAG, "Extraction failed cause 403 HTTP Error", e)
672+
Log.e(LOG_TAG, "Extraction failed", e)
654673
}
655-
} catch (e: java.lang.Exception) {
674+
} else {
675+
retry++
656676
state = State.ERROR
657-
Log.e(LOG_TAG, "Extraction failed", e)
677+
Log.e(LOG_TAG, "Wrong YouTube link format")
658678
}
659-
} else {
660-
state = State.ERROR
661-
Log.e(LOG_TAG, "Wrong YouTube link format")
662679
}
663680
return@async null
664681
}.await()

0 commit comments

Comments
 (0)