diff --git a/app/src/main/java/com/deniscerri/ytdl/core/stream/StreamProcessExtractor.kt b/app/src/main/java/com/deniscerri/ytdl/core/stream/StreamProcessExtractor.kt index 015b51ae..539393ba 100644 --- a/app/src/main/java/com/deniscerri/ytdl/core/stream/StreamProcessExtractor.kt +++ b/app/src/main/java/com/deniscerri/ytdl/core/stream/StreamProcessExtractor.kt @@ -1,10 +1,10 @@ package com.deniscerri.ytdl.core.stream import android.util.Log +import java.io.BufferedReader import java.io.IOException import java.io.InputStream import java.io.InputStreamReader -import java.io.Reader import java.nio.charset.StandardCharsets import java.util.regex.Pattern @@ -25,10 +25,11 @@ internal class StreamProcessExtractor( override fun run() { try { + val reader = BufferedReader(InputStreamReader(stream, StandardCharsets.UTF_8), 8192) val currentLine = StringBuilder() var nextChar: Int - while (stream.read().also { nextChar = it } != -1) { + while (reader.read().also { nextChar = it } != -1) { val c = nextChar.toChar() if (c == '\r' || c == '\n') { diff --git a/app/src/main/java/com/deniscerri/ytdl/util/Extensions.kt b/app/src/main/java/com/deniscerri/ytdl/util/Extensions.kt index fc153334..19824f2b 100644 --- a/app/src/main/java/com/deniscerri/ytdl/util/Extensions.kt +++ b/app/src/main/java/com/deniscerri/ytdl/util/Extensions.kt @@ -640,31 +640,35 @@ object Extensions { downloadStartTime = timeInMillis } + private val YOUTUBE_URL_PATTERN = Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?)|(^(https?)://(www.)?piped.video)") + private val YOUTUBE_CHANNEL_URL_PATTERN = Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/@[a-zA-Z]+") + private val YOUTUBE_WATCH_VIDEOS_URL_PATTERN = Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/watch_videos\\?video_ids=.*") + private val SOUNDCLOUD_URL_PATTERN = Pattern.compile("""^(https?://)?(www\.|m\.)?soundcloud\.com/[\w\-.]+(/[\w\-.]+)*/?$""") + private val GENERAL_URL_PATTERN = Pattern.compile("(http|ftp|https)://([\\w_-]+(?:\\.[\\w_-]+)+)([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])") + fun TextWithSubtitle(title: String, subtitle: String) : Spanned { return HtmlCompat.fromHtml("" + title + "" + "
" + "" + subtitle + "" + "
", HtmlCompat.FROM_HTML_MODE_LEGACY) } fun String.isYoutubeURL() : Boolean { - return Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?)|(^(https?)://(www.)?piped.video)").matcher(this).find() + return YOUTUBE_URL_PATTERN.matcher(this).find() } fun String.isYoutubeChannelURL() : Boolean { - return Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/@[a-zA-Z]+").matcher(this).find() + return YOUTUBE_CHANNEL_URL_PATTERN.matcher(this).find() } fun String.isYoutubeWatchVideosURL() : Boolean { - return Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/watch_videos\\?video_ids=.*").matcher(this).find() + return YOUTUBE_WATCH_VIDEOS_URL_PATTERN.matcher(this).find() } fun String.isSoundCloudURL() : Boolean { - return Pattern.compile("""^(https?://)?(www\.|m\.)?soundcloud\.com/[\w\-.]+(/[\w\-.]+)*/?$""").matcher(this).find() + return SOUNDCLOUD_URL_PATTERN.matcher(this).find() } fun String.extractURL() : String { - val res = - Pattern.compile("(http|ftp|https)://([\\w_-]+(?:\\.[\\w_-]+)+)([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])") - .matcher(this) + val res = GENERAL_URL_PATTERN.matcher(this) return if (res.find()) { res.group() } else { @@ -673,7 +677,7 @@ object Extensions { } fun String.isURL(): Boolean { - return Pattern.compile("(http|ftp|https)://([\\w_-]+(?:\\.[\\w_-]+)+)([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])").matcher(this).find() + return GENERAL_URL_PATTERN.matcher(this).find() } fun combine( diff --git a/app/src/main/java/com/deniscerri/ytdl/work/YTDLPCoroutineWorker.kt b/app/src/main/java/com/deniscerri/ytdl/work/YTDLPCoroutineWorker.kt index 0824d482..3faaba11 100644 --- a/app/src/main/java/com/deniscerri/ytdl/work/YTDLPCoroutineWorker.kt +++ b/app/src/main/java/com/deniscerri/ytdl/work/YTDLPCoroutineWorker.kt @@ -39,13 +39,12 @@ abstract class YTDLPCoroutineWorker( runWork() } catch (e: Exception) { Result.failure() + } finally { + if (requiresServer) { + // Decrement job count & auto-stop service when 0 active jobs remain + BgUtilsPoTokenGeneratorUtil.releaseServer(context) + } } -// finally { -// if (requiresServer) { -// // Decrement job count & auto-stop service when 0 active jobs remain -// BgUtilsPoTokenGeneratorUtil.releaseServer(context) -// } -// } } private suspend fun isBgUtilsServerAlive(): Boolean = withContext(Dispatchers.IO) {