Skip to content

Commit

Permalink
添加自定义头,增加统计功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
HeiSir committed Jun 19, 2020
1 parent c8022b9 commit cde7f4a
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 51 deletions.
36 changes: 31 additions & 5 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,41 @@ html{
border: 1px solid;
border-color: #47b3f7;
box-sizing: border-box;
margin-left: 25px;
}
.heisir .main .addTask input[type=text]{
width: 60%;
padding: 0 10px;
border-right: 0;
border-radius: 15px 0px 0px 15px;
border-radius: 0px;
margin-left: -5px;
}
.heisir .main .addTask input[type=button]{
width: 100px;
.heisir .main .addTask .httpHeader{
width: 80px;
color: #fff;
background: #3385ff;
letter-spacing: 0px;
border-left: 0;
border-radius: 15px 0px 0px 15px;
cursor: pointer;
}

.heisir .main .addTask .headers{
width: 60%;
margin: 10px auto;
}

.heisir .main .addTask .headers .content{
width: 100%;
min-height: 100px;
border: 1px solid;
border-color: #47b3f7;
box-sizing: border-box;
padding: 10px;
margin-left: -70px;
}

.heisir .main .addTask .download{
width: 80px;
color: #fff;
background: #3385ff;
letter-spacing: 2px;
Expand All @@ -136,6 +161,7 @@ html{
cursor: pointer;
margin-left: -5px;
}

.heisir .main .addTask .info .success{
color: mediumseagreen;
margin-top: 5px;
Expand All @@ -146,7 +172,7 @@ html{
}
.heisir .main .TaskList{
width: 85%;
height: calc(100% - 160px);
height: calc(100% - 260px);
margin: 15px auto;
padding: 10px;
overflow-y: auto;
Expand Down
14 changes: 14 additions & 0 deletions ffmpegTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@ const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const { Readable} = require('stream');
const path = require('path');
const got = require('got');



(async () => {
const { body } = await got('http://v.ytsjk.cn/38df87dc4b70456aa2eadde6e713fa38/6a2a090b523d4c9491277a41bb6a2567-dcb98c29eb2d3f032c85e7a6cf2c5b83-sd.m3u8',{headers:{
'Origin':'http://www.ytsjk.cn',
'Referer':'http://www.ytsjk.cn',
}});
console.log(body);
})();


/*
const dir = 'E:\\Project\\my_project\\M3U8-Downloader\\source\\download\\1592447619950\\';
let inputStream = new Readable();
let _ffmpeg = ffmpeg(inputStream)
Expand Down Expand Up @@ -38,3 +51,4 @@ fs.readdir(dir,function(err,files){
inputStream.push(null);
});
*/
22 changes: 20 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
<link rel="stylesheet" href="css/style.css">
</head>
<body class="heisir">
<div class="header"><span class="title">M3U8-Downloader v1.0.5</span>
<div class="header"><span class="title">M3U8-Downloader v1.0.6</span>
<div class="space"></div>
<a class="home" href="https://tools.heisir.cn/HLSDownload/" target="_blank">官网</a>
<a class="qqgroup" href="https://jq.qq.com/?_wv=1027&k=nhFrZBS0">点击加群</a>
<button class="close"></button>
</div>
<div class="main">
<div class="addTask">
<input class=httpHeader type="button" value="自定义头">
<input type="text" placeholder="请输入m3u8地址">
<input type="button" value="下载">
<input class=download type="button" value="下载">
<label><input class="setting_isdelts" type="checkbox" onclick="setting_isdelts()" checked />删除TS文件</label>

<div class="headers" style="display: none;">
<textarea class="content" placeholder="//一行一个Header,例如:
Origin: http://www.ytsjk.cn
Referer: http://www.ytsjk.cn/
"></textarea>
</div>
<div class="info"></div>
</div>
<div class="TaskList">
Expand All @@ -44,5 +52,15 @@
</div>
</div>
<script src="render.js"></script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?300991eff395036b1ba22ae155143ff3";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>

</body>
</html>
128 changes: 102 additions & 26 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
exports.__esModule = true;
var _a = require('electron'), app = _a.app, BrowserWindow = _a.BrowserWindow, Tray = _a.Tray, ipcMain = _a.ipcMain, shell = _a.shell, Menu = _a.Menu;
var spawn = require('child_process').spawn;
Expand Down Expand Up @@ -93,7 +94,7 @@ function createWindow() {
// 与此同时,你应该删除相应的元素。
mainWindow = null;
});
//mainWindow.webContents.openDevTools();
mainWindow.webContents.openDevTools();
}
function createPlayerWindow(src) {
if (playerWindow == null) {
Expand Down Expand Up @@ -152,17 +153,67 @@ app.on('ready', function () {
}
catch (error) {
}
//百度统计代码
(function () { return __awaiter(_this, void 0, void 0, function () {
var HMACCOUNT, headers, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
HMACCOUNT = '';
if (fs.existsSync('tongji.ini')) {
HMACCOUNT = fs.readFileSync('tongji.ini', { encoding: "utf-8", flag: "r" });
}
return [4 /*yield*/, got("http://hm.baidu.com/hm.js?300991eff395036b1ba22ae155143ff3", { headers: { "Referer": "https://tools.heisir.cn/M3U8Soft-Client/", "Cookie": "HMACCOUNT=" + HMACCOUNT } })];
case 1:
headers = (_a.sent()).headers;
try {
HMACCOUNT = headers['set-cookie'][0].match(/HMACCOUNT=(.*?);/i)[1];
fs.writeFileSync('tongji.ini', HMACCOUNT, { encoding: "utf-8", flag: "w" });
}
catch (error_) {
}
console.log(HMACCOUNT);
return [4 /*yield*/, got("http://hm.baidu.com/hm.gif?hca=" + HMACCOUNT + "&cc=1&ck=1&cl=24-bit&ds=1920x1080&vl=977&ep=6621%2C1598&et=3&ja=0&ln=zh-cn&lo=0&lt=" + (new Date().getTime() / 1000) + "&rnd=0&si=300991eff395036b1ba22ae155143ff3&v=1.2.74&lv=3&sn=0&r=0&ww=1920&u=https%3A%2F%2Ftools.heisir.cn%2FM3U8Soft-Client%2F", { headers: { "Referer": "https://tools.heisir.cn/M3U8Soft-Client/", "Cookie": "HMACCOUNT=" + HMACCOUNT } })];
case 2:
_a.sent();
return [4 /*yield*/, got("http://hm.baidu.com/hm.gif?cc=1&ck=1&cl=24-bit&ds=1920x1080&vl=977&et=0&ja=0&ln=zh-cn&lo=0&rnd=0&si=300991eff395036b1ba22ae155143ff3&v=1.2.74&lv=1&sn=0&r=0&ww=1920&ct=!!&tt=M3U8Soft-Client", { headers: { "Referer": "https://tools.heisir.cn/M3U8Soft-Client/", "Cookie": "HMACCOUNT=" + HMACCOUNT } })];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
error_1 = _a.sent();
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
}); })();
});
// 当全部窗口关闭时退出。
app.on('window-all-closed', function () {
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
// 否则绝大部分应用及其菜单栏会保持激活。
if (process.platform !== 'darwin') {
tray = null;
app.quit();
}
;
});
app.on('window-all-closed', function () { return __awaiter(_this, void 0, void 0, function () {
var HMACCOUNT;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
HMACCOUNT = '';
if (!fs.existsSync('tongji.ini')) return [3 /*break*/, 2];
HMACCOUNT = fs.readFileSync('tongji.ini', { encoding: "utf-8", flag: "r" });
return [4 /*yield*/, got("http://hm.baidu.com/hm.gif?cc=1&ck=1&cl=24-bit&ds=1920x1080&vl=977&et=0&ja=0&ln=zh-cn&lo=0&rnd=0&si=300991eff395036b1ba22ae155143ff3&v=1.2.74&lv=1&sn=0&r=0&ww=1920&ct=!!&tt=M3U8Soft-Client", { headers: { "Referer": "https://tools.heisir.cn/M3U8Soft-Client/", "Cookie": "HMACCOUNT=" + HMACCOUNT } })];
case 1:
_a.sent();
_a.label = 2;
case 2:
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
// 否则绝大部分应用及其菜单栏会保持激活。
if (process.platform !== 'darwin') {
tray = null;
app.quit();
}
;
return [2 /*return*/];
}
});
}); });
app.on('activate', function () {
// 在macOS上,当单击dock图标并且没有其他窗口打开时,
// 通常在应用程序中重新创建一个窗口。
Expand All @@ -186,15 +237,34 @@ ipcMain.on("hide-windows", function () {
ipcMain.on('get-all-videos', function (event, arg) {
event.sender.send('get-all-videos-reply', configVideos);
});
ipcMain.on('task-add', function (event, arg) {
ipcMain.on('task-add', function (event, arg, headers) {
return __awaiter(this, void 0, void 0, function () {
var hlsSrc, response, info, code, parser, count_seg, duration_1;
var hlsSrc, _headers, __, mes, _hosts, response, info, code, parser, count_seg, duration_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log(arg);
hlsSrc = arg;
return [4 /*yield*/, got(hlsSrc)["catch"](console.log)];
_headers = {};
if (headers != '') {
__ = headers.match(/([^ ]*?): ?([^ ]*?)(\n|\r|$)/g);
__ && __.forEach(function (_) {
var ___ = _.match(/([^ ]*?): ?([^ ]*?)(\n|\r|$)/i);
___ && (_headers[___[1]] = ___[2]);
});
}
mes = hlsSrc.match(/^https?:\/\/[^/]*/);
_hosts = '';
if (mes && mes.length >= 1) {
_hosts = mes[0];
}
if (_headers['Origin'] == null && _headers['origin'] == null) {
_headers['Origin'] = _hosts;
}
if (_headers['Referer'] == null && _headers['referer'] == null) {
_headers['Referer'] = _hosts;
}
return [4 /*yield*/, got(hlsSrc, { headers: _headers })["catch"](console.log)];
case 1:
response = _a.sent();
{
Expand All @@ -216,11 +286,11 @@ ipcMain.on('task-add', function (event, arg) {
duration_1 += segment.duration;
});
info = "\u70B9\u64AD\u8D44\u6E90\u89E3\u6790\u6210\u529F\uFF0C\u6709 " + count_seg + " \u4E2A\u7247\u6BB5\uFF0C\u65F6\u957F\uFF1A" + formatTime(duration_1) + "\uFF0C\u5373\u5C06\u5F00\u59CB\u7F13\u5B58...";
startDownload(hlsSrc);
startDownload(hlsSrc, _headers);
}
else {
info = "\u76F4\u64AD\u8D44\u6E90\u89E3\u6790\u6210\u529F\uFF0C\u5373\u5C06\u5F00\u59CB\u7F13\u5B58...";
startDownloadLive(hlsSrc);
startDownloadLive(hlsSrc, _headers);
}
}
}
Expand Down Expand Up @@ -273,7 +343,7 @@ var QueueObject = /** @class */ (function () {
if (!(index < 3)) return [3 /*break*/, 10];
if (!!fs.existsSync(filpath)) return [3 /*break*/, 8];
that = this;
return [4 /*yield*/, download(uri_ts, that.dir, { filename: filename + ".dl", timeout: 30000 })["catch"](function (err) {
return [4 /*yield*/, download(uri_ts, that.dir, { filename: filename + ".dl", timeout: 30000, headers: that.headers })["catch"](function (err) {
console.log(err);
if (fs.existsSync(filpath_dl))
fs.unlinkSync(filpath_dl);
Expand Down Expand Up @@ -349,7 +419,8 @@ var QueueObject = /** @class */ (function () {
function queue_callback(that, callback) {
that.callback(callback);
}
function startDownload(url, nId) {
function startDownload(url, headers, nId) {
if (headers === void 0) { headers = null; }
if (nId === void 0) { nId = null; }
return __awaiter(this, void 0, void 0, function () {
var id, dir, filesegments, response, parser, tsQueues, count_seg, count_downloaded, video, segments, iSeg, qo;
Expand All @@ -363,7 +434,7 @@ function startDownload(url, nId) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
return [4 /*yield*/, got(url)["catch"](console.log)];
return [4 /*yield*/, got(url, { headers: headers })["catch"](console.log)];
case 1:
response = _a.sent();
if (response == null || response.body == null || response.body == '') {
Expand All @@ -384,18 +455,21 @@ function startDownload(url, nId) {
time: dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"),
status: '初始化...',
isLiving: false,
headers: headers,
videopath: ''
};
if (nId == null) {
mainWindow.webContents.send('task-notify-create', video);
}
globalCond[id] = true;
segments = parser.manifest.segments;
for (iSeg = 0; iSeg < segments.length; iSeg++) {
qo = new QueueObject();
qo.dir = dir;
qo.idx = iSeg;
qo.id = id;
qo.url = url;
qo.headers = headers;
qo.segment = segments[iSeg];
qo.then = function () {
count_downloaded = count_downloaded + 1;
Expand Down Expand Up @@ -473,10 +547,11 @@ var FFmpegStreamReadable = /** @class */ (function (_super) {
FFmpegStreamReadable.prototype._read = function () { };
return FFmpegStreamReadable;
}(Readable));
function startDownloadLive(url, nId) {
function startDownloadLive(url, headers, nId) {
if (headers === void 0) { headers = null; }
if (nId === void 0) { nId = null; }
return __awaiter(this, void 0, void 0, function () {
var id, dir, count_downloaded, count_seg, video, partent_uri, segmentSet, ffmpegInputStream, ffmpegObj, response, parser, count_seg_1, segments, find, _startTime, _videoDuration, _loop_1, iSeg, state_1, _downloadTime, error_1;
var id, dir, count_downloaded, count_seg, video, partent_uri, segmentSet, ffmpegInputStream, ffmpegObj, response, parser, count_seg_1, segments, find, _startTime, _videoDuration, _loop_1, iSeg, state_1, _downloadTime, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -497,6 +572,7 @@ function startDownloadLive(url, nId) {
time: dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"),
status: '初始化...',
isLiving: true,
headers: headers,
videopath: ''
};
configVideos.push(video);
Expand All @@ -515,7 +591,7 @@ function startDownloadLive(url, nId) {
_a.label = 2;
case 2:
_a.trys.push([2, 12, , 13]);
return [4 /*yield*/, got(url)["catch"](console.log)];
return [4 /*yield*/, got(url, { headers: headers })["catch"](console.log)];
case 3:
response = _a.sent();
if (response == null || response.body == null || response.body == '') {
Expand Down Expand Up @@ -575,7 +651,7 @@ function startDownloadLive(url, nId) {
if (!globalCond[id]) {
return [2 /*return*/, "break"];
}
return [4 /*yield*/, download(uri_ts, dir, { filename: filename + ".dl", timeout: 30000 })["catch"](function (err) {
return [4 /*yield*/, download(uri_ts, dir, { filename: filename + ".dl", timeout: 30000, headers: headers })["catch"](function (err) {
console.log(err);
if (fs.existsSync(filpath_dl)) {
fs.unlinkSync(filpath_dl);
Expand Down Expand Up @@ -688,8 +764,8 @@ function startDownloadLive(url, nId) {
parser = null;
return [3 /*break*/, 13];
case 12:
error_1 = _a.sent();
console.log(error_1.response.body);
error_2 = _a.sent();
console.log(error_2.response.body);
return [3 /*break*/, 13];
case 13: return [3 /*break*/, 1];
case 14:
Expand Down Expand Up @@ -761,10 +837,10 @@ ipcMain.on('StartOrStop', function (event, arg) {
configVideos.forEach(function (Element) {
if (Element.id == id) {
if (Element.isLiving == true) {
startDownloadLive(Element.url, id);
startDownloadLive(Element.url, Element.headers, id);
}
else {
startDownload(Element.url, id);
startDownload(Element.url, Element.headers, id);
}
}
});
Expand Down
Loading

0 comments on commit cde7f4a

Please sign in to comment.