iptv/scripts/commands/save-results.js

177 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-02-05 04:59:34 +01:00
const _ = require('lodash')
const statuses = require('../data/statuses')
const { db, store, parser, file, logger } = require('../core')
const items = []
2022-02-07 04:51:59 +01:00
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/load-cluster'
2022-02-05 04:59:34 +01:00
async function main() {
2022-02-07 04:51:59 +01:00
let streams = await loadStreams()
const results = await loadResults()
const origins = await findOrigins(results)
streams = await updateStreams(streams, results, origins)
await updateDatabase(streams)
2022-02-05 04:59:34 +01:00
}
main()
2022-02-07 04:51:59 +01:00
async function loadStreams() {
logger.info('loading streams...')
2022-02-05 04:59:34 +01:00
2022-02-07 04:51:59 +01:00
await db.streams.load()
const streams = await db.streams.find({})
2022-02-05 04:59:34 +01:00
logger.info(`found ${streams.length} streams`)
2022-02-07 04:51:59 +01:00
return streams
2022-02-05 04:59:34 +01:00
}
async function loadResults() {
2022-02-07 04:51:59 +01:00
logger.info('loading results from logs...')
2022-02-05 04:59:34 +01:00
2022-02-07 04:51:59 +01:00
const results = {}
const files = await file.list(`${LOGS_DIR}/cluster_*.log`)
2022-02-05 04:59:34 +01:00
for (const filepath of files) {
const parsed = await parser.parseLogs(filepath)
2022-02-07 04:51:59 +01:00
for (const item of parsed) {
results[item._id] = item
2022-02-05 04:59:34 +01:00
}
}
logger.info(`found ${Object.values(results).length} results`)
2022-02-07 04:51:59 +01:00
return results
2022-02-05 04:59:34 +01:00
}
2022-02-07 04:51:59 +01:00
async function findOrigins(results = {}) {
2022-02-05 04:59:34 +01:00
logger.info('searching for stream origins...')
2022-02-07 04:51:59 +01:00
const origins = {}
2022-02-05 04:59:34 +01:00
for (const { error, requests } of Object.values(results)) {
if (error || !Array.isArray(requests) || !requests.length) continue
let origin = requests.shift()
origin = new URL(origin.url)
for (const request of requests) {
const curr = new URL(request.url)
const key = curr.href.replace(/(^\w+:|^)/, '')
if (!origins[key] && curr.host === origin.host) {
origins[key] = origin.href
}
}
}
logger.info(`found ${_.uniq(Object.values(origins)).length} origins`)
2022-02-07 04:51:59 +01:00
return origins
2022-02-05 04:59:34 +01:00
}
2022-02-07 04:51:59 +01:00
async function updateStreams(items = [], results = {}, origins = {}) {
2022-02-05 04:59:34 +01:00
logger.info('updating streams...')
let updated = 0
2022-02-07 04:51:59 +01:00
const output = []
for (const item of items) {
2022-02-05 04:59:34 +01:00
const stream = store.create(item)
const result = results[item._id]
if (result) {
const { error, streams, requests } = result
const resolution = parseResolution(streams)
2022-02-07 04:51:59 +01:00
const origin = findOrigin(requests, origins)
2022-02-05 04:59:34 +01:00
let status = parseStatus(error)
if (status) {
const prevStatus = item.status
if (prevStatus.code === 'not_247')
// not_247 -> * = not_247
status = item.status
else if (prevStatus.code === 'geo_blocked')
// geo_blocked -> * = geo_blocked
status = item.status
else if (status.code === 'geo_blocked')
// * -> geo_blocked = *
status = item.status
else if (prevStatus.code === 'offline' && status.code === 'online')
// offline -> online = not_247
status = statuses['not_247']
stream.set('status', { status })
stream.set('is_broken', { status: stream.get('status') })
}
if (resolution) {
stream.set('resolution', { resolution })
}
if (origin) {
stream.set('url', { url: origin })
}
}
if (stream.changed) {
stream.set('updated', true)
2022-02-07 04:51:59 +01:00
output.push(stream.data())
2022-02-05 04:59:34 +01:00
updated++
}
}
2022-02-07 04:51:59 +01:00
logger.info(`updated ${updated} streams`)
return output
2022-02-05 04:59:34 +01:00
}
2022-02-07 04:51:59 +01:00
async function updateDatabase(streams = []) {
2022-02-05 04:59:34 +01:00
logger.info('updating database...')
2022-02-07 04:51:59 +01:00
for (const stream of streams) {
await db.streams.update({ _id: stream._id }, stream)
2022-02-05 04:59:34 +01:00
}
2022-02-07 04:51:59 +01:00
db.streams.compact()
2022-02-05 04:59:34 +01:00
logger.info('done')
}
2022-02-07 04:51:59 +01:00
function findOrigin(requests = [], origins = {}) {
2022-02-05 04:59:34 +01:00
if (origins && Array.isArray(requests)) {
requests = requests.map(r => r.url.replace(/(^\w+:|^)/, ''))
for (const url of requests) {
if (origins[url]) {
return origins[url]
}
}
}
return null
}
function parseResolution(streams) {
const resolution = streams
.filter(s => s.codec_type === 'video')
.reduce(
(acc, curr) => {
if (curr.height > acc.height) return { width: curr.width, height: curr.height }
return acc
},
{ width: 0, height: 0 }
)
if (resolution.width > 0 && resolution.height > 0) return resolution
return null
}
function parseStatus(error) {
if (error) {
if (error.includes('timed out')) {
return statuses['timeout']
} else if (error.includes('403')) {
return statuses['geo_blocked']
}
return statuses['offline']
}
return statuses['online']
}