iptv/test/index.js

70 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-11-16 00:41:15 +01:00
var parsers = require('playlist-parser');
var M3U = parsers.M3U;
var fs = require("fs");
var axios = require('axios')
var path = require('path')
var errorLog = path.resolve(__dirname) + '/../error.log'
2018-11-16 04:46:05 +01:00
var instance = axios.create()
2018-11-16 00:41:15 +01:00
function _writeToLog(test, msg, url) {
var now = new Date()
var line = `${now.toISOString()} ${test}(): ${msg} '${url}'`
fs.appendFileSync(errorLog, line + '\n')
}
function _getFullPathToFile(parent, file) {
if(/(http|https)/i.test(file) || !parent) {
return file
}
var parsedUrl = parent.substring(0, parent.lastIndexOf("/"))
return parsedUrl + '/' + file
}
function _parsePlaylist(parent, playlist) {
playlist.forEach(async (item) => {
if(!item) return
var file = _getFullPathToFile(parent, item.file)
2018-11-16 05:40:06 +01:00
if(/^(http)/i.test(file) && /(\.m3u|\.m3u8)/i.test(file)) {
2018-11-16 00:41:15 +01:00
2018-11-16 04:46:05 +01:00
try {
2018-11-16 00:41:15 +01:00
var response = await instance.get(file)
2018-11-17 21:50:32 +01:00
// console.log(file)
// console.log(response.status)
2018-11-16 00:41:15 +01:00
2018-11-16 05:40:06 +01:00
// DEBUG: return errors if link is working
2018-11-16 04:46:05 +01:00
// var sublist = M3U.parse(response.data);
// _parsePlaylist(file, sublist)
2018-11-16 00:41:15 +01:00
} catch(err) {
console.log(file)
console.log('Error:',err.message)
2018-11-16 04:46:05 +01:00
if(err.response || err.request) {
2018-11-16 00:41:15 +01:00
_writeToLog('testThatAllLinksIsWorking', err.message, file)
2018-11-17 21:50:32 +01:00
process.exit(0)
2018-11-16 00:41:15 +01:00
}
}
}
return
})
}
function testThatAllLinksIsWorking() {
var playlist = M3U.parse(fs.readFileSync(path.resolve(__dirname) + "/../index.m3u", { encoding: "utf8" }));
// playlist = playlist.slice(1600, 1700)
2018-11-16 04:46:05 +01:00
// playlist = [{ file: 'http://163.172.107.234:1914/live/medoum1/FH6Oxe1vOH/19.m3u8' }]
2018-11-16 00:41:15 +01:00
_parsePlaylist(null, playlist)
}
testThatAllLinksIsWorking()