Skip to content

Commit

Permalink
feat: 修改FTP
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhelinCheng committed Jun 1, 2021
1 parent ce489ae commit a1bd8cf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 64 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@types/promise-ftp": "^1.3.4",
"async": "^3.2.0",
"axios": "^0.21.1",
"basic-ftp": "^4.6.6",
"cheerio": "^1.0.0-rc.5",
"cron": "^1.8.2",
"dayjs": "^1.10.4",
Expand Down
75 changes: 19 additions & 56 deletions src/core/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
* @Author : Zhelin Cheng
* @Date : 2021-02-19 15:16:57
* @LastEditors : Zhelin Cheng
* @LastEditTime : 2021-05-31 22:30:14
* @LastEditTime : 2021-06-01 15:57:29
* @FilePath : /bilibili-downloader/src/core/downloader.ts
* @Description : 未添加文件描述
*/

import axios, { CancelTokenSource } from 'axios';
import axios from 'axios';
import fs from 'fs';
import fse from 'fs-extra';
import { db, logger, env } from '../utils';
import { db, logger, env, timeout } from '../utils';
import { mapLimit } from 'async';
import PromiseFtp from 'promise-ftp';
import * as FTP from 'basic-ftp';
import { getVideoDownloadUrl, getVideoPage, VideoUrlItems } from './url';
import { postData } from './ftp';
import { outputPath, isFtp } from '../const';
import dayjs from 'dayjs';

const ftp = new PromiseFtp();
const client = new FTP.Client();
client.ftp.verbose = true;

const baseFtpPath = env.BILIBILI_FTP_PATH || '/Multimedia/Bilibili';

type BaseItemType = VideoUrlItems & { cid: string };

// import fs from 'fs';
let cancelTokenSource: null | CancelTokenSource = null;
// let cancelTokenSource: null | CancelTokenSource = null;

export const downloadVideo = async (
url: string,
Expand All @@ -37,14 +38,12 @@ export const downloadVideo = async (
if (!url) {
return { headerSize: 0 };
}
cancelTokenSource = axios.CancelToken.source();
const { data, headers } = await axios({
method: 'get',
url,
headers: {
Cookie: '',
},
cancelToken: cancelTokenSource.token,
responseType: 'stream',
});
return {
Expand All @@ -61,21 +60,11 @@ export const downloadVideo = async (
async function ftpLink() {
try {
if (isFtp) {
logger.info(`FTP状态:${ftp.getConnectionStatus()}`);
const serverMessage = await ftp.connect({
await client.access({
host: env.BILIBILI_FTP_HOST,
user: env.BILIBILI_FTP_USER,
password: env.BILIBILI_FTP_PASS,
});

const errFile = db.get('errorFile').value();
if (errFile) {
logger.info('删除错误文件');
await ftp.delete(errFile);
await db.set('errorFile', '').write();
}
logger.info(serverMessage);
// ftp.mkdir(baseFtpPath, true)
} else {
logger.info(`存储到本地:${outputPath}`);
}
Expand All @@ -86,12 +75,6 @@ async function ftpLink() {
return false;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
// let errTimer: any = 0

// 下载列表
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let errTimer: any = 0;
async function downloadList(
queue: Array<BaseItemType>,
): Promise<Array<string>> {
Expand All @@ -112,11 +95,11 @@ async function downloadList(
1,
async ({ bvid, name, cid }) => {
try {
clearTimeout(errTimer);
if (notes.includes(cid)) {
return bvid;
}

await timeout(2000);
logger.info(`下载 ⇒ 昵称:${name} | BVID:${bvid} | CID:${cid}`);

const { url, size, ext } = await getVideoDownloadUrl(bvid, cid);
Expand All @@ -125,25 +108,6 @@ async function downloadList(
const filePos = `${filePath}/${fileName}`;
const localPath = `${outputPath}/${name}/${fileName}`;

// 超时删除
errTimer = setTimeout(async () => {
if (
cancelTokenSource &&
typeof cancelTokenSource.cancel === 'function'
) {
logger.error('清除下载');
cancelTokenSource.cancel('清除超时文件');
if (isFtp) {
await db.set('errorFile', filePos).write();
ftp.destroy();
} else {
fse.removeSync(localPath);
}
}
cancelTokenSource = null;
reject('未下载完成');
}, 480000);

logger.info(`开始下载:${url}`);
const { data, headerSize } = await downloadVideo(url);
if (!data || size <= 0) {
Expand All @@ -152,7 +116,7 @@ async function downloadList(

logger.info(`保存中...`);
const uploadFtp = await postData(
ftp,
client,
data,
filePath,
fileName,
Expand All @@ -161,7 +125,7 @@ async function downloadList(

logger.info(`判断是否下载完成...`);
const fileSize = isFtp
? await ftp.size(filePos)
? await client.size(filePos)
: fs.statSync(localPath).size;

// 校验下载完整性及上传状态
Expand All @@ -176,17 +140,17 @@ async function downloadList(
// 删除不正确的文件
if (!isOver && uploadFtp) {
if (isFtp) {
await ftp.delete(filePos);
await client.remove(filePos);
} else {
fse.removeSync(localPath);
}
}

/* if (isOver) {
if (isOver) {
notes.push(cid);
notes.push(bvid);
await db.set('notes', notes).write();
} */
}

return isOver ? bvid : '';
} catch (e) {
Expand All @@ -195,10 +159,7 @@ async function downloadList(
}
},
async (err, results) => {
clearTimeout(errTimer);
if (isFtp) {
await ftp.end();
}
// clearTimeout(errTimer);
if (err) {
return reject(err);
}
Expand Down Expand Up @@ -286,8 +247,6 @@ export const downloader = async (): Promise<void> => {
}
}

// console.log(downSuccess)

logger.info('删除已下载完成的bvid');
downSuccess.forEach(async (bvid: string) => {
await db.get('queue').remove({ bvid }).write();
Expand All @@ -296,5 +255,9 @@ export const downloader = async (): Promise<void> => {
logger.info('+++ 本次下载完成 +++');
} catch (e) {
logger.error(e);
} finally {
if (isFtp) {
client.close();
}
}
};
12 changes: 6 additions & 6 deletions src/core/ftp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* @Author : Zhelin Cheng
* @Date : 2021-02-19 17:09:10
* @LastEditors : Zhelin Cheng
* @LastEditTime : 2021-04-24 00:19:45
* @FilePath : \bilibili-downloader\src\core\ftp.ts
* @LastEditTime : 2021-06-01 15:39:49
* @FilePath : /bilibili-downloader/src/core/ftp.ts
* @Description : 未添加文件描述
*/

import fs from 'fs';
import fse from 'fs-extra';
import { isFtp } from '../const';
import PromiseFtp from 'promise-ftp';
import * as FTP from 'basic-ftp';

const localSave = (
input: NodeJS.ReadStream,
Expand All @@ -30,16 +30,16 @@ const localSave = (
};

export const postData = async (
ftp: PromiseFtp,
client: FTP.Client,
input: NodeJS.ReadStream,
destPath: string,
fileName: string,
localPath: string,
): Promise<boolean> => {
try {
if (isFtp) {
await ftp.mkdir(destPath, true);
await ftp.put(input, `${destPath}/${fileName}`);
await client.ensureDir(destPath);
await client.uploadFrom(input, `${destPath}/${fileName}`);
return true;
} else {
fse.ensureDirSync(localPath.replace(/(\/|\\)\d+-\d+\.(mp4|flv)$/, ''));
Expand Down
4 changes: 2 additions & 2 deletions src/core/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author : Zhelin Cheng
* @Date : 2021-04-10 17:35:02
* @LastEditors : Zhelin Cheng
* @LastEditTime : 2021-05-26 21:13:32
* @LastEditTime : 2021-06-01 16:05:10
* @FilePath : /bilibili-downloader/src/core/url.ts
* @Description : 未添加文件描述
*/
Expand Down Expand Up @@ -441,7 +441,7 @@ export const getVideosUrl = async (): Promise<boolean> => {
const notes: string[] = db.get('notes').value();
const timeout =
Math.floor(Date.now() / 1000) -
(process.env.NODE_ENV === 'development' ? 600 : 43200);
(process.env.NODE_ENV === 'development' ? 1200 : 43200);
let isDownload = false;

const includeRe = new RegExp(env.BILIBILI_INCLUDE_KW || '', 'img');
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ balanced-match@^1.0.0:
resolved "https://registry.npm.taobao.org/balanced-match/download/balanced-match-1.0.2.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fbalanced-match%2Fdownload%2Fbalanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha1-6D46fj8wCzTLnYf2FfoMvzV2kO4=

basic-ftp@^4.6.6:
version "4.6.6"
resolved "https://registry.nlark.com/basic-ftp/download/basic-ftp-4.6.6.tgz#c37abae3b3bffa131027adb6a5b60938d9109501"
integrity sha1-w3q647O/+hMQJ622pbYJONkQlQE=

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.nlark.com/binary-extensions/download/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
Expand Down

0 comments on commit a1bd8cf

Please sign in to comment.