import { inject , injectable } from 'inversify' ; import * as apid from '../../../../api' ; import Program from '../../../db/entities/Program' ; import DateUtil from '../../../util/DateUtil' ; import IChannelDB from '../../db/IChannelDB' ; import IProgramDB from '../../db/IProgramDB' ; import IIPTVApiModel from './IIPTVApiModel' ; @ injectable ( ) class IPTVApiModel implements IIPTVApiModel { private channelDB : IChannelDB ; private programDB : IProgramDB ; constructor ( @ inject ( 'IChannelDB' ) channelDB , @ inject ( 'IProgramDB' ) programDB ) { this . channelDB = channelDB ; this . programDB = programDB ; } param host param isSecure param mode param isHalfWidth return public async getChannelList ( host , isSecure , mode , isHalfWidth , subDirectory ? ) { const channels = await this . channelDB . findAll ( true ) ; const channelIndex = { } ; let str = '#EXTM3U\n' ; for ( const channel of channels ) { if ( channel . type !== 1 ) { continue ; } let channelName = isHalfWidth === true ? channel . halfWidthName : channel . name ; if ( typeof channelIndex [ channelName ] === 'undefined' ) { channelIndex [ channelName ] = 0 ; } else { channelIndex [ channelName ] += 1 ; for ( let i = 0 ; i <= channelIndex [ channelName ] ; i ++ ) { channelName += ' ' ; } } let logo = '' ; const base = subDirectory === undefined ? host : `${ host }${ subDirectory }` ; if ( channel . hasLogoData ) { logo = `tvg-logo="${ isSecure ? 'https' : 'http' }://${ base }/api/channels/${ channel . id }/logo"` ; } str += `#EXTINF:-1 tvg-id="${ channel . id }" ${ logo } group-title="${ channel . channelType }",${ channelName }　\n` ; str += `${ isSecure ? 'https' : 'http' }://${ base }/api/streams/live/${ channel . id }/m2ts?mode=${ mode }\n` ; } return str ; } param days param isHalfWidth return public async getEpg ( days , isHalfWidth ) { const now = new Date ( ) . getTime ( ) ; const programs = await this . programDB . findSchedule ( { startAt : now , endAt : now + 1000 * 60 * 60 * 24 * days , isHalfWidth : isHalfWidth , types : [ 'GR' , 'BS' , 'CS' , 'SKY' ] , } ) ; const channels = await this . channelDB . findAll ( ) ; const programsIndex = { } ; for ( const program of programs ) { if ( typeof programsIndex [ program . channelId ] === 'undefined' ) { programsIndex [ program . channelId ] = [ ] ; } program . name = this . replaceStr ( program . name ) ; if ( program . description !== null ) { program . description = this . replaceStr ( program . description ) ; if ( program . extended !== null ) { program . description += this . replaceStr ( program . extended ) ; } } programsIndex [ program . channelId ] . push ( program ) ; } let str = '<?xml version="1.0" encoding="UTF-8"?>' + '<!DOCTYPE tv SYSTEM "xmltv.dtd">' + '<tv generator-info-name="EPGStation">' ; for ( const channel of channels ) { if ( typeof programsIndex [ channel . id ] === 'undefined' ) { continue ; } str += `<channel id="${ channel . id }" tp="${ channel . channel }">` ; str += `<display-name lang="ja_JP">${ isHalfWidth === true ? channel . halfWidthName : channel . name }</display-name>` ; str += `<service_id>${ channel . serviceId }</service_id>` ; str += '</channel>\n' ; for ( const program of programsIndex [ channel . id ] ) { str += `<programme start="${ this . getTimeStr ( program . startAt ) }" stop="${ this . getTimeStr ( program . endAt ) }" channel="${ program . channelId }">` ; str += `<title lang="ja_JP">${ program . name }</title>` ; if ( program . description !== null ) { str += `    <desc lang="ja_JP">${ program . description }</desc>` ; } str += '</programme>' ; } } str += '</tv>' ; return str ; } param str return private replaceStr ( str ) { return str . replace ( /</g , '＜' ) . replace ( />/g , '＞' ) . replace ( /&/g , '＆' ) . replace ( /"/g , '”' ) . replace ( /'/g , '’' ) . replace ( /\x1a/g , '' ) ; } param time return private getTimeStr ( time ) { return DateUtil . format ( new Date ( time ) , `yyyyMMddhhmmss ${ IPTVApiModel . TIMEZONE }` ) ; } } namespace IPTVApiModel { export const TIMEZONE = new Date ( ) . toString ( ) . replace ( /^.*GMT([+-]\d{4}).*$/ , '$1' ) ; } export default IPTVApiModel ;