Skip to content

Commit

Permalink
Merge pull request #2 from yajuhua/tmp
Browse files Browse the repository at this point in the history
合并tmp2
  • Loading branch information
yajuhua authored Sep 13, 2024
2 parents 969efa0 + 3df56d3 commit d126282
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 44 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ on:
required: true
default: '23.0.2'
type: string
artifacts-file:
description: 'artifacts-file'
required: true
default: '*'
type: string

jobs:
# 新标签
Expand Down Expand Up @@ -403,5 +398,7 @@ jobs:
prerelease: ${{ inputs.prerelease }}
title: "${{ env.RELEASE_TAG }}"
files: |
${{ inputs.artifacts-file }}
artifacts/*/*.zip
artifacts/jar/iv-dl.jar
native-image-conf.zip
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.yajuhua</groupId>
<artifactId>iv-dl</artifactId>
<version>0.0.1</version>
<version>none</version>
<name>${project.artifactId}</name>
<url>https://github.com/yajuhua/invidious-dlj</url>
<url>https://github.com/yajuhua/iv-dl</url>
<description>invidious downloader</description>

<properties>
Expand Down Expand Up @@ -76,16 +76,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.13.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
79 changes: 71 additions & 8 deletions src/main/java/io/github/yajuhua/invidious/dlj/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
import io.github.yajuhua.invidious.dlj.command.Command;
import io.github.yajuhua.invidious.dlj.compiler.RunAllFeatForConfig;
import io.github.yajuhua.invidious.dlj.pojo.Video;
import io.github.yajuhua.invidious.dlj.utils.FileUtils;
import io.github.yajuhua.invidious.wrapper.Invidious;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Slf4j
public class Application {
Expand Down Expand Up @@ -46,6 +49,18 @@ public static Properties getProperties(){
*/
public static void main(String[] args) throws Exception {

/**
* 打印帮助
*/
if (Command.toList(args).contains("--help") || Command.toList(args).contains("-h")){
Command.printHelp();
return;
}

//yt-dlp路径
String ytDlpPath = Command.YtDlpPath(args);
System.setProperty("yt-dlp",ytDlpPath);

//执行测试代码
if (Command.toList(args).contains("--test")){
RunAllFeatForConfig.run();
Expand All @@ -57,22 +72,65 @@ public static void main(String[] args) throws Exception {
return;
}

//打印各种调试信息
if (Command.hasVerbose(args)){
//打印各种调试信息
printVerbose();
}

//设置为error级别日志
if (Command.isIgnoreWarnLog(args)){
//设置为error级别日志
ignoreWarnLog = true;
setLogLevel(Level.ERROR);
}

log.debug(Arrays.toString(args));

//校验和过滤参数
List<String> argList = Command.filterYtDlpArguments(args);
//批量下载
if (Command.hasBatchFile(args)){
//获取批量下载文件中的链接,如果有的话
List<String> urlList;
File batchFile = Command.getBatchFile(args);
if (batchFile.exists()){
urlList = FileUtils.readLines(batchFile,"UTF-8");
}else {
throw new Exception("找不到文件: " + batchFile);
}

//过滤不合法的链接
urlList = urlList.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
try {
new URL(s);
return true;
} catch (Exception e) {
return false;
}
}
}).collect(Collectors.toList());

for (String url : urlList) {
//校验和过滤参数
List<String> argList = Command.filterYtDlpArguments(args);
argList.add(url);
download(Command.toArray(argList));
}
}else {
//下载单个
List<String> argList = Command.filterYtDlpArguments(args);
String url = Command.getUrl(args);
argList.add(url);
download(Command.toArray(argList));
}
}

/**
* 下载
* @param args
* @throws Exception
*/
public static void download(String[] args) throws Exception{
List<String> argList = Command.toList(args);
//获取并设置代理
Proxy proxy = Command.getProxy(args);
if (proxy != null){
Expand Down Expand Up @@ -118,8 +176,12 @@ public static void main(String[] args) throws Exception {
System.out.println("[info] generate json file: " + tempJsonFile.getAbsolutePath());
FileUtils.write(tempJsonFile,gson.toJson(videoList),"UTF-8");

//移除下载链接
argList.remove(argList.size() - 1);

//执行cmd命令
argList.add(0,"yt-dlp");
String execFile = System.getProperty("yt-dlp");
argList.add(0,execFile);
argList.add("--load-info-json");
argList.add(tempJsonFile.getAbsolutePath());

Expand All @@ -144,15 +206,15 @@ public static void main(String[] args) throws Exception {
}
}
int waitFor = process.waitFor();
if (waitFor != 0){
if (waitFor != 0) {
bre = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
while ((line = bre.readLine()) != null){
System.out.println(line);
}
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}finally {
//清理临时文件
if (tempJsonFile.exists()){
Expand All @@ -167,6 +229,7 @@ public static void main(String[] args) throws Exception {
bre.close();
}
}

}

/**
Expand Down
119 changes: 99 additions & 20 deletions src/main/java/io/github/yajuhua/invidious/dlj/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.yajuhua.invidious.dlj.command;

import java.io.File;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
Expand All @@ -21,21 +22,20 @@ public class Command {
* @return没有就返回null
*/
public static Proxy getProxy(String[] args) throws Exception {
//TODO 获取系统代理
Proxy proxy = null;
if (hasProxy(args)){
List<String> argList = toList(args);
String proxyStr = argList.get(argList.indexOf("--proxy") + 1);
URL url = new URL(proxyStr);
int port = url.getPort();
String host = url.getHost();
InetSocketAddress address = new InetSocketAddress(host, port);
if (proxyStr.startsWith("http")){
proxy = new Proxy(Proxy.Type.HTTP,address);
} else if (proxyStr.startsWith("socks5") || proxyStr.startsWith("socks")) {
proxy = new Proxy(Proxy.Type.HTTP,address);
}
}
if (hasProxy(args)){
List<String> argList = toList(args);
String proxyStr = argList.get(argList.indexOf("--proxy") + 1);
URL url = new URL(proxyStr);
int port = url.getPort();
String host = url.getHost();
InetSocketAddress address = new InetSocketAddress(host, port);
if (proxyStr.startsWith("http")){
proxy = new Proxy(Proxy.Type.HTTP,address);
} else if (proxyStr.startsWith("socks5") || proxyStr.startsWith("socks")) {
proxy = new Proxy(Proxy.Type.HTTP,address);
}
}
return proxy;
}

Expand Down Expand Up @@ -175,8 +175,25 @@ public static List<String> filterYtDlpArguments(String[] args){
filteredArgs = argList;
}
filteredArgs.remove("--version");//删除版本信息选项
filteredArgs.remove(filteredArgs.size() - 1);//删除下载链接

//自定义yt-dlp路径
if (hasYtDlpPath(args)){
filteredArgs.remove(filteredArgs.indexOf("--yt-dlp-path") + 1);
filteredArgs.remove("--yt-dlp-path");
}

//批量下载
if (hasBatchFile(args)){
if (filteredArgs.contains("--batch-file")){
filteredArgs.remove(filteredArgs.indexOf("--batch-file") + 1);
} else if (filteredArgs.contains("-a")) {
filteredArgs.remove(filteredArgs.indexOf("-a") + 1);
}
filteredArgs.remove("-a");
filteredArgs.remove("--batch-file");
}else {
filteredArgs.remove(filteredArgs.size() - 1);//删除下载链接
}
return filteredArgs;
}

Expand All @@ -194,11 +211,13 @@ public static void validArgs(String[] args){
String regex = "https?://(?:www\\.)?[a-zA-Z0-9-]+(?:\\.[a-zA-Z]{2,})+(?:/[^\\s]*)?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);
if (!matcher.find()){
throw new RuntimeException("请输入合法链接: " + url);
}
if (!supportUrlKey.stream().anyMatch(url::contains)){
throw new RuntimeException("不支持该链接: " + url);
if (!hasBatchFile(args)){
if (!matcher.find()){
throw new RuntimeException("请输入合法链接: " + url);
}
if (!supportUrlKey.stream().anyMatch(url::contains)){
throw new RuntimeException("不支持该链接: " + url);
}
}
}else {
throw new RuntimeException("请输入合法参数");
Expand Down Expand Up @@ -235,4 +254,64 @@ public static String[] toArray(List<String> argList){
public static boolean hasVerbose(String[] args){
return toList(args).contains("-v") || toList(args).contains("--verbose");
}

/**
* 判断是否有批量下载文件
* @param args
* @return
*/
public static boolean hasBatchFile(String[] args){
return toList(args).contains("-a") || toList(args).contains("--batch-file");
}

/**
* 获取批量下载文件
* @param args
* @return
*/
public static File getBatchFile(String[] args) throws Exception {
List<String> argList = toList(args);
String filePath;
if (hasBatchFile(args)){
if (argList.contains("-a")){
filePath = argList.get(argList.indexOf("-a") + 1);
return new File(filePath);
}else if (argList.contains("--batch-file")){
filePath = argList.get(argList.indexOf("--batch-file") + 1);
return new File(filePath);
}
}
throw new Exception("--batch-file 选项异常");
}

/**
* 打印help
*/
public static void printHelp(){
System.out.println("--help,-h 帮助");
System.out.println("--version 版本信息");
System.out.println("--yt-dlp-path 设置yt-dlp二进制文件路径,默认'yt-dlp'");
}

/**
* 获取自定义yt-dlp路径的,如果有的话
* @return
*/
public static String YtDlpPath(String[] args){
List<String> argList = toList(args);
if (argList.contains("--yt-dlp-path")){
String path = argList.get(argList.indexOf("--yt-dlp-path") + 1);
return path;
}
return "yt-dlp";//默认
}

/**
* 是否有自定义yt-dlp路径
* @param args
* @return
*/
public static boolean hasYtDlpPath(String[] args){
return toList(args).contains("--yt-dlp-path");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import io.github.yajuhua.invidious.dlj.Info;
import io.github.yajuhua.invidious.dlj.command.Command;
import io.github.yajuhua.invidious.dlj.pojo.Video;
import io.github.yajuhua.invidious.dlj.utils.FileUtils;
import io.github.yajuhua.invidious.wrapper.Invidious;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -202,6 +202,22 @@ public static void ignoreWarnLog() throws Exception {
base(args);
}

/**
* 批量下载
*/
public static void batchFile() throws Exception {
List<String> urlList = new ArrayList<>();
urlList.add("https://www.youtube.com/watch?v=eDqfg_LexCQ");
urlList.add("https://www.youtube.com/watch?v=fE6XAeZfAsk");
FileUtils.writeLines(new File("a.txt"),urlList);
String[] args = new String[]{"--batch-file","a.txt"};
List<String> argList = Command.filterYtDlpArguments(args);
for (String url : urlList) {
argList.add(url);
Application.download(Command.toArray(argList));
}
}

/**
* 使用所有功能,用于生成编译用的配置文件
* @throws Exception
Expand All @@ -214,6 +230,7 @@ public static void run()throws Exception{
playlist();
version();
ignoreWarnLog();
batchFile();
log.info("end");
System.exit(0);
}
Expand Down
Loading

0 comments on commit d126282

Please sign in to comment.