YT Manual Extraction Fixes

This commit is contained in:
Shabinder Singh 2021-10-13 01:20:41 +05:30
parent 8326767db2
commit 9685091c4b
3 changed files with 33 additions and 40 deletions

View File

@ -175,37 +175,23 @@ class FetchPlatformQueryResult(
if (downloadLink.isNullOrBlank()) { if (downloadLink.isNullOrBlank()) {
// Try Fetching Track from Available Sources // Try Fetching Track from Available Sources
val queryResult = saavnProvider.findBestSongDownloadURL( saavnProvider.findBestSongDownloadURL(
trackName = track.title, trackName = track.title,
trackArtists = track.artists, trackArtists = track.artists,
preferredQuality = preferredQuality preferredQuality = preferredQuality
).onSuccess { audioFormat = AudioFormat.MP4 }.flatMapError { saavnError -> ).onSuccess { (URL, quality) ->
audioFormat = AudioFormat.MP4
downloadLink = URL
audioQuality = quality
}.flatMapError { saavnError ->
appendPadded("Fetching From Saavn Failed:", saavnError.stackTraceToString()) appendPadded("Fetching From Saavn Failed:", saavnError.stackTraceToString())
// Saavn Failed, Lets Try Fetching Now From Youtube Music // Saavn Failed, Lets Try Fetching Now From Youtube Music
youtubeMusic.findMp3SongDownloadURLYT(track, preferredQuality).onSuccess { youtubeMusic.findSongDownloadURLYT(track, preferredQuality, this).onSuccess { (URL, quality, format) ->
audioFormat = AudioFormat.MP3 downloadLink = URL
}.flatMapError { audioQuality = quality
// Append Error To StackTrace audioFormat = format
appendPadded(
"Fetching From Yt1s Failed:",
it.stackTraceToString()
)
appendPadded("Extracting Manually...")
SuspendableEvent {
youtubeProvider.fetchVideoM4aLink(track.videoID.requireNotNull())
}.onFailure { throwable ->
appendPadded("YT Manual Extraction Failed!", throwable.stackTraceToString())
}.onSuccess {
audioFormat = AudioFormat.MP4
}
} }
} }
queryResult.component1()?.let {
downloadLink = it.first
audioQuality = it.second
}
} }
} }
return if (downloadLink.isNullOrBlank()) SuspendableEvent.error( return if (downloadLink.isNullOrBlank()) SuspendableEvent.error(

View File

@ -26,9 +26,12 @@ import com.shabinder.common.models.YoutubeTrack
import com.shabinder.common.models.corsApi import com.shabinder.common.models.corsApi
import com.shabinder.common.models.event.coroutines.SuspendableEvent import com.shabinder.common.models.event.coroutines.SuspendableEvent
import com.shabinder.common.models.event.coroutines.flatMap import com.shabinder.common.models.event.coroutines.flatMap
import com.shabinder.common.models.event.coroutines.flatMapError
import com.shabinder.common.models.event.coroutines.map import com.shabinder.common.models.event.coroutines.map
import com.shabinder.common.models.event.coroutines.onFailure
import com.shabinder.common.providers.youtube.YoutubeProvider import com.shabinder.common.providers.youtube.YoutubeProvider
import com.shabinder.common.providers.youtube_to_mp3.requests.YoutubeMp3 import com.shabinder.common.providers.youtube_to_mp3.requests.YoutubeMp3
import com.shabinder.common.utils.appendPadded
import io.github.shabinder.fuzzywuzzy.diffutils.FuzzySearch import io.github.shabinder.fuzzywuzzy.diffutils.FuzzySearch
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.request.headers import io.ktor.client.request.headers
@ -61,10 +64,11 @@ class YoutubeMusic constructor(
} }
// Get Downloadable Link // Get Downloadable Link
suspend fun findMp3SongDownloadURLYT( suspend fun findSongDownloadURLYT(
trackDetails: TrackDetails, trackDetails: TrackDetails,
preferredQuality: AudioQuality = fileManager.preferenceManager.audioQuality preferredQuality: AudioQuality = fileManager.preferenceManager.audioQuality,
): SuspendableEvent<Pair<String, AudioQuality>, Throwable> { errorReportBuilder: StringBuilder? = null
): SuspendableEvent<Triple<String, AudioQuality, AudioFormat>, Throwable> {
return getYTIDBestMatch(trackDetails).flatMap { videoID -> return getYTIDBestMatch(trackDetails).flatMap { videoID ->
// As YT compress Audio hence there is no benefit of quality for more than 192 // As YT compress Audio hence there is no benefit of quality for more than 192
val optimalQuality = val optimalQuality =
@ -72,18 +76,21 @@ class YoutubeMusic constructor(
?: 0) > 192 ?: 0) > 192
) AudioQuality.KBPS192 else preferredQuality ) AudioQuality.KBPS192 else preferredQuality
// 1 Try getting Link from Yt1s // 1 Try getting Link from Yt1s
youtubeMp3.getMp3DownloadLink(videoID, optimalQuality)/*.flatMapError { youtubeMp3.getMp3DownloadLink(videoID, optimalQuality).map {
Triple(it, optimalQuality, AudioFormat.MP3)
}.flatMapError {
errorReportBuilder?.appendPadded(
"Yt1sMp3 Failed for $videoID:", it.stackTraceToString()
)
// 2 if Yt1s failed , Extract Manually // 2 if Yt1s failed , Extract Manually
errorReportBuilder?.appendPadded("Extracting Manually...")
SuspendableEvent { SuspendableEvent {
youtubeProvider.ytDownloader.getVideo(videoID).get()?.url youtubeProvider.fetchVideoM4aLink(videoID)
?: throw SpotiFlyerException.YoutubeLinkNotFound( }.onFailure { throwable ->
videoID, errorReportBuilder?.appendPadded("YT Manual Extraction Failed!", throwable.stackTraceToString())
message = "Caught Following Errors While Finding Downloadable Link for $videoID : \n${it.stackTraceToString()}" }.map { (URL, quality) ->
) Triple(URL, quality, AudioFormat.MP4)
} }
}*/.map {
trackDetails.audioFormat = AudioFormat.MP3
Pair(it, optimalQuality)
} }
} }
} }
@ -203,7 +210,9 @@ class YoutubeMusic constructor(
*/ */
for (detailArray in result.subList(0, result.size - 1)) { for (detailArray in result.subList(0, result.size - 1)) {
for (detail in detailArray.jsonArray) { for (detail in detailArray.jsonArray) {
if ((detail.jsonObject["musicResponsiveListItemFlexColumnRenderer"]?.jsonObject?.size ?: 0) < 2) continue if ((detail.jsonObject["musicResponsiveListItemFlexColumnRenderer"]?.jsonObject?.size
?: 0) < 2
) continue
// if not a dummy, collect All Variables // if not a dummy, collect All Variables
val details = val details =

View File

@ -7,8 +7,6 @@ import com.shabinder.common.providers.utils.CommonUtils
import com.shabinder.common.providers.utils.SpotifyUtils import com.shabinder.common.providers.utils.SpotifyUtils
import com.shabinder.common.providers.utils.SpotifyUtils.toTrackDetailsList import com.shabinder.common.providers.utils.SpotifyUtils.toTrackDetailsList
import io.github.shabinder.runBlocking import io.github.shabinder.runBlocking
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.InternalSerializationApi
import kotlin.test.Test import kotlin.test.Test
@ -41,7 +39,7 @@ class TestSpotifyTrackMatching {
// val matched = CommonUtils.youtubeMusic.getYTTracks(CommonUtils.getYTQueryString(trackDetails)) // val matched = CommonUtils.youtubeMusic.getYTTracks(CommonUtils.getYTQueryString(trackDetails))
// println("YT-MATCHES: \n ${matched.component1()?.joinToString("\n")} \n") // println("YT-MATCHES: \n ${matched.component1()?.joinToString("\n")} \n")
val ytMatch = CommonUtils.youtubeMusic.findMp3SongDownloadURLYT(trackDetails) val ytMatch = CommonUtils.youtubeMusic.findSongDownloadURLYT(trackDetails)
println("YT MATCH: $ytMatch") println("YT MATCH: $ytMatch")
} }
} }