diff --git a/app/src/main/java/com/yausername/youtubedl_android_example/MainActivity.java b/app/src/main/java/com/yausername/youtubedl_android_example/MainActivity.java index 94e7cd8a..22bee96f 100644 --- a/app/src/main/java/com/yausername/youtubedl_android_example/MainActivity.java +++ b/app/src/main/java/com/yausername/youtubedl_android_example/MainActivity.java @@ -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; diff --git a/library/src/main/java/com/yausername/youtubedl_android/YoutubeDL.kt b/library/src/main/java/com/yausername/youtubedl_android/YoutubeDL.kt index 20e8da6e..260bd717 100644 --- a/library/src/main/java/com/yausername/youtubedl_android/YoutubeDL.kt +++ b/library/src/main/java/com/yausername/youtubedl_android/YoutubeDL.kt @@ -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 @@ -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() @@ -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) } @@ -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" diff --git a/library/src/main/java/com/yausername/youtubedl_android/YoutubeDLUpdater.kt b/library/src/main/java/com/yausername/youtubedl_android/YoutubeDLUpdater.kt index 6c161777..a4db9890 100644 --- a/library/src/main/java/com/yausername/youtubedl_android/YoutubeDLUpdater.kt +++ b/library/src/main/java/com/yausername/youtubedl_android/YoutubeDLUpdater.kt @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 } @@ -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] } } \ No newline at end of file