import axios , { AxiosRequestConfig } from 'axios' ; import { inject , injectable } from 'inversify' ; import * as path from 'path' ; import * as url from 'url' ; import urljoin = require ( 'url-join' ) ; import { KodiInfo } from '../IConfigFile' ; import IConfiguration from '../IConfiguration' ; import IApiUtil , { CreateM3U8Option } from './IApiUtil' ; @ injectable ( ) export default class ApiUtil implements IApiUtil { private configuration : IConfiguration ; constructor ( @ inject ( 'IConfiguration' ) configuration ) { this . configuration = configuration ; } param option return public createM3U8PlayListStr ( option ) { const fullUrl = urljoin ( `${ option . isSecure ? 'https' : 'http' }://${ this . getHost ( option . host ) }` , option . baseUrl ) ; return '#EXTM3U\n' + `#EXTINF: ${ option . duration }, ${ option . name }\n` + fullUrl ; } param baseHost return public getHost ( baseHost ) { const config = this . configuration . getConfig ( ) ; return typeof config . subDirectory === 'undefined' ? baseHost : path . join ( baseHost , config . subDirectory ) ; } param source param hkodiInfo public async sendToKodi ( source , kodiInfo ) { const option = { url : url . resolve ( kodiInfo . host , '/jsonrpc' ) , method : 'POST' , headers : { 'Content-Type' : 'application/json' , } , responseType : 'json' , data : { jsonrpc : '2.0' , method : 'Player.Open' , params : { item : { file : source } , } , id : 1 , } , } ; if ( typeof kodiInfo . user !== 'undefined' && typeof kodiInfo . password !== 'undefined' ) { option . auth = { username : kodiInfo . user , password : kodiInfo . password , } ; } await axios . request ( option ) ; } }