Skip to content

Commit

Permalink
*Fixed Vimeo downloader error with some problem on ../
Browse files Browse the repository at this point in the history
  • Loading branch information
Udhayarajan committed Jan 27, 2023
1 parent 0dfc932 commit 078769a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugins {
}

group = "io.github.udhayarajan"
version = "2.0.3"
version = "2.0.4"
//Version Naming incremented if "<NEW_FEATURE_ADDED>.<WORKED_ON_BUG>.<BETA_VERSION_COUNT>"

repositories {
Expand Down
13 changes: 8 additions & 5 deletions src/commonMain/kotlin/com/mugames/vidsnapkit/MimeType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ class MimeType {
*/
const val APPLICATION_X_MPEG_URL = "application/x-mpegURL";

fun fromCodecs(codec: String) = with(codec) {
fun fromCodecs(codec: String, defaultCodec: String = "") = with(codec) {
when {
contains("mp4a", ignoreCase = true) -> MimeType.AUDIO_MP4
contains("opus", ignoreCase = true) -> MimeType.AUDIO_WEBM
contains("avc", ignoreCase = true) -> MimeType.VIDEO_MP4
contains("mp4a", ignoreCase = true) -> AUDIO_MP4
contains("opus", ignoreCase = true) -> AUDIO_WEBM
contains("avc", ignoreCase = true) -> VIDEO_MP4
contains("vp8", ignoreCase = true) || contains("vp9", ignoreCase = true) -> MimeType.VIDEO_WEBM
else -> throw Exception("Unable to find mimetype from codec $codec")
else ->{
println("Unable to find mimetype from codec $codec")
defaultCodec
}
}
}
}
Expand Down
35 changes: 24 additions & 11 deletions src/commonMain/kotlin/com/mugames/vidsnapkit/extractor/Vimeo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,23 @@ class Vimeo internal constructor(url: String) : Extractor(url) {
}

private fun extractFromCdns(response: String, cdnUrl: String) {
var url = cdnUrl
var baseUrl = response.toJSONObject().getString("base_url")
val baseUrl = response.toJSONObject().getString("base_url")
val videoArray = response.toJSONObject().getJSONArray("video")
val audioArray = response.toJSONObject().getJSONArray("audio")

fun getUrlAndMimeFromObject(jsonObject: JSONObject) =
val modified = goBackPossibly(baseUrl, cdnUrl).toMutableList()
modified[1] += "/${modified[0]}"

fun getUrlAndMimeFromObject(jsonObject: JSONObject) = run {
val tempList = goBackPossibly(jsonObject.getString("base_url"), modified[1]).toMutableList()
tempList[1] += "/${tempList[0]}"

listOf(
url + jsonObject.get("base_url") + jsonObject.get("id") + ".mp4",
MimeType.fromCodecs(jsonObject.getString("codecs"))
tempList[1] + jsonObject.get("id") + ".mp4",
MimeType.fromCodecs(jsonObject.getString("codecs"), jsonObject.getString("mime_type"))
)
}


fun extractVideoData() {
for (i in 0 until videoArray.length()) {
Expand Down Expand Up @@ -127,14 +134,20 @@ class Vimeo internal constructor(url: String) : Extractor(url) {
)
}
}
extractVideoData()
extractAudioData()
}

val backCount = baseUrl.split("../").dropLastWhile { it.isEmpty() }.toList().size
baseUrl = baseUrl.replace("../", "")
private fun goBackPossibly(baseUrl: String, mainUrl: String): List<String> {
val backCount = baseUrl.split("/").dropLastWhile { it.isEmpty() }.run {
if(all{it==".."}) size -1
else size
}
val tempBaseUrl = baseUrl.replace("../", "")
var tempMainUrl = mainUrl.dropLastWhile { it == '/' }
repeat(backCount) {
url = url.replace("(?:.(?!\\/))+\$".toRegex(), "")
tempMainUrl = tempMainUrl.substring(0, tempMainUrl.lastIndexOf("/"))
}
url += "/$baseUrl"
extractVideoData()
extractAudioData()
return listOf(tempBaseUrl, tempMainUrl.dropLastWhile { it == '/' })
}
}

0 comments on commit 078769a

Please sign in to comment.