Skip to content

Commit 44ab303

Browse files
committed
Add support for custom headers in CLI arguments
1 parent 3f2937c commit 44ab303

5 files changed

Lines changed: 67 additions & 16 deletions

File tree

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ To download a video using AbyssVideoDownloader:
2828
1. Run the following command:
2929

3030
```bash
31-
java -jar abyss-dl.jar [-o <output_file_path>]
31+
java -jar mycli.jar [-H <header>] [--header <header>] [-o <output_file_path>]
3232
```
3333

34-
- The `-o <output_file_path>` flag is optional. If not specified, the file will be saved in the Downloads directory with a default name.
35-
- The `output_file_path` can either be the file name or the full path (including the file name) where you want the video to be saved.
34+
- The `-H <header>` or `--header <header>` flag allows you to add HTTP headers in the format `Header-Name: Header-Value`.
35+
- You can specify multiple headers by repeating the `-H` or `--header` flag.
36+
- The `-o <output_file_path>` flag is optional. If not specified, the file will be saved in the Downloads directory with a default name.
37+
- The `output_file_path` can either be the file name or the full path (including the file name) where you want the video to be saved.
3638

3739
2. After running the command, the application will prompt you to input the video URL or the ID from Abyss.
3840

@@ -50,7 +52,13 @@ To download a video using AbyssVideoDownloader:
5052
java -jar abyss-dl.jar
5153
```
5254

53-
3. After running the command, you will be prompted for the video URL:
55+
3. **Make an HTTP request with custom headers to download a video:**
56+
57+
```bash
58+
java -jar mycli.jar -H "Ref: Bearer TOKEN" --header "Referer: https://example.com" -o my_video.mp4
59+
```
60+
61+
4. After running the command, you will be prompted for the video URL:
5462

5563
```
5664
enter the video URL or ID: https://abysscdn.com/?v=K8R6OOjS7

src/main/kotlin/CliArguments.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.abmo
2+
3+
class CliArguments(private val args: Array<String>) {
4+
5+
fun getHeaders(): Map<String, String> {
6+
val headers = mutableMapOf<String, String>()
7+
var i = 0
8+
9+
while (i < args.size) {
10+
when (args[i]) {
11+
"--header", "-H" -> {
12+
if (i + 1 < args.size) {
13+
val header = args[i + 1]
14+
val parts = header.split(":", limit = 2)
15+
if (parts.size == 2) {
16+
val key = parts[0].trim()
17+
val value = parts[1].trim()
18+
headers[key] = value
19+
} else {
20+
println("Invalid header format. Use 'Header-Name: Header-Value'")
21+
}
22+
i += 1
23+
}
24+
}
25+
}
26+
i += 1
27+
}
28+
29+
return headers
30+
}
31+
32+
fun getOutputFileName(args: Array<String>): String? {
33+
for (i in args.indices) {
34+
if (args[i] == "-o" && i + 1 < args.size) {
35+
return args[i + 1]
36+
}
37+
}
38+
return null
39+
}
40+
41+
}

src/main/kotlin/Main.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ suspend fun main(args: Array<String>) {
1414
val javaScriptRunner = JavaScriptRunner()
1515
val cryptoHelper = CryptoHelper(javaScriptRunner)
1616
val scraperService = ScraperService(cryptoHelper)
17+
val cliArguments = CliArguments(args)
1718

1819

19-
val outputFileName = if (args.isNotEmpty() && args[0] == "-o") {
20-
args.getOrNull(1)
21-
} else {
22-
null
23-
}
20+
val outputFileName = cliArguments.getOutputFileName(args)
21+
val headers = cliArguments.getHeaders()
22+
2423

2524
if (outputFileName != null && !isValidPath(outputFileName)) {
2625
exitProcess(0)
@@ -34,7 +33,7 @@ suspend fun main(args: Array<String>) {
3433
val videoID = scanner.nextLine().getVideoID()
3534

3635
val url = "https://abysscdn.com/?v=$videoID"
37-
val videoSources = scraperService.getVideoMetaData(url)?.sources
36+
val videoSources = scraperService.getVideoMetaData(url, headers)?.sources
3837
?.sortedBy { it?.label?.filter { char -> char.isDigit() }?.toInt() }
3938

4039
if (videoSources == null) {
@@ -56,9 +55,9 @@ suspend fun main(args: Array<String>) {
5655

5756
val config = if (outputFileName == null) {
5857
println("\nOutput file not specified. The video will be saved in the 'Downloads' folder as '${url.getVideoID()}_$resolution.mp4'.")
59-
Config(url, resolution)
58+
Config(url, resolution, header = headers)
6059
} else {
61-
Config(url, resolution, File(outputFileName))
60+
Config(url, resolution, File(outputFileName), header = headers)
6261
}
6362
println("\nvideo with id $videoID and resolution $resolution being processed....")
6463
scraperService.downloadVideo(config)

src/main/kotlin/ScraperService.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ScraperService(
2222
suspend fun downloadVideo(config: Config) {
2323
var totalBytesDownloaded = 0L
2424
val startTime = System.currentTimeMillis()
25-
val video = getVideoMetaData(config.url)
25+
val video = getVideoMetaData(config.url, config.header)
2626
val simpleVideo = video?.toSimpleVideo(config.resolution)
2727
val segmentBodies = generateSegmentsBody(simpleVideo)
2828
val segmentUrl = getSegmentUrl(video)
@@ -46,8 +46,10 @@ class ScraperService(
4646
}
4747

4848

49-
fun getVideoMetaData(url: String): Video? {
50-
val document = Unirest.get(url).asString().body
49+
fun getVideoMetaData(url: String, headers: Map<String, String>?): Video? {
50+
val document = Unirest.get(url)
51+
.headers(headers)
52+
.asString().body
5153
val encryptedVideoData = extractEncryptedVideoMetaData(document)
5254
return cryptoHelper.decodeEncryptedString(encryptedVideoData)
5355
}

src/main/kotlin/model/Config.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ import java.io.File
77
data class Config(
88
val url: String,
99
val resolution: String,
10-
var outputFile: File? = File(systemDownloadFolder(), "${url.getVideoID()}_$resolution.mp4")
10+
var outputFile: File? = File(systemDownloadFolder(), "${url.getVideoID()}_$resolution.mp4"),
11+
val header: Map<String, String>? = null
1112
)

0 commit comments

Comments
 (0)