Add colors

This commit is contained in:
the1812 2019-04-24 13:45:06 +08:00
parent 372d637155
commit 9e6b4b983f
5 changed files with 82 additions and 55 deletions

View File

@ -8,6 +8,7 @@
},
"dependencies": {
"clipboardy": "^2.0.0",
"colors": "^1.3.3",
"command-line-args": "^5.1.1",
"progress": "^2.0.3",
"request": "^2.88.0"
@ -36,4 +37,4 @@
"url": "https://github.com/the1812/Bilibili-Evolved/issues"
},
"homepage": "https://github.com/the1812/Bilibili-Evolved/"
}
}

View File

@ -6,6 +6,7 @@ const clipboardy = require("clipboardy");
const request = require("request");
const fs = require("fs");
const ProgressBar = require("progress");
require("colors");
const optionDefinitions = [
{ name: 'info', alias: 'i', defaultOption: true, type: String, defaultValue: undefined },
{ name: 'parts', alias: 'p', type: Number, defaultValue: 30 },
@ -13,13 +14,14 @@ const optionDefinitions = [
];
const commandLineOptions = commandLineArgs(optionDefinitions);
let options = commandLineOptions;
options.parts = Math.round(options.parts);
// if (fs.existsSync("settings.json"))
// {
// const jsonOptions = JSON.parse(fs.readFileSync("settings.json").toString("utf-8")) as Settings;
// options = Object.assign(jsonOptions, options);
// }
if (options.parts < 1) {
console.error("分段数不能小于1");
console.error("分段数不能小于1".red);
process.exit();
}
class Downloader {
@ -44,7 +46,7 @@ class Downloader {
const files = fs.readdirSync(".");
const parts = files.filter(it => it.includes(this.inputData.title));
parts.forEach(file => fs.unlinkSync(file));
console.log("已取消下载");
console.log("已取消下载".blue);
}
async downloadFragment(fragment, index = -1) {
const partialLength = Math.round(fragment.size / options.parts);
@ -80,7 +82,7 @@ class Downloader {
this.progressMap.delete(req);
this.progressMap.set(makeRequest(), 0);
this.updateProgress();
console.error(`\n片段下载失败: ${error} 重试中...`);
console.error(`\n片段下载失败: ${error} 重试中...`.yellow);
});
req.pipe(fs.createWriteStream(`${title}.part${part}`));
return req;
@ -96,10 +98,10 @@ class Downloader {
async mergeFragment(title, index = -1) {
const dest = title + ".flv";
if (index !== -1) {
console.log("正在合并片段" + index.toString() + "...");
console.log("正在合并片段" + index.toString() + "...".green);
}
else {
console.log("正在合并文件...");
console.log("正在合并文件...".green);
}
if (options.parts === 1) {
fs.renameSync(title + ".part0", dest);
@ -124,7 +126,7 @@ class Downloader {
return dest;
}
async download() {
console.log(`正在下载: ${this.inputData.title}`);
console.log(`正在下载: ${this.inputData.title}`.green);
this.progressBar.render();
let result;
if (this.inputData.fragments.length === 1) {
@ -134,7 +136,7 @@ class Downloader {
const titles = await Promise.all(this.inputData.fragments.map((f, i) => this.downloadFragment(f, i)));
result = await Promise.all(titles.map((t, i) => this.mergeFragment(t, i)));
}
console.log(`下载完成: `);
console.log(`下载完成: `.green);
if (typeof result === "string") {
console.log(result);
}
@ -145,16 +147,25 @@ class Downloader {
}
Downloader.workingDownloader = null;
(async () => {
// console.log(`分段值: ${options.parts}`);
let jsonText = '';
if (fs.existsSync(options.info)) {
jsonText = fs.readFileSync(options.info).toString("utf-8");
}
else {
jsonText = await clipboardy.read();
}
try {
let jsonText = '';
if (fs.existsSync(options.info)) {
jsonText = fs.readFileSync(options.info).toString("utf-8");
}
else {
jsonText = await clipboardy.read();
}
const inputData = JSON.parse(jsonText);
if (!(function (data) {
if (Array.isArray(data) && data.length > 0) {
return data[0].fragments !== undefined;
}
else {
return data.fragments !== undefined;
}
})(inputData)) {
throw new Error();
}
try {
process.chdir(options.output);
process.on("SIGINT", () => {
@ -174,10 +185,10 @@ Downloader.workingDownloader = null;
}
catch (error) {
Downloader.workingDownloader && Downloader.workingDownloader.cancelDownload();
console.error(`\n错误: ${error}`);
console.error(`\n错误: ${error}`.red);
}
}
catch (error) {
console.log(`[无数据] 未在剪贴板检测到有效数据/没有指定输入文件/输入文件的数据无效.`);
console.log(`[无数据] 未在剪贴板检测到有效数据/没有指定输入文件/输入文件的数据无效.`.red);
}
})();

View File

@ -4,6 +4,7 @@ import clipboardy = require("clipboardy");
import request = require("request");
import fs = require("fs");
import ProgressBar = require("progress");
import "colors";
interface Fragment
{
@ -32,6 +33,7 @@ const optionDefinitions = [
];
const commandLineOptions = commandLineArgs(optionDefinitions) as Settings;
let options = commandLineOptions;
options.parts = Math.round(options.parts);
// if (fs.existsSync("settings.json"))
// {
// const jsonOptions = JSON.parse(fs.readFileSync("settings.json").toString("utf-8")) as Settings;
@ -40,7 +42,7 @@ let options = commandLineOptions;
if (options.parts < 1)
{
console.error("分段数不能小于1");
console.error("分段数不能小于1".red);
process.exit();
}
@ -73,7 +75,7 @@ class Downloader
const files = fs.readdirSync(".");
const parts = files.filter(it => it.includes(this.inputData.title));
parts.forEach(file => fs.unlinkSync(file));
console.log("已取消下载");
console.log("已取消下载".blue);
}
private async downloadFragment(fragment: Fragment, index: number = -1)
{
@ -118,7 +120,7 @@ class Downloader
this.progressMap.delete(req);
this.progressMap.set(makeRequest(), 0);
this.updateProgress();
console.error(`\n片段下载失败: ${error} 重试中...`);
console.error(`\n片段下载失败: ${error} 重试中...`.yellow);
});
req.pipe(fs.createWriteStream(`${title}.part${part}`));
return req;
@ -136,11 +138,11 @@ class Downloader
const dest = title + ".flv";
if (index !== -1)
{
console.log("正在合并片段" + index.toString() + "...");
console.log("正在合并片段" + index.toString() + "...".green);
}
else
{
console.log("正在合并文件...");
console.log("正在合并文件...".green);
}
if (options.parts === 1)
{
@ -171,7 +173,7 @@ class Downloader
}
async download()
{
console.log(`正在下载: ${this.inputData.title}`);
console.log(`正在下载: ${this.inputData.title}`.green);
this.progressBar.render();
let result: string | string[];
if (this.inputData.fragments.length === 1)
@ -183,7 +185,7 @@ class Downloader
const titles = await Promise.all(this.inputData.fragments.map((f, i) => this.downloadFragment(f, i)));
result = await Promise.all(titles.map((t, i) => this.mergeFragment(t, i)));
}
console.log(`下载完成: `);
console.log(`下载完成: `.green);
if (typeof result === "string")
{
console.log(result);
@ -196,19 +198,32 @@ class Downloader
}
(async () =>
{
// console.log(`分段值: ${options.parts}`);
let jsonText = '';
if (fs.existsSync(options.info))
{
jsonText = fs.readFileSync(options.info).toString("utf-8");
}
else
{
jsonText = await clipboardy.read();
}
try
{
let jsonText = '';
if (fs.existsSync(options.info))
{
jsonText = fs.readFileSync(options.info).toString("utf-8");
}
else
{
jsonText = await clipboardy.read();
}
const inputData = JSON.parse(jsonText) as InputData | InputData[];
if (!(function (data: any): data is InputData | InputData[]
{
if (Array.isArray(data) && data.length > 0)
{
return data[0].fragments !== undefined;
}
else
{
return data.fragments !== undefined;
}
})(inputData))
{
throw new Error();
}
try
{
process.chdir(options.output);
@ -234,11 +249,11 @@ class Downloader
catch (error)
{
Downloader.workingDownloader && Downloader.workingDownloader.cancelDownload();
console.error(`\n错误: ${error}`);
console.error(`\n错误: ${error}`.red);
}
}
catch (error)
{
console.log(`[无数据] 未在剪贴板检测到有效数据/没有指定输入文件/输入文件的数据无效.`);
console.log(`[无数据] 未在剪贴板检测到有效数据/没有指定输入文件/输入文件的数据无效.`.red);
}
})();

View File

@ -10,7 +10,7 @@
TintColor="#0fff"
FallbackColor="#0fff"
Foreground="Black"
NoiseOpacity="0.01"
NoiseOpacity="0.05"
Title="MainWindow"
ShowTitleBar="False"
Height="450"

View File

@ -21,23 +21,23 @@ namespace VideoLinkDownloader
public MainWindow()
{
InitializeComponent();
button.Click += (s,e) =>
{
AcrylicBlur.Apply(this, new Color
{
A = 0,
R = 255,
G = 255,
B = 255,
}, AccentState.Diabled);
AcrylicBlur.Apply(this, new Color
{
A = 0,
R = 255,
G = 255,
B = 255,
}, AccentState.AcrylicBlurBehind);
};
//button.Click += (s,e) =>
//{
// AcrylicBlur.Apply(this, new Color
// {
// A = 0,
// R = 255,
// G = 255,
// B = 255,
// }, AccentState.Diabled);
// AcrylicBlur.Apply(this, new Color
// {
// A = 0,
// R = 255,
// G = 255,
// B = 255,
// }, AccentState.AcrylicBlurBehind);
//};
}
}
}