-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add KDocs and integrate Koin for dependency injection
- Loading branch information
Showing
15 changed files
with
406 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.abmo | ||
|
||
import com.abmo.common.Constants | ||
import com.abmo.common.Logger | ||
import com.abmo.model.Config | ||
import com.abmo.services.ProviderDispatcher | ||
import com.abmo.services.VideoDownloader | ||
import com.abmo.util.* | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import org.koin.core.parameter.parametersOf | ||
import java.io.File | ||
import kotlin.system.exitProcess | ||
|
||
class Application(private val args: Array<String>): KoinComponent { | ||
|
||
private val videoDownloader: VideoDownloader by inject() | ||
private val providerDispatcher: ProviderDispatcher by inject() | ||
private val cliArguments: CliArguments by inject { parametersOf(args) } | ||
|
||
suspend fun run() { | ||
|
||
val outputFileName = cliArguments.getOutputFileName() | ||
val headers = cliArguments.getHeaders() | ||
val numberOfConnections = cliArguments.getParallelConnections() | ||
Constants.VERBOSE = cliArguments.isVerboseEnabled() | ||
|
||
if (outputFileName != null && !isValidPath(outputFileName)) { | ||
exitProcess(0) | ||
} | ||
|
||
val scanner = java.util.Scanner(System.`in`) | ||
|
||
|
||
try { | ||
println("Enter the video URL or ID (e.g., K8R6OOjS7):") | ||
val videoUrl = scanner.nextLine() | ||
|
||
val dispatcher = providerDispatcher.getProviderForUrl(videoUrl) | ||
val videoID = dispatcher.getVideoID(videoUrl) | ||
|
||
val defaultHeader = if (videoUrl.isValidUrl()) { | ||
mapOf("Referer" to videoUrl?.extractReferer()) | ||
} else { emptyMap() } | ||
|
||
val url = "https://abysscdn.com/?v=$videoID" | ||
val videoMetadata = videoDownloader.getVideoMetaData(url, headers ?: defaultHeader) | ||
|
||
val videoSources = videoMetadata?.sources | ||
?.sortedBy { it?.label?.filter { char -> char.isDigit() }?.toIntOrNull() } | ||
|
||
if (videoSources == null) { | ||
Logger.error("Video with ID $videoID not found") | ||
exitProcess(0) | ||
} | ||
|
||
// For some reason ANSI applies to rest of text in the terminal starting from here | ||
// I'm not sure what causes that, so I removed all error logger here, and it still occurs | ||
// the only solution for now is to reset ANSI before displaying the message | ||
println("${Logger.RESET}Choose the resolution you want to download:") | ||
videoSources | ||
.forEachIndexed { index, video -> | ||
println("${index + 1}] ${video?.label} - ${video?.size.formatBytes()}") | ||
} | ||
|
||
val choice = scanner.nextInt() | ||
val resolution = videoSources[choice - 1]?.label | ||
|
||
|
||
if (resolution != null) { | ||
|
||
val defaultFileName = "${url.getParameter("v")}_${resolution}_${System.currentTimeMillis()}.mp4" | ||
val outputFile = outputFileName?.let { File(it) } ?: run { | ||
Logger.warn("No output file specified. The video will be saved to the current directory as '$defaultFileName'.\n") | ||
File(".", defaultFileName) // Default directory and name for saving video | ||
} | ||
|
||
val config = Config(url, resolution, outputFile, headers, numberOfConnections) | ||
Logger.info("video with id $videoID and resolution $resolution being processed...\n") | ||
videoDownloader.downloadSegmentsInParallel(config, videoMetadata) | ||
} | ||
} catch (e: NoSuchElementException) { | ||
println("\nCtrl + C detected. Exiting...") | ||
} | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,10 @@ | ||
package com.abmo | ||
|
||
import com.abmo.common.Constants | ||
import com.abmo.common.Logger | ||
import com.abmo.model.Config | ||
import com.abmo.services.ProviderDispatcher | ||
import com.abmo.services.VideoDownloader | ||
import com.abmo.crypto.CryptoHelper | ||
import com.abmo.executor.JavaScriptExecutor | ||
import com.abmo.util.* | ||
import java.io.File | ||
import kotlin.system.exitProcess | ||
import com.abmo.di.koinModule | ||
import org.koin.core.context.startKoin | ||
|
||
|
||
suspend fun main(args: Array<String>) { | ||
|
||
val javaScriptExecutor = JavaScriptExecutor() | ||
val cryptoHelper = CryptoHelper(javaScriptExecutor) | ||
val videoDownloader = VideoDownloader(cryptoHelper) | ||
val cliArguments = CliArguments(args) | ||
val providerDispatcher = ProviderDispatcher(javaScriptExecutor) | ||
|
||
|
||
val outputFileName = cliArguments.getOutputFileName(args) | ||
val headers = cliArguments.getHeaders() | ||
val numberOfConnections = cliArguments.getParallelConnections() | ||
Constants.VERBOSE = args.contains("--verbose") | ||
|
||
if (outputFileName != null && !isValidPath(outputFileName)) { | ||
exitProcess(0) | ||
} | ||
|
||
val scanner = java.util.Scanner(System.`in`) | ||
|
||
|
||
try { | ||
println("Enter the video URL or ID (e.g., K8R6OOjS7):") | ||
val videoUrl = scanner.nextLine() | ||
|
||
val dispatcher = providerDispatcher.getProviderForUrl(videoUrl) | ||
val videoID = dispatcher.getVideoID(videoUrl) | ||
|
||
val defaultHeader = if (videoUrl.isValidUrl()) { | ||
mapOf("Referer" to videoUrl?.extractReferer()) | ||
} else { emptyMap() } | ||
|
||
val url = "https://abysscdn.com/?v=$videoID" | ||
val videoMetadata = videoDownloader.getVideoMetaData(url, headers ?: defaultHeader) | ||
|
||
val videoSources = videoMetadata?.sources | ||
?.sortedBy { it?.label?.filter { char -> char.isDigit() }?.toIntOrNull() } | ||
|
||
if (videoSources == null) { | ||
Logger.error("Video with ID $videoID not found") | ||
exitProcess(0) | ||
} | ||
|
||
// For some reason ANSI applies to rest of text in the terminal starting from here | ||
// I'm not sure what causes that, so I removed all error logger here, and it still occurs | ||
// the only solution for now is to reset ANSI before displaying the message | ||
println("${Logger.RESET}Choose the resolution you want to download:") | ||
videoSources | ||
.forEachIndexed { index, video -> | ||
println("${index + 1}] ${video?.label} - ${formatBytes(video?.size)}") | ||
} | ||
|
||
val choice = scanner.nextInt() | ||
val resolution = videoSources[choice - 1]?.label | ||
|
||
|
||
if (resolution != null) { | ||
|
||
val defaultFileName = "${url.getParameter("v")}_${resolution}_${System.currentTimeMillis()}.mp4" | ||
val outputFile = outputFileName?.let { File(it) } ?: run { | ||
Logger.warn("No output file specified. The video will be saved to the current directory as '$defaultFileName'.\n") | ||
File(".", defaultFileName) // Default directory and name for saving video | ||
} | ||
|
||
val config = Config(url, resolution, outputFile, headers, numberOfConnections) | ||
Logger.info("video with id $videoID and resolution $resolution being processed...\n") | ||
videoDownloader.downloadSegmentsInParallel(config, videoMetadata) | ||
} | ||
} catch (e: NoSuchElementException) { | ||
println("\nCtrl + C detected. Exiting...") | ||
} | ||
|
||
|
||
startKoin { modules(koinModule) } | ||
Application(args).run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.