Fixes test script
This commit is contained in:
		| @@ -1,69 +1,72 @@ | |||||||
| var parsers = require('playlist-parser'); | const parsers = require('playlist-parser'); | ||||||
| var M3U = parsers.M3U; | const M3U = parsers.M3U; | ||||||
| var fs = require("fs"); | const fs = require("fs"); | ||||||
| var axios = require('axios') | const axios = require('axios') | ||||||
| var path = require('path') | const path = require('path') | ||||||
|  |  | ||||||
| var errorLog = path.resolve(__dirname) + '/../error.log' | const errorLog = 'error.log' | ||||||
|  | const timeout = 10000 | ||||||
|  |  | ||||||
| var instance = axios.create() | let tests = 0 | ||||||
|  | let channels = 0 | ||||||
|  | let failures = 0 | ||||||
|  |  | ||||||
| function _writeToLog(test, msg, url) { | const http = axios.create({ timeout }) | ||||||
|  | http.defaults.headers.common["User-Agent"] = "VLC/2.2.4 LibVLC/2.2.4" | ||||||
|  |  | ||||||
|  | function _writeToLog(test, country, msg, url) { | ||||||
|   var now = new Date() |   var now = new Date() | ||||||
|   var line = `${now.toISOString()} ${test}(): ${msg} '${url}'` |   var line = `${test}(): ${country}: ${msg} '${url}'` | ||||||
|   fs.appendFileSync(errorLog, line + '\n') |   fs.appendFileSync(path.resolve(__dirname) + '/../' + errorLog, now.toISOString() + ' ' + line + '\n') | ||||||
|  |   console.log(line) | ||||||
| } | } | ||||||
|  |  | ||||||
| function _getFullPathToFile(parent, file) { | function _loadPlaylist(filename) { | ||||||
|   if(/(http|https)/i.test(file) || !parent) { |   return M3U.parse(fs.readFileSync(path.resolve(__dirname) + "/../" + filename, { encoding: "utf8" })) | ||||||
|     return file |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   var parsedUrl = parent.substring(0, parent.lastIndexOf("/")) |  | ||||||
|  |  | ||||||
|   return parsedUrl + '/' + file |  | ||||||
| } | } | ||||||
|  |  | ||||||
| function _parsePlaylist(parent, playlist) { | async function testAllLinksIsWorking() { | ||||||
|   playlist.forEach(async (item) => { |  | ||||||
|     if(!item) return |  | ||||||
|  |  | ||||||
|     var file = _getFullPathToFile(parent, item.file) |   tests++ | ||||||
|  |  | ||||||
|     if(/^(http)/i.test(file) && /(\.m3u|\.m3u8)/i.test(file)) { |   let countries = _loadPlaylist('index.m3u') | ||||||
|  |   // countries = countries.slice(0, 2) | ||||||
|  |  | ||||||
|  |   for(let country of countries) { | ||||||
|  |  | ||||||
|  |     const playlist = _loadPlaylist(country.file) | ||||||
|  |  | ||||||
|  |     for(let channel of playlist) { | ||||||
|  |  | ||||||
|  |       channels++ | ||||||
|  |  | ||||||
|       try { |       try { | ||||||
|         var response = await instance.get(file) |  | ||||||
|         // console.log(file) |  | ||||||
|         // console.log(response.status) |  | ||||||
|  |  | ||||||
|         // DEBUG: return errors if link is working |         await http.get(channel.file) | ||||||
|         // var sublist = M3U.parse(response.data); |  | ||||||
|         // _parsePlaylist(file, sublist) |  | ||||||
|  |  | ||||||
|       } catch(err) { |         continue | ||||||
|         console.log(file) |  | ||||||
|         console.log('Error:',err.message) |       } catch (err) { | ||||||
|  |  | ||||||
|  |         failures++ | ||||||
|  |  | ||||||
|  |         _writeToLog('testAllLinksIsWorking', country.file, err.message, channel.file) | ||||||
|  |  | ||||||
|         if(err.response || err.request) { |  | ||||||
|           _writeToLog('testThatAllLinksIsWorking', err.message, file) |  | ||||||
|           process.exit(0) |  | ||||||
|         } |  | ||||||
|       } |       } | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|     return |   if(failures === 0) { | ||||||
|   }) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function testThatAllLinksIsWorking() { |     console.log(`OK (${tests} tests, ${channels} channels)`) | ||||||
|      |      | ||||||
|   var playlist = M3U.parse(fs.readFileSync(path.resolve(__dirname) + "/../index.m3u", { encoding: "utf8" })); |   } else { | ||||||
|   // playlist = playlist.slice(1600, 1700) |  | ||||||
|   // playlist = [{ file: 'http://163.172.107.234:1914/live/medoum1/FH6Oxe1vOH/19.m3u8' }] |  | ||||||
|  |  | ||||||
|   _parsePlaylist(null, playlist) |     console.log(`FAILURES (${tests} tests, ${channels} channels, ${failures} failures)`) | ||||||
|  |  | ||||||
|  |   } | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| testThatAllLinksIsWorking() | testAllLinksIsWorking() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user