Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
Former-commit-id: 9bed7d2a575582df6aee4067a980c8ff9fa9f9fd [formerly 10a46e0]
Former-commit-id: a7de363b9da5c579999daf1ff3daf0d736560ba6
  • Loading branch information
ingbyr committed Sep 24, 2017
2 parents 36d5ced + a6915de commit e1b77c9
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 36 deletions.
4 changes: 2 additions & 2 deletions RemoteConf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"youget": "https://raw.githubusercontent.com/rg3/youtube-dl/master/youtube_dl/version.py",
"youtubedl": "https://raw.githubusercontent.com/soimort/you-get/master/src/you_get/version.py",
"youget": "https://raw.githubusercontent.com/soimort/you-get/master/src/you_get/version.py",
"youtubedl": "https://raw.githubusercontent.com/rg3/youtube-dl/master/youtube_dl/version.py",
"app-version": "0.2"
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mainClassName = "com.ingbyr.guiyouget.Main"
sourceCompatibility = 1.8

repositories {
// mavenCentral()
mavenCentral()
jcenter()
}

Expand All @@ -30,7 +30,7 @@ dependencies {
compile 'com.jfoenix:jfoenix:1.4.0'
compile 'com.beust:klaxon:0.30'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile 'com.github.kittinunf.fuel:fuel:1.10.0' //for JVM
compile 'com.squareup.okhttp3:okhttp:3.9.0'
}

compileKotlin {
Expand Down
Binary file removed screenshot/fileDialog.png
Binary file not shown.
Binary file removed screenshot/mainWindow.png
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.ingbyr.guiyouget.controllers

import com.ingbyr.guiyouget.events.RequestMediasWithYoutubeDL
import com.ingbyr.guiyouget.core.CoreContents
import com.ingbyr.guiyouget.core.YouGet
import com.ingbyr.guiyouget.core.YoutubeDL
import com.ingbyr.guiyouget.events.RequestMediasWithYouGet
import com.ingbyr.guiyouget.events.RequestMediasWithYoutubeDL
import com.ingbyr.guiyouget.utils.CoreUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import tornadofx.*


class MainController : Controller() {
private val logger: Logger = LoggerFactory.getLogger(MainController::class.java)

Expand All @@ -18,11 +19,11 @@ class MainController : Controller() {

// Init the request args
when (core) {
CoreContents.YOUTUBE_DL -> {
CoreUtils.YOUTUBE_DL -> {
fire(RequestMediasWithYoutubeDL(YoutubeDL(url)))
}

CoreContents.YOU_GET -> {
CoreUtils.YOU_GET -> {
fire(RequestMediasWithYouGet(YouGet(url)))
}

Expand All @@ -33,10 +34,6 @@ class MainController : Controller() {

}

fun updateCore() {
// TODO: check updates
// https://raw.githubusercontent.com/rg3/youtube-dl/master/youtube_dl/version.py
}

fun updateGUI() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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
import com.ingbyr.guiyouget.events.DownloadingRequestWithYoutubeDL
import com.ingbyr.guiyouget.core.YoutubeDL
import org.slf4j.LoggerFactory
import tornadofx.*

class ProgressController : Controller() {
val logger = LoggerFactory.getLogger(this::class.java)
val okhttp = OkHttpController()

fun subscribeEvents() {
subscribe<DownloadingRequestWithYoutubeDL> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.ingbyr.guiyouget.controllers

import com.ingbyr.guiyouget.core.OkHttpController
import com.ingbyr.guiyouget.events.RequestCheckUpdatesYouGet
import com.ingbyr.guiyouget.events.RequestCheckUpdatesYoutubeDL
import com.ingbyr.guiyouget.events.UpdateStates
import com.ingbyr.guiyouget.utils.CoreUtils
import org.slf4j.LoggerFactory
import tornadofx.*
import java.io.File

class UpdatesController: Controller() {
val logger = LoggerFactory.getLogger(this::class.java)
val okhttp = OkHttpController()

fun subscribeEvents() {
subscribe<RequestCheckUpdatesYouGet> {
fire(UpdateStates("Check for updates of you-get"))
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")
doUpdates(CoreUtils.YOU_GET, youget)
} else {
//todo update failed
}
}

subscribe<RequestCheckUpdatesYoutubeDL> {
fire(UpdateStates("Check for updates of youyube-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")
doUpdates(CoreUtils.YOUTUBE_DL, youtubedl)
} else {
//todo update failed
}
}
}

private fun doUpdates(core: String, url: String) {
val vStr = okhttp.requestString(url)
val v = Regex("'\\d+.+'").findAll(vStr.toString()).toList().flatMap(MatchResult::groupValues)
val remoteVersion = v.first().substring(1, v.first().length - 1)
when (core) {
CoreUtils.YOU_GET -> {
val localVersion = app.config["you-get-version"] as String
logger.debug("remote version is $remoteVersion, local version is $localVersion")
if (remoteVersion > localVersion) {
// do updates
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()))
}
}
CoreUtils.YOUTUBE_DL -> {
val localVersion = app.config["youtube-dl-version"] as String
if (remoteVersion > localVersion) {
// do updates
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()))
}
}
}

}
}
7 changes: 0 additions & 7 deletions src/main/kotlin/com/ingbyr/guiyouget/core/CoreContents.kt

This file was deleted.

86 changes: 86 additions & 0 deletions src/main/kotlin/com/ingbyr/guiyouget/core/OkHttpController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.ingbyr.guiyouget.core

import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import okhttp3.*
import org.slf4j.LoggerFactory
import tornadofx.*
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


class OkHttpController : Controller() {
val logger = LoggerFactory.getLogger(this::class.java)
val client = OkHttpClient()
val parser = Parser()

fun requestString(url: String): String? {
val request = Request.Builder().get().url(url).build()
var msg: String? = null
client.newCall(request).execute().let { response ->
msg = response.body()?.string()
}
return msg
}

fun requestJson(url: String): JsonObject? {
val request = Request.Builder().get().url(url).build()
var msg: String? = null
client.newCall(request).execute().let { response ->
msg = response.body()?.string()
}
if (msg != null) {
return parser.parse(StringBuilder(msg)) as JsonObject
} else {
return null
}
}

fun downloadFile(url: String, file: File) {
val request = Request.Builder().url(url).build()
logger.debug("save file to ${file.absolutePath}")
client.newCall(request).enqueue(DownloadFileCallBack(file))
}
}

class DownloadFileCallBack(var file: File) : Callback, Controller() {
val logger = LoggerFactory.getLogger(this::class.java)

override fun onFailure(call: Call?, e: IOException?) {
logger.error(e.toString())
}

override fun onResponse(call: Call?, response: Response) {
if (!response.isSuccessful) throw IOException("Unexpected code " + response)
val byteStream = response.body()?.byteStream()
val length = response.body()?.contentLength()
logger.debug("length $length")
val os: FileOutputStream
try {
os = FileOutputStream(file)
} catch (e: Exception) {
logger.error(e.toString())
return
}

var bytesRead = -1
val buffer = ByteArray(2048)
var process = 0L
try {
do {
if (byteStream != null) {
bytesRead = byteStream.read(buffer)
}
if (bytesRead == -1) break
process += bytesRead
println(process)
os.write(buffer, 0, bytesRead)
} while (true)
} catch (e: Exception) {
logger.error(e.toString())

}

}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/com/ingbyr/guiyouget/core/YouGet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class YouGet(val url: String) : CoreController() {
var line: String?
val args = CoreArgs(core)
args.add("foramtID", "--itag=$formatID")
args.add("-o", app.config["storagePath"] as String)
args.add("url", url)
val builder = ProcessBuilder(args.build())
builder.redirectErrorStream(true)
Expand All @@ -63,7 +64,7 @@ class YouGet(val url: String) : CoreController() {
}

override fun parseStatus(line: String) {
val p = Regex("\\s*\\d+%").findAll(line).toList().flatMap(MatchResult::groupValues)
val p = Regex("\\d+\\.*\\d*%").findAll(line).toList().flatMap(MatchResult::groupValues)
if (p.isNotEmpty()) {
progress = p[0].subSequence(0, p[0].length - 1).toString().toDouble()
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/ingbyr/guiyouget/core/YoutubeDL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class YoutubeDL(private val url: String) : CoreController() {
private var extTime = "00:00"
private var status = "Analyzing..."
private var isDownloading = false
private val outputTemplate = "%(title)s.%(ext)s"

init {
subscribe<StopDownloading> {
Expand All @@ -45,8 +46,10 @@ class YoutubeDL(private val url: String) : CoreController() {
isDownloading = true
var line: String?
val args = CoreArgs(core)
// todo remove proxy from this
args.add("--proxy", "socks5://127.0.0.1:1080/")
args.add("-f", formatID)
args.add("-o", Paths.get(app.config["storagePath"] as String, outputTemplate).toString())
args.add("url", url)
val builder = ProcessBuilder(args.build())
builder.redirectErrorStream(true)
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/com/ingbyr/guiyouget/events/UpdatesEvents.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ingbyr.guiyouget.events

import tornadofx.*

// common event
class UpdateStates(val status: String) : FXEvent()

// you-get updates
object RequestCheckUpdatesYouGet : FXEvent(EventBus.RunOn.BackgroundThread)

object UpdateYouGet : FXEvent(EventBus.RunOn.BackgroundThread)

// you-get updates
object RequestCheckUpdatesYoutubeDL : FXEvent(EventBus.RunOn.BackgroundThread)

object UpdateYoutubeDL : FXEvent(EventBus.RunOn.BackgroundThread)

12 changes: 12 additions & 0 deletions src/main/kotlin/com/ingbyr/guiyouget/utils/CoreUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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"

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"
}
24 changes: 14 additions & 10 deletions src/main/kotlin/com/ingbyr/guiyouget/views/MainView.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ingbyr.guiyouget.views

import com.ingbyr.guiyouget.controllers.MainController
import com.ingbyr.guiyouget.core.CoreContents
import com.ingbyr.guiyouget.events.RequestCheckUpdatesYouGet
import com.ingbyr.guiyouget.events.RequestCheckUpdatesYoutubeDL
import com.ingbyr.guiyouget.utils.CoreUtils
import com.jfoenix.controls.JFXButton
import com.jfoenix.controls.JFXCheckBox
import com.jfoenix.controls.JFXTextField
Expand Down Expand Up @@ -92,19 +94,19 @@ class MainView : View("GUI-YouGet") {
// Load download core config
val core = app.config["core"]
when (core) {
CoreContents.YOUTUBE_DL -> {
CoreUtils.YOUTUBE_DL -> {
cbYoutubeDL.isSelected = true
CoreContents.current = CoreContents.YOUTUBE_DL
CoreUtils.current = CoreUtils.YOUTUBE_DL
}
CoreContents.YOU_GET -> {
CoreUtils.YOU_GET -> {
cbYouGet.isSelected = true
CoreContents.current = CoreContents.YOU_GET
CoreUtils.current = CoreUtils.YOU_GET
}
else -> {
app.config["core"] = CoreContents.YOUTUBE_DL
app.config["core"] = CoreUtils.YOUTUBE_DL
app.config.save()
cbYoutubeDL.isSelected = true
CoreContents.current = CoreContents.YOUTUBE_DL
CoreUtils.current = CoreUtils.YOUTUBE_DL
}
}

Expand All @@ -116,22 +118,24 @@ class MainView : View("GUI-YouGet") {
cbYouGet.action {
if (cbYouGet.isSelected) {
cbYoutubeDL.isSelected = false
app.config["core"] = CoreContents.YOU_GET
app.config["core"] = CoreUtils.YOU_GET
app.config.save()
}
}

cbYoutubeDL.action {
if (cbYoutubeDL.isSelected) {
cbYouGet.isSelected = false
app.config["core"] = CoreContents.YOUTUBE_DL
app.config["core"] = CoreUtils.YOUTUBE_DL
app.config.save()
}
}

// Updates listener
btnUpdateCore.setOnMouseClicked {
controller.updateCore()
UpdatesWindow().openModal(StageStyle.UNDECORATED)
fire(RequestCheckUpdatesYouGet)
fire(RequestCheckUpdatesYoutubeDL)
}

btnUpdate.setOnMouseClicked {
Expand Down
Loading

0 comments on commit e1b77c9

Please sign in to comment.