Skip to content

Commit

Permalink
Merge pull request ZhangMingZhao1#133 from umuoy1/master
Browse files Browse the repository at this point in the history
支持日志推送<微信,邮箱>;优化日志;删除package-lock.json
  • Loading branch information
umuoy1 authored Jan 24, 2022
2 parents d302d1f + e2c0b7f commit 48f97ba
Show file tree
Hide file tree
Showing 30 changed files with 408 additions and 5,586 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ templates/info.json
download/*
.cache
*.png
package-lock.json
5,284 changes: 0 additions & 5,284 deletions package-lock.json

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"homepage": "https://github.com/ZhangMingZhao1/StreamerHelper#readme",
"dependencies": {
"@types/nodemailer": "^6.4.4",
"axios": "0.19.2",
"chalk": "^4.1.0",
"crypto-js": "^4.0.0",
Expand All @@ -29,11 +30,11 @@
"log4js": "^5.3.0",
"md5-node": "^1.0.1",
"node-rsa": "^1.0.8",
"nodemailer": "^6.7.2",
"qr-image": "^3.2.0",
"qrcode-terminal": "^0.12.0",
"qs": "^6.9.4",
"retry-axios": "^2.4.0",
"terminal-image": "^1.2.1",
"uuid": "^8.3.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.config = require('../templates/info.json')
4 changes: 0 additions & 4 deletions src/engine/liveStreamStatus.ts

This file was deleted.

13 changes: 6 additions & 7 deletions src/engine/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { spawn, ChildProcess } from "child_process";
import * as dayjs from "dayjs";
import * as chalk from "chalk";
import { Logger } from "log4js";
const FileHound = require("filehound")

import { emitter } from "@/util/utils";
import { log4js } from "../log";
import { emitter, FileHound } from "@/util/utils";
import { getExtendedLogger } from "@/log";
import { FileStatus } from "@/type/fileStatus";
import { roomPathStatus } from "@/engine/roomPathStatus";
import { uploadStatus } from "@/uploader/uploadStatus";
Expand Down Expand Up @@ -41,7 +40,7 @@ export class Recorder {
this.isPost = false
this._recorderTask.timeV = `${dayjs().format("YYYY-MM-DD")} ${this.getTitlePostfix()}`;

this.logger = log4js.getLogger(`Recorder ${this._recorderTask.recorderName}`)
this.logger = getExtendedLogger(`Recorder ${this._recorderTask.recorderName}`)
}

startRecord(_streamUrl: string | undefined = undefined) {
Expand Down Expand Up @@ -120,10 +119,10 @@ export class Recorder {

roomPathStatus.set(this.savePath, 1)

this.App.stdout.on("data", (data: any) => {
this.App.stdout?.on("data", (data: any) => {
this.logger.info(`FFmpeg error: ${data.toString("utf8")}`);
});
this.App.stderr.on("data", () => {
this.App.stderr?.on("data", () => {

// ffmpeg by default the program logs to stderr ,正常流日志不记录
// logger.error(data.toString("utf8"));
Expand All @@ -147,7 +146,7 @@ export class Recorder {
stopRecord() {
this.ffmpegProcessEndByUser = true
if (!this.ffmpegProcessEnd) {
this.App.stdin.end('q')
this.App.stdin?.end('q')

this.logger.info(`停止录制 ${chalk.red(this._recorderTask.recorderName)}`)
this.logger.info(`记录退出时间 ${chalk.red(this._recorderTask.recorderName)}`)
Expand Down
5 changes: 1 addition & 4 deletions src/engine/roomStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
// 0 不在线 1 在线
let RoomStatus = new Map<string,0|1>()
export {
RoomStatus
}
export const RoomStatus = new Map<string,0|1>()
1 change: 0 additions & 1 deletion src/engine/website/afreecatv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function getChannelInfo(bid: string) {
let r = await axios(config)
resolve(r.data.CHANNEL)
} catch (err) {
console.log(err)
reject(err)
}
})
Expand Down
6 changes: 3 additions & 3 deletions src/engine/website/bilibili.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {logger} from "../../log";
import { getExtendedLogger } from "../../log";

const axios = require("axios");

Expand All @@ -11,7 +11,7 @@ export function main(url: string) {
)
.then(function (response: any) {
const data: any = response.data;
if (data["code"] != 0 || data["data"]["live_time"] < 0) {
if (data["code"] != 0 || data["data"]["live_time"] < 0) {
reject(
"BILIBILI=>No match results:Maybe the roomid is error,or this room is not open!"
);
Expand All @@ -27,7 +27,7 @@ export function main(url: string) {

axios(config).then(function (response: any) {
const html: any = response.data;
logger.trace(`获取哔哩哔哩房间 ${rid} 的推流信息 ${JSON.stringify(html, null, 2)}`)
getExtendedLogger("bilibili").trace(`获取哔哩哔哩房间 ${rid} 的推流信息 ${JSON.stringify(html, null, 2)}`)
const links: any = html["data"]["durl"];
let m3u8_url = links[0]["url"];
resolve(m3u8_url);
Expand Down
1 change: 0 additions & 1 deletion src/engine/website/egame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function main(url: string) {
axios(config)
.then(function (response: any) {
const jsons: any = response.data;
console.log(jsons);
if (jsons["ecode"] == 0) {
resolve(
jsons["data"]["0"]["retBody"]["data"]["video_info"][
Expand Down
3 changes: 0 additions & 3 deletions src/engine/website/huya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function main(url: string) {
//const urlInfo2 = streamInfoList[1];

//可以得到六种链接,m3u8链接最稳定
//console.log("阿里的CDN");
const aliFLV =
urlInfo1["sFlvUrl"] +
"/" +
Expand All @@ -28,8 +27,6 @@ export function main(url: string) {
urlInfo1["sFlvAntiCode"];
//const aliHLS:string = urlInfo1["sHlsUrl"] + "/" + urlInfo1["sStreamName"] + ".m3u8?" + urlInfo1["sHlsAntiCode"];
//const aliP2P = urlInfo1["sP2pUrl"] + "/" + urlInfo1["sStreamName"] + ".slice?" + urlInfo1["newCFlvAntiCode"];

//console.log("腾讯的CDN");
//const txFLV = urlInfo2["sFlvUrl"] + "/" + urlInfo2["sStreamName"] + ".flv?" + urlInfo2["sFlvAntiCode"];
//const txHLS = urlInfo2["sHlsUrl"] + "/" + urlInfo2["sStreamName"] + ".m3u8?" + urlInfo2["sHlsAntiCode"];
//const txP2P = urlInfo2["sP2pUrl"] + "/" + urlInfo2["sStreamName"] + ".slice?" + urlInfo2["newCFlvAntiCode"];
Expand Down
11 changes: 5 additions & 6 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare namespace NodeJS {
export interface Global {
config: import("./type/config").Config,
app: import("index").App
}
}
export interface global {}
declare global {
var config: import("./type/config").Config
var app: import("index").App
}
11 changes: 6 additions & 5 deletions src/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import axios, { AxiosInstance } from 'axios'
import axios, { AxiosInstance ,AxiosResponse} from 'axios'
import * as rax from 'retry-axios';
import { log4js } from "@/log";
import { AxiosResponse } from 'axios';

import { getExtendedLogger } from "@/log";

const headers = {
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': '',
'Accept-Encoding': 'gzip,deflate',
}
const logger = log4js.getLogger(`HTTP`)
const logger = getExtendedLogger(`HTTP`)

const $axios: AxiosInstance & {
[key: string]: any
Expand All @@ -22,7 +23,7 @@ $axios.defaults.raxConfig = {
noResponseRetries: 5,
onRetryAttempt: err => {
const cfg = rax.getConfig(err);
logger.error(`Retry attempt #${cfg && cfg.currentRetryAttempt}`);
logger.warn(`Retry attempt #${cfg && cfg.currentRetryAttempt}`);
}
}

Expand Down
67 changes: 52 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { User } from "@/uploader/user";
import { log4js } from "@/log";
import { Logger } from "log4js";
import "@/config"

import * as fs from "fs";
import { join, basename } from "path";
import { emitter } from "@/util/utils";
import { Scheduler } from "./type/scheduler";
import { Recorder } from "./engine/message";
import { Config } from "./type/config";

import { Logger } from "log4js";

import { User } from "@/uploader/user";
import { getExtendedLogger } from "@/log";
import { changeFileStatus, emitter } from "@/util/utils";
import { Scheduler } from "@/type/scheduler";
import { Recorder } from "@/engine/message";
import { FileStatus } from "@/type/fileStatus";
import { FileHound } from "@/util/utils"


type Schedulers = {
[key: string]: {
Expand All @@ -15,12 +21,12 @@ type Schedulers = {
}
}


export class App {
private _logger: Logger;
private _user!: User; // will init in initUser()
private _schedulers: Schedulers;
private _recorderPool: Map<string, Recorder>;
private _config: Config;
static _i: any;

get logger(): Logger {
Expand All @@ -39,14 +45,8 @@ export class App {
return this._recorderPool
}

get config(): Config {
return this._config
}

constructor() {
global.config = this._config = require('../templates/info.json')

this._logger = log4js.getLogger(`APP`)
this._logger = getExtendedLogger(`APP`)
this._schedulers = {}
this._recorderPool = new Map<string, Recorder>()

Expand All @@ -71,6 +71,7 @@ export class App {
await this.initUser()
await this.initExitSignal()
await this.initStreamDisconnect()
await this.initSyncFileStatus()
await this.initSchedule()
} catch (e) {
return reject(e)
Expand Down Expand Up @@ -171,6 +172,42 @@ export class App {

})
}

initSyncFileStatus = async () => {
const files: string[] = await FileHound.create()
.paths(join(process.cwd(), "/download"))
.match('fileStatus.json')
.ext('json')
.find();



for (const file of files) {
const text = fs.readFileSync(file)
const obj: FileStatus = JSON.parse(text.toString())

this.logger.debug(`Sync fileStatus: ${file} ${JSON.stringify(obj, null, 2)}`)

const streamer = global.config.streamerInfo.find(elem => elem.name === obj.recorderName)
if (!streamer)
continue

changeFileStatus({
uploadLocalFile: streamer.uploadLocalFile,
deleteLocalFile: streamer.deleteLocalFile,
templateTitle: streamer.templateTitle,
delayTime: streamer.delayTime,
desc: streamer.desc,
source: streamer.source,
dynamic: streamer.dynamic,
copyright: streamer.copyright,
recorderLink: streamer.roomUrl,
tid: streamer.tid,
tags: streamer.tags
}, file)

}
}
}

global.app = App.getInstance()
Expand Down
41 changes: 41 additions & 0 deletions src/log/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as log4js from "log4js";

const logLevel = require('../../templates/info.json').StreamerHelper.debug ? "debug" : "info"

log4js.configure({
appenders: {
cheese: {
type: "file",
filename: process.cwd() + "/logs/artanis.log",
maxLogSize: 20971520,
backups: 10,
encoding: "utf-8",
},
memory: {
type: "file",
filename: process.cwd() + "/logs/memory.log",
maxLogSize: 20971520,
backups: 10,
encoding: "utf-8",
},
console: {
type: "console"
}
},
categories: {
cheese: {
appenders: ["cheese", "console"], level: logLevel
},
memory: {
appenders: ["memory"], level: "info"
},
check: {
appenders: ["console"], level: "debug"
},
default: {
appenders: ["cheese", "console"], level: logLevel
},
},
});

export { log4js }
Loading

0 comments on commit 48f97ba

Please sign in to comment.