mirror of
https://github.com/lyz05/danmaku.git
synced 2026-02-03 02:04:38 +08:00
feat: 优化xml弹幕输出
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
const urlmodule = require("url");
|
||||
const axios = require("axios");
|
||||
const got = require("got");
|
||||
const {inflateRawSync} = require("zlib");
|
||||
|
||||
function Bilibili() {
|
||||
this.name = "B站";
|
||||
@@ -62,29 +60,17 @@ function Bilibili() {
|
||||
|
||||
};
|
||||
|
||||
this.parse = async (urls) => {
|
||||
// B站使用特殊的压缩方法,需要使用got模块
|
||||
const bufferData = await got(urls[0], {
|
||||
decompress: false
|
||||
}).buffer();
|
||||
const content = inflateRawSync(bufferData).toString();
|
||||
return content;
|
||||
};
|
||||
|
||||
this.work = async (url) => {
|
||||
const urls = await this.resolve(url);
|
||||
if (!this.error_msg) {
|
||||
console.log(this.name,"api lens:",urls.length);
|
||||
this.content = await this.parse(urls);
|
||||
this.url = urls[0];
|
||||
}
|
||||
return {
|
||||
title: this.title,
|
||||
content: this.content,
|
||||
url: this.url,
|
||||
msg: this.error_msg? this.error_msg: "ok"
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = Bilibili;
|
||||
|
||||
@@ -108,7 +108,7 @@ function Iqiyi() {
|
||||
memory();
|
||||
}
|
||||
datas = undefined;
|
||||
contents = make_response(contents);
|
||||
// contents = make_response(contents);
|
||||
memory();
|
||||
return contents;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const urlmodule = require("url");
|
||||
const axios = require("axios");
|
||||
const {time_to_second, make_response, content_template} = require("./utils");
|
||||
const {time_to_second, content_template} = require("./utils");
|
||||
|
||||
function Mgtv() {
|
||||
this.name = "芒果TV";
|
||||
@@ -45,7 +45,7 @@ function Mgtv() {
|
||||
contents.push(content);
|
||||
}
|
||||
}
|
||||
contents = make_response(contents);
|
||||
// contents = make_response(contents);
|
||||
return contents;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const urlmodule = require("url");
|
||||
const axios = require("axios");
|
||||
const whacko = require("whacko");
|
||||
const {make_response, content_template} = require("./utils");
|
||||
const {content_template} = require("./utils");
|
||||
|
||||
function Tencentvideo() {
|
||||
this.name = "腾讯视频";
|
||||
@@ -61,7 +61,7 @@ function Tencentvideo() {
|
||||
contents.push(content);
|
||||
}
|
||||
}
|
||||
contents = make_response(contents);
|
||||
// contents = make_response(contents);
|
||||
return contents;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
const convert = require("xml-js");
|
||||
|
||||
const content_template = {
|
||||
timepoint: 0,
|
||||
content: "",
|
||||
@@ -21,30 +19,4 @@ function time_to_second(time) {
|
||||
return s;
|
||||
}
|
||||
|
||||
function make_response(contents) {
|
||||
let xml = {
|
||||
_declaration: {
|
||||
_attributes: {
|
||||
version: "1.0",
|
||||
encoding: "utf-8"
|
||||
}
|
||||
},
|
||||
i: {
|
||||
d: []
|
||||
}
|
||||
};
|
||||
for (let content of contents) {
|
||||
xml.i.d.push({
|
||||
_attributes: {
|
||||
p: `${content.timepoint},${content.ct},${content.size},${content.color},${content.unixtime},${content.uid},26732601000067074`
|
||||
},
|
||||
_text: content.content
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const res = convert.js2xml(xml, {compact: true, spaces: 4});
|
||||
return res;
|
||||
}
|
||||
|
||||
module.exports = {time_to_second, make_response, content_template};
|
||||
module.exports = {time_to_second, content_template};
|
||||
|
||||
@@ -2,10 +2,7 @@ const urlmodule = require('url');
|
||||
const axios = require('axios');
|
||||
const cookie = require('cookie');
|
||||
const crypto = require('crypto');
|
||||
const {
|
||||
make_response,
|
||||
content_template
|
||||
} = require('./utils');
|
||||
const { content_template } = require('./utils');
|
||||
|
||||
function Youku() {
|
||||
this.name = '优酷';
|
||||
@@ -153,7 +150,7 @@ function Youku() {
|
||||
contents.push(content);
|
||||
}
|
||||
}
|
||||
contents = make_response(contents);
|
||||
// contents = make_response(contents);
|
||||
return contents;
|
||||
};
|
||||
|
||||
@@ -173,9 +170,9 @@ module.exports = Youku;
|
||||
|
||||
if (!module.parent) {
|
||||
const b = new Youku();
|
||||
b.work(b.example_urls[0])
|
||||
b.work(b.example_urls[2])
|
||||
.then(() => {
|
||||
console.log(b.content);
|
||||
// console.log(b.content);
|
||||
console.log(b.title);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -75,15 +75,18 @@ router.get("/", async function (req, res) {
|
||||
const ret = await build_response(url, req);
|
||||
memory(); //显示内存使用量
|
||||
if (ret.msg !== "ok") {
|
||||
res.status(403)
|
||||
.send(ret.msg);
|
||||
res.status(403).send(ret.msg);
|
||||
return;
|
||||
} else if (download) {
|
||||
res.attachment(ret.title + ".xml");
|
||||
res.end(ret.content);
|
||||
} else {
|
||||
res.type("application/xml");
|
||||
res.end(ret.content);
|
||||
}
|
||||
//B站视频,直接重定向
|
||||
if (ret.url)
|
||||
res.redirect(ret.url);
|
||||
else
|
||||
res.render("danmaku-xml",{contents: ret.content});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -53,8 +53,14 @@ describe("App", () => {
|
||||
.get("/")
|
||||
.query({url})
|
||||
.end((err, res) => {
|
||||
if (err) {
|
||||
//B站弹幕获取会遇到解压错误
|
||||
err.code.should.equal("Z_DATA_ERROR")
|
||||
}
|
||||
if (res) {
|
||||
res.should.have.status(200);
|
||||
res.header["content-type"].should.equal("application/xml");
|
||||
res.header["content-type"].should.equal("application/xml; charset=utf-8");
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
</p>
|
||||
<p>
|
||||
相关软件<a href="/sub/download">下载链接</a><br>
|
||||
<a href="/sub/cache">立即更新</a>订阅缓存
|
||||
</p>
|
||||
|
||||
<h3 class="card-heading">Telegram 代理:</h3>
|
||||
|
||||
8
views/danmaku-xml.ejs
Normal file
8
views/danmaku-xml.ejs
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<i>
|
||||
<% for (const content of contents) { %>
|
||||
<d p="<%= content.timepoint %>,<%= content.ct %>,<%= content.size %>,<%= content.color %>,<%= content.unixtime %>,<%= content.uid %>,26732601000067074">
|
||||
<%=content.content%>
|
||||
</d>
|
||||
<% } %>
|
||||
</i>
|
||||
Reference in New Issue
Block a user