Skip to content

Commit

Permalink
Migrate to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Apr 12, 2023
1 parent ce44ea5 commit f6a69ac
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public void onClick(View v) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Update Channel")
.setItems(new String[]{"Stable Releases", "Nightly Releases"},
(dialogInterface, which) -> updateYoutubeDL(which == 0
? YoutubeDL.UpdateChannel.STABLE : YoutubeDL.UpdateChannel.NIGHTLY))
(dialogInterface, which) -> {
if (which == 0)
updateYoutubeDL(YoutubeDL.UpdateChannel._STABLE);
else updateYoutubeDL(YoutubeDL.UpdateChannel._NIGHTLY);
})
.create();
dialog.show();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.yausername.youtubedl_android

import android.content.Context
import android.os.Build
import android.util.Log
import com.fasterxml.jackson.databind.ObjectMapper
import com.yausername.youtubedl_android.YoutubeDLException
import com.yausername.youtubedl_android.mapper.VideoInfo
Expand Down Expand Up @@ -170,7 +169,7 @@ object YoutubeDL {
this["PATH"] = System.getenv("PATH") + ":" + binDir!!.absolutePath
this["PYTHONHOME"] = ENV_PYTHONHOME
this["HOME"] = ENV_PYTHONHOME
}.entries.forEach { Log.d("PythonInit", "[${it.key}] = ${it.value}") }
}

process = try {
processBuilder.start()
Expand Down Expand Up @@ -212,10 +211,13 @@ object YoutubeDL {

@Synchronized
@Throws(YoutubeDLException::class)
fun updateYoutubeDL(appContext: Context): UpdateStatus? {
fun updateYoutubeDL(
appContext: Context,
updateChannel: UpdateChannel = UpdateChannel.STABLE
): UpdateStatus? {
assertInit()
return try {
YoutubeDLUpdater.update(appContext)
YoutubeDLUpdater.update(appContext, updateChannel)
} catch (e: IOException) {
throw YoutubeDLException("failed to update youtube-dl", e)
}
Expand All @@ -225,10 +227,29 @@ object YoutubeDL {
return YoutubeDLUpdater.version(appContext)
}

fun versionName(appContext: Context?): String? {
return YoutubeDLUpdater.versionName(appContext)
}

enum class UpdateStatus {
DONE, ALREADY_UP_TO_DATE
}

sealed class UpdateChannel(val apiUrl: String) {
object STABLE : UpdateChannel("https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest")
object NIGHTLY :
UpdateChannel("https://api.github.com/repos/yt-dlp/yt-dlp-nightly-builds/releases/latest")

companion object {
@JvmField
val _STABLE: STABLE = STABLE

@JvmField
val _NIGHTLY: NIGHTLY = NIGHTLY
}
}


const val baseName = "youtubedl-android"
private const val packagesRoot = "packages"
private const val pythonBinName = "libpython.so"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package com.yausername.youtubedl_android
import android.content.Context
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ArrayNode
import com.yausername.youtubedl_android.YoutubeDL.UpdateChannel
import com.yausername.youtubedl_android.YoutubeDL.UpdateStatus
import com.yausername.youtubedl_android.YoutubeDLException
import com.yausername.youtubedl_android.YoutubeDL.getInstance
import com.yausername.youtubedl_common.SharedPrefsHelper
import com.yausername.youtubedl_common.SharedPrefsHelper.update
import org.apache.commons.io.FileUtils
Expand All @@ -13,15 +14,27 @@ import java.io.IOException
import java.net.URL

internal object YoutubeDLUpdater {
private const val releasesUrl = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest"
private const val youtubeDLVersionKey = "youtubeDLVersion"
private const val youtubeDLStableChannelUrl =
"https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest"
private const val youtubeDLNightlyChannelUrl =
"https://api.github.com/repos/yt-dlp/yt-dlp-nightly-builds/releases/latest"
private const val dlpBinaryName = "yt-dlp"
private const val dlpVersionKey = "dlpVersion"
private const val dlpVersionNameKey = "dlpVersionName"

@Throws(IOException::class, YoutubeDLException::class)
fun update(appContext: Context): UpdateStatus {
val json = checkForUpdate(appContext) ?: return UpdateStatus.ALREADY_UP_TO_DATE
internal fun update(
appContext: Context?,
youtubeDLChannel: UpdateChannel = UpdateChannel.STABLE
): UpdateStatus {
val json = checkForUpdate(appContext!!, youtubeDLChannel)
?: return UpdateStatus.ALREADY_UP_TO_DATE
val downloadUrl = getDownloadUrl(json)
val file = download(appContext, downloadUrl)
val ytdlpDir = getYoutubeDLDir(appContext)
val binary = File(ytdlpDir, "yt-dlp")
val ytdlpDir = getYoutubeDLDir(
appContext
)
val binary = File(ytdlpDir, dlpBinaryName)
try {
/* purge older version */
if (ytdlpDir.exists()) FileUtils.deleteDirectory(ytdlpDir)
Expand All @@ -30,25 +43,26 @@ internal object YoutubeDLUpdater {
} catch (e: Exception) {
/* if something went wrong restore default version */
FileUtils.deleteQuietly(ytdlpDir)
YoutubeDL.init_ytdlp(appContext, ytdlpDir)
getInstance().init_ytdlp(appContext, ytdlpDir)
throw YoutubeDLException(e)
} finally {
file.delete()
}
updateSharedPrefs(appContext, getTag(json))
updateSharedPrefs(appContext, getTag(json), getName(json))
return UpdateStatus.DONE
}

private fun updateSharedPrefs(appContext: Context, tag: String) {
update(appContext, youtubeDLVersionKey, tag)
private fun updateSharedPrefs(appContext: Context, tag: String, name: String) {
update(appContext, dlpVersionKey, tag)
update(appContext, dlpVersionNameKey, name)
}

@Throws(IOException::class)
private fun checkForUpdate(appContext: Context): JsonNode? {
val url = URL(releasesUrl)
val json: JsonNode = YoutubeDL.objectMapper.readTree(url)
private fun checkForUpdate(appContext: Context, youtubeDLChannel: UpdateChannel): JsonNode? {
val url = youtubeDLChannel.apiUrl
val json = YoutubeDL.objectMapper.readTree(url)
val newVersion = getTag(json)
val oldVersion = SharedPrefsHelper[appContext, youtubeDLVersionKey]
val oldVersion = SharedPrefsHelper[appContext, dlpVersionKey]
return if (newVersion == oldVersion) {
null
} else json
Expand All @@ -58,6 +72,10 @@ internal object YoutubeDLUpdater {
return json["tag_name"].asText()
}

private fun getName(json: JsonNode): String {
return json["name"].asText()
}

@Throws(YoutubeDLException::class)
private fun getDownloadUrl(json: JsonNode): String {
val assets = json["assets"] as ArrayNode
Expand All @@ -75,7 +93,7 @@ internal object YoutubeDLUpdater {
@Throws(IOException::class)
private fun download(appContext: Context, url: String): File {
val downloadUrl = URL(url)
val file = File.createTempFile("yt-dlp", null, appContext.cacheDir)
val file = File.createTempFile(dlpBinaryName, null, appContext.cacheDir)
FileUtils.copyURLToFile(downloadUrl, file, 5000, 10000)
return file
}
Expand All @@ -86,6 +104,10 @@ internal object YoutubeDLUpdater {
}

fun version(appContext: Context?): String? {
return SharedPrefsHelper[appContext!!, youtubeDLVersionKey]
return SharedPrefsHelper[appContext!!, dlpVersionKey]
}

fun versionName(appContext: Context?): String? {
return SharedPrefsHelper[appContext!!, dlpVersionNameKey]
}
}

0 comments on commit f6a69ac

Please sign in to comment.