This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
player.js
153 lines (132 loc) · 3.97 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var sleep = (t) => new Promise((y) => setTimeout(y, t));
var Logger = require("./logger.js");
class ProgressBar {
constructor(bar_length) {
// 两个基本参数(属性)
this.length = bar_length || 32; // 进度条的长度(单位:字符),默认设为 35
// 刷新进度条图案、文字的方法
this.render = function (opts) {
var percent = (opts.completed / opts.total).toFixed(4); // 计算进度(子任务的 完成数 除以 总数)
var cell_num = Math.floor(percent * this.length); // 计算需要多少个 █ 符号来拼凑图案
// 拼接黑色条
var cell = '';
for (var i = 0; i < cell_num; i++) {
cell += '█';
}
// 拼接灰色条
var empty = '';
for (var i = 0; i < this.length - cell_num; i++) {
empty += '░';
}
// 拼接最终文本
var cmdText = (100 * percent).toFixed(2) + '% ' + cell + empty + ' ' + opts.completed + '/' + opts.total;
return cmdText;
};
}
}
var pb = new ProgressBar();
class Player {
constructor(classid, user, video, speed) {
this.playing = video;
this.progress = 0;
this.end = false;
this.user = user;
this.logger = new Logger(classid, user);
this.statusinfo = "";
if (!speed) speed = 1.0; //倍速
this.speed = speed;
this.reportTimeInterval = 60 * 1000;
this.tick = this.reportTimeInterval; //确保视频刚开始report一次
this.simspeed = 100; //更新速度;
this.eventLoop();
}
async wait(timeout) {
let tick = 0;
if (!timeout) timeout = 9999999;
while (true) {
if (this.end) return;
if (tick++ > timeout) return;
await sleep(500);
}
}
async init() {}
getStatusInfo() {
return this.statusinfo + "\n\n";
}
async doLog(status, isDrag) {
return await this.logger.sendLog(
this.playing.ruri,
this.playing.otherInfo,
this.playing.chapid,
this.playing.jobid,
this.playing.objectId,
parseInt(this.progress / 1000),
status,
isDrag
);
}
async doTick() {
const rawJson = await this.user.net.rawGet(
`ananas/status/${this.playing.objectId}?k=9790&flag=normal&_dc=` +
new Date().getTime()
);
// console.log("status",rawJson);
let status = JSON.parse(rawJson);
let duration = status.duration;
// if(duration<60*6) //小于6分钟的完整看完
// this.speed=1;
//当视频失效时,duration为空
if (!duration) {
console.log(
`\n${this.playing.property.name} <- 该视频无法解析, 已跳过\n`
);
this.end = true;
return;
}
let loginfo = `下次报告: ${((this.reportTimeInterval - this.tick) / 1000).toFixed(1)}s`;
let reportinfo = "";
if (this.progress / 1000 >= duration) {
this.progress = duration * 1000;
this.end = true;
reportinfo = await this.doLog(status, 4);
loginfo = reportinfo;
}
if (this.tick >= this.reportTimeInterval) {
//时间达到需要log的时刻,sendLog一次
this.tick = 0;
reportinfo = await this.doLog(status, 0);
loginfo = reportinfo;
}
try {
if (JSON.parse(reportinfo).isPassed == true) this.end = true;
} catch (e) {}
//if(this.tick++%10==0)
this.statusinfo =
"正在 " +
this.speed.toFixed(1) +
" 倍速播放: " +
this.playing.property.name +
// " " +
// ((this.progress / 1000 / duration) * 100).toFixed(2) +
// "% " +
// ((this.progress / 1000).toFixed(1) + "/" + duration) +
" " +
loginfo+
"\n"+
pb.render({ completed: parseInt(this.progress / 1000), total: duration });
this.progress += parseInt(this.speed * this.simspeed);
}
async eventLoop() {
//自动消费播放队列
while (!this.end) {
try {
await this.doTick();
this.tick += this.simspeed;
} catch (e) {
console.log(e);
}
await sleep(this.simspeed);
}
}
}
module.exports = Player;