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()) {
// Try Fetching Track from Available Sources
val queryResult = saavnProvider.findBestSongDownloadURL(
saavnProvider.findBestSongDownloadURL(
trackName = track.title,
trackArtists = track.artists,
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())
// Saavn Failed, Lets Try Fetching Now From Youtube Music
youtubeMusic.findMp3SongDownloadURLYT(track, preferredQuality).onSuccess {
audioFormat = AudioFormat.MP3
}.flatMapError {
// Append Error To StackTrace
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
}
youtubeMusic.findSongDownloadURLYT(track, preferredQuality, this).onSuccess { (URL, quality, format) ->
downloadLink = URL
audioQuality = quality
audioFormat = format
}
}
queryResult.component1()?.let {
downloadLink = it.first
audioQuality = it.second
}
}
}
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.event.coroutines.SuspendableEvent
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.onFailure
import com.shabinder.common.providers.youtube.YoutubeProvider
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.ktor.client.HttpClient
import io.ktor.client.request.headers
@ -61,10 +64,11 @@ class YoutubeMusic constructor(
}
// Get Downloadable Link
suspend fun findMp3SongDownloadURLYT(
suspend fun findSongDownloadURLYT(
trackDetails: TrackDetails,
preferredQuality: AudioQuality = fileManager.preferenceManager.audioQuality
): SuspendableEvent<Pair<String, AudioQuality>, Throwable> {
preferredQuality: AudioQuality = fileManager.preferenceManager.audioQuality,
errorReportBuilder: StringBuilder? = null
): SuspendableEvent<Triple<String, AudioQuality, AudioFormat>, Throwable> {
return getYTIDBestMatch(trackDetails).flatMap { videoID ->
// As YT compress Audio hence there is no benefit of quality for more than 192
val optimalQuality =
@ -72,18 +76,21 @@ class YoutubeMusic constructor(
?: 0) > 192
) AudioQuality.KBPS192 else preferredQuality
// 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
errorReportBuilder?.appendPadded("Extracting Manually...")
SuspendableEvent {
youtubeProvider.ytDownloader.getVideo(videoID).get()?.url
?: throw SpotiFlyerException.YoutubeLinkNotFound(
videoID,
message = "Caught Following Errors While Finding Downloadable Link for $videoID : \n${it.stackTraceToString()}"
)
youtubeProvider.fetchVideoM4aLink(videoID)
}.onFailure { throwable ->
errorReportBuilder?.appendPadded("YT Manual Extraction Failed!", throwable.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 (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
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.toTrackDetailsList
import io.github.shabinder.runBlocking
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import kotlinx.serialization.InternalSerializationApi
import kotlin.test.Test
@ -41,7 +39,7 @@ class TestSpotifyTrackMatching {
// val matched = CommonUtils.youtubeMusic.getYTTracks(CommonUtils.getYTQueryString(trackDetails))
// 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")
}
}