From 1bd6d964eb502a6b1d435d618a3db6769610c587 Mon Sep 17 00:00:00 2001 From: ingbyr Date: Tue, 26 Sep 2017 11:06:57 +0800 Subject: [PATCH 1/5] check for updates Former-commit-id: 61d52bd4f9c0930c66d95ac4a9b5780ed2dc54c2 [formerly 2e0fb68dd1e41eb6acffff1737738cc8499d086c] Former-commit-id: 02baa9ed8b85b15d67adf7731342ccc76072724d --- build.gradle | 15 +++++-- .../core => core}/you-get.exe.REMOVED.git-id | 0 core/youtube-dl.exe.REMOVED.git-id | 1 + .../guiyouget/controllers/MainController.kt | 2 +- .../controllers/ProgressController.kt | 2 - .../controllers/UpdatesController.kt | 40 ++++++++++++------- .../ingbyr/guiyouget/core/OkHttpController.kt | 25 +++++++----- .../com/ingbyr/guiyouget/core/YouGet.kt | 3 +- .../com/ingbyr/guiyouget/core/YoutubeDL.kt | 3 +- .../com/ingbyr/guiyouget/utils/CoreUtils.kt | 12 ++++-- .../com/ingbyr/guiyouget/views/MainView.kt | 10 +++-- .../core/youtube-dl.exe.REMOVED.git-id | 1 - src/main/resources/fxml/MainWindow.fxml | 21 ++++++++-- 13 files changed, 89 insertions(+), 46 deletions(-) rename {src/main/resources/core => core}/you-get.exe.REMOVED.git-id (100%) create mode 100644 core/youtube-dl.exe.REMOVED.git-id delete mode 100644 src/main/resources/core/youtube-dl.exe.REMOVED.git-id diff --git a/build.gradle b/build.gradle index 9efdf26..ceb83d8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.ingbyr.guiyouget' -version '1.0-SNAPSHOT' +version '0.2' buildscript { ext.kotlin_version = '1.1.50' @@ -13,12 +13,21 @@ buildscript { } apply plugin: 'kotlin' -apply plugin: 'application' -mainClassName = "com.ingbyr.guiyouget.Main" +jar { + manifest { + attributes('Main-Class': 'com.ingbyr.guiyouget.MainKt') + } + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } +} + sourceCompatibility = 1.8 +sourceSets { + deploy +} + repositories { mavenCentral() jcenter() diff --git a/src/main/resources/core/you-get.exe.REMOVED.git-id b/core/you-get.exe.REMOVED.git-id similarity index 100% rename from src/main/resources/core/you-get.exe.REMOVED.git-id rename to core/you-get.exe.REMOVED.git-id diff --git a/core/youtube-dl.exe.REMOVED.git-id b/core/youtube-dl.exe.REMOVED.git-id new file mode 100644 index 0000000..3dab9aa --- /dev/null +++ b/core/youtube-dl.exe.REMOVED.git-id @@ -0,0 +1 @@ +772ac2cc9a9eabd17bcb3d0c14d787d48dd5c941 \ No newline at end of file diff --git a/src/main/kotlin/com/ingbyr/guiyouget/controllers/MainController.kt b/src/main/kotlin/com/ingbyr/guiyouget/controllers/MainController.kt index fa0e9f8..0942942 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/controllers/MainController.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/controllers/MainController.kt @@ -36,6 +36,6 @@ class MainController : Controller() { fun updateGUI() { - + hostServices.showDocument("https://github.com/ingbyr/GUI-YouGet/releases/latest") } } \ No newline at end of file diff --git a/src/main/kotlin/com/ingbyr/guiyouget/controllers/ProgressController.kt b/src/main/kotlin/com/ingbyr/guiyouget/controllers/ProgressController.kt index d50f9b7..bd452f9 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/controllers/ProgressController.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/controllers/ProgressController.kt @@ -1,6 +1,5 @@ package com.ingbyr.guiyouget.controllers -import com.ingbyr.guiyouget.core.OkHttpController import com.ingbyr.guiyouget.core.YouGet import com.ingbyr.guiyouget.core.YoutubeDL import com.ingbyr.guiyouget.events.DownloadingRequestWithYouGet @@ -10,7 +9,6 @@ import tornadofx.* class ProgressController : Controller() { val logger = LoggerFactory.getLogger(this::class.java) - val okhttp = OkHttpController() fun subscribeEvents() { subscribe { diff --git a/src/main/kotlin/com/ingbyr/guiyouget/controllers/UpdatesController.kt b/src/main/kotlin/com/ingbyr/guiyouget/controllers/UpdatesController.kt index 61c10e6..c56e482 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/controllers/UpdatesController.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/controllers/UpdatesController.kt @@ -7,11 +7,11 @@ import com.ingbyr.guiyouget.events.UpdateStates import com.ingbyr.guiyouget.utils.CoreUtils import org.slf4j.LoggerFactory import tornadofx.* -import java.io.File +import java.nio.file.Paths -class UpdatesController: Controller() { - val logger = LoggerFactory.getLogger(this::class.java) - val okhttp = OkHttpController() +class UpdatesController : Controller() { + private val logger = LoggerFactory.getLogger(this::class.java) + private val okhttp = OkHttpController() fun subscribeEvents() { subscribe { @@ -19,7 +19,7 @@ class UpdatesController: Controller() { val remoteJson = okhttp.requestJson(CoreUtils.REMOTE_CONF_URL) if (remoteJson != null) { val youget = remoteJson["youget"] as String - logger.debug("get remote version file url $youget") + logger.debug("[you-get] remote version url $youget") doUpdates(CoreUtils.YOU_GET, youget) } else { //todo update failed @@ -27,11 +27,11 @@ class UpdatesController: Controller() { } subscribe { - fire(UpdateStates("Check for updates of youyube-dl")) + fire(UpdateStates("Check for updates of youtube-dl")) val remoteJson = okhttp.requestJson(CoreUtils.REMOTE_CONF_URL) if (remoteJson != null) { val youtubedl = remoteJson["youtubedl"] as String - logger.debug("get remote version file url $youtubedl") + logger.debug("[youtube-dl] remote version url $youtubedl") doUpdates(CoreUtils.YOUTUBE_DL, youtubedl) } else { //todo update failed @@ -46,26 +46,36 @@ class UpdatesController: Controller() { when (core) { CoreUtils.YOU_GET -> { val localVersion = app.config["you-get-version"] as String - logger.debug("remote version is $remoteVersion, local version is $localVersion") + logger.debug("[you-get] remote version is $remoteVersion, local version is $localVersion") if (remoteVersion > localVersion) { // do updates - fire(UpdateStates("New version $remoteVersion of you-get, downloading")) + fire(UpdateStates("New version $remoteVersion of you-get, downloading...")) val url = CoreUtils.yougetUpdateURL(remoteVersion) - logger.debug("you-get update url $url") - okhttp.downloadFile(url, File(MainController::class.java.getResource("/core/you-get.exe").toURI())) + logger.debug("[you-get] update url $url") + okhttp.downloadFile(url, + Paths.get(System.getProperty("user.dir"), "core", "you-get.exe").toFile(), + CoreUtils.YOU_GET_VERSION, + remoteVersion) + } else { + logger.debug("[you-get] no updates") } } CoreUtils.YOUTUBE_DL -> { val localVersion = app.config["youtube-dl-version"] as String + logger.debug("[youtube-dl] remote version is $remoteVersion, local version is $localVersion") if (remoteVersion > localVersion) { // do updates - fire(UpdateStates("New version $remoteVersion of youtube-dl, downloading")) + fire(UpdateStates("New version $remoteVersion of youtube-dl, downloading...")) val url = CoreUtils.youtubedlUpdateURL(remoteVersion) - logger.debug("youtube-dl update url $url") - okhttp.downloadFile(url, File(MainController::class.java.getResource("/core/youtube-dl.exe").toURI())) + logger.debug("[youtube-dl] update url $url") + okhttp.downloadFile(url, + Paths.get(System.getProperty("user.dir"), "core", "youtube-dl.exe").toFile(), + CoreUtils.YOUTUBE_DL_VERSION, + remoteVersion) + } else { + logger.debug("[youtube-dl] no updates") } } } - } } \ No newline at end of file diff --git a/src/main/kotlin/com/ingbyr/guiyouget/core/OkHttpController.kt b/src/main/kotlin/com/ingbyr/guiyouget/core/OkHttpController.kt index ed10dc0..4762131 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/core/OkHttpController.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/core/OkHttpController.kt @@ -2,6 +2,7 @@ package com.ingbyr.guiyouget.core import com.beust.klaxon.JsonObject import com.beust.klaxon.Parser +import com.ingbyr.guiyouget.events.UpdateStates import okhttp3.* import org.slf4j.LoggerFactory import tornadofx.* @@ -11,9 +12,9 @@ import java.io.IOException class OkHttpController : Controller() { - val logger = LoggerFactory.getLogger(this::class.java) - val client = OkHttpClient() - val parser = Parser() + private val logger = LoggerFactory.getLogger(this::class.java) + private val client = OkHttpClient() + private val parser = Parser() fun requestString(url: String): String? { val request = Request.Builder().get().url(url).build() @@ -37,17 +38,18 @@ class OkHttpController : Controller() { } } - fun downloadFile(url: String, file: File) { + fun downloadFile(url: String, file: File, k: String? = null, v: String? = null) { val request = Request.Builder().url(url).build() logger.debug("save file to ${file.absolutePath}") - client.newCall(request).enqueue(DownloadFileCallBack(file)) + client.newCall(request).enqueue(DownloadFileCallBack(file, k, v)) } } -class DownloadFileCallBack(var file: File) : Callback, Controller() { - val logger = LoggerFactory.getLogger(this::class.java) +class DownloadFileCallBack(private val file: File, private val k: String?, private val v: String?) : Callback, Controller() { + private val logger = LoggerFactory.getLogger(this::class.java) override fun onFailure(call: Call?, e: IOException?) { + fire(UpdateStates("Updating failed")) logger.error(e.toString()) } @@ -74,13 +76,18 @@ class DownloadFileCallBack(var file: File) : Callback, Controller() { } if (bytesRead == -1) break process += bytesRead - println(process) + logger.trace(process.toString()) os.write(buffer, 0, bytesRead) } while (true) } catch (e: Exception) { logger.error(e.toString()) } - + fire(UpdateStates("Complete updates")) + // Update config of APP + if (k != null && v != null) { + app.config[k] = v + app.config.save() + } } } \ No newline at end of file diff --git a/src/main/kotlin/com/ingbyr/guiyouget/core/YouGet.kt b/src/main/kotlin/com/ingbyr/guiyouget/core/YouGet.kt index c1f5296..cec40cc 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/core/YouGet.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/core/YouGet.kt @@ -6,11 +6,10 @@ import com.ingbyr.guiyouget.events.StopDownloading import com.ingbyr.guiyouget.events.UpdateProgressWithYouGet import java.io.BufferedReader import java.io.InputStreamReader -import java.nio.file.Files import java.nio.file.Paths class YouGet(val url: String) : CoreController() { - val core = this::class.java.getResource("/core/you-get.exe").path + val core = Paths.get(System.getProperty("user.dir"), "core", "you-get.exe").toAbsolutePath().toString() val parser = Parser() private var progress = 0.0 private var speed = "0MB/s" diff --git a/src/main/kotlin/com/ingbyr/guiyouget/core/YoutubeDL.kt b/src/main/kotlin/com/ingbyr/guiyouget/core/YoutubeDL.kt index 7e31050..76afa6d 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/core/YoutubeDL.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/core/YoutubeDL.kt @@ -6,13 +6,12 @@ import com.ingbyr.guiyouget.events.StopDownloading import com.ingbyr.guiyouget.events.UpdateProgressWithYoutubeDL import java.io.BufferedReader import java.io.InputStreamReader -import java.nio.file.Files import java.nio.file.Paths class YoutubeDL(private val url: String) : CoreController() { - private val core = this::class.java.getResource("/core/youtube-dl.exe").path + val core = Paths.get(System.getProperty("user.dir"), "core", "youtube-dl.exe").toAbsolutePath().toString() private val parser = Parser() private var progress = 0.0 private var speed = "0MiB/s" diff --git a/src/main/kotlin/com/ingbyr/guiyouget/utils/CoreUtils.kt b/src/main/kotlin/com/ingbyr/guiyouget/utils/CoreUtils.kt index 5ef9f39..0e360f7 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/utils/CoreUtils.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/utils/CoreUtils.kt @@ -3,10 +3,14 @@ package com.ingbyr.guiyouget.utils object CoreUtils { lateinit var current: String var REMOTE_CONF_URL = "https://raw.githubusercontent.com/ingbyr/GUI-YouGet/master/RemoteConf.json" - val YOUTUBE_DL = "YOUTUBE_DL" - val YOU_GET = "YOU_GET" + val YOUTUBE_DL = "youtube-dl" + val YOUTUBE_DL_VERSION = "youtube-dl-version" + val YOU_GET = "you-get" + val YOU_GET_VERSION = "you-get-version" - fun yougetUpdateURL(ver:String) = "https://github.com/soimort/you-get/releases/download/v$ver/you-get-$ver-win32.exe" + val APP_VERSION = "version" - fun youtubedlUpdateURL(ver:String) = "https://github.com/rg3/youtube-dl/releases/download/$ver/youtube-dl.exe" + fun yougetUpdateURL(ver: String) = "https://github.com/soimort/you-get/releases/download/v$ver/you-get-$ver-win32.exe" + + fun youtubedlUpdateURL(ver: String) = "https://github.com/rg3/youtube-dl/releases/download/$ver/youtube-dl.exe" } \ No newline at end of file diff --git a/src/main/kotlin/com/ingbyr/guiyouget/views/MainView.kt b/src/main/kotlin/com/ingbyr/guiyouget/views/MainView.kt index c9e0d37..ce2ceb8 100644 --- a/src/main/kotlin/com/ingbyr/guiyouget/views/MainView.kt +++ b/src/main/kotlin/com/ingbyr/guiyouget/views/MainView.kt @@ -28,7 +28,6 @@ class MainView : View("GUI-YouGet") { private val tfURL: JFXTextField by fxid() private val labelStoragePath: Label by fxid() - private val labelCoreVersion: Label by fxid() private val labelVersion: Label by fxid() private val labelYoutubeDL: Label by fxid() private val labelYouGet: Label by fxid() @@ -110,9 +109,12 @@ class MainView : View("GUI-YouGet") { } } - // Load core version - labelYouGet.text = app.config["you-get-version"] as String - labelYoutubeDL.text = app.config["youtube-dl-version"] as String + // Init core version + labelYouGet.text = app.config[CoreUtils.YOU_GET_VERSION] as String + labelYoutubeDL.text = app.config[CoreUtils.YOUTUBE_DL_VERSION] as String + + // Init app version + labelVersion.text = app.config[CoreUtils.APP_VERSION] as String // Change download core cbYouGet.action { diff --git a/src/main/resources/core/youtube-dl.exe.REMOVED.git-id b/src/main/resources/core/youtube-dl.exe.REMOVED.git-id deleted file mode 100644 index a733b1b..0000000 --- a/src/main/resources/core/youtube-dl.exe.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -65b7746b64a6db783a45d837fab2343da4e61a8e \ No newline at end of file diff --git a/src/main/resources/fxml/MainWindow.fxml b/src/main/resources/fxml/MainWindow.fxml index 99c21a7..b2e3a5d 100644 --- a/src/main/resources/fxml/MainWindow.fxml +++ b/src/main/resources/fxml/MainWindow.fxml @@ -96,12 +96,22 @@ -