2019-10-01 21:34:02 +02:00
'use strict' ;
/ * *
* Module dependencies .
* /
var CP _get = require ( '../lib/CP_get.min' ) ;
/ * *
* Configuration dependencies .
* /
var config = require ( '../config/production/config' ) ;
var modules = require ( '../config/production/modules' ) ;
/ * *
* Node dependencies .
* /
var request = require ( 'request' ) ;
var moment = require ( 'moment' ) ;
moment . locale ( config . language ) ;
/ * *
* Callback .
*
* @ callback Callback
* @ param { Object } err
* @ param { Object } [ render ]
* /
/ * *
* Adding list episodes on index page .
*
* @ param { Object } [ options ]
* @ param { Callback } callback
* /
function indexEpisode ( options , callback ) {
if ( arguments . length === 1 ) {
options = { } ;
options . domain = '' + config . domain ;
2019-10-09 03:51:57 +02:00
options . origin =
config . protocol + '' + config . subdomain + '' + config . domain ;
2019-10-01 21:34:02 +02:00
}
var source = {
url :
modules . episode . data . source === 'iframe'
? 'iframe.video'
: 'streamguard.cc' ,
token :
modules . episode . data . source === 'iframe'
? modules . player . data . iframe . token . trim ( )
: modules . player . data . moonwalk . token . trim ( )
} ;
var url =
'https://' +
source . url +
'/api/serials_updates.json?api_token=' +
source . token ;
getReq ( url , function ( err , list ) {
if ( err || ! list . updates || ! list . updates . length ) {
return callback ( null , null ) ;
}
var query _id = { } ;
list . updates . forEach ( function ( serial ) {
if ( parseInt ( serial . serial . kinopoisk _id ) ) {
query _id [ serial . serial . kinopoisk _id ] = { } ;
}
} ) ;
CP _get . additional (
{ query _id : Object . keys ( query _id ) } ,
'ids' ,
options ,
function ( err , movies ) {
if ( err || ! movies . length ) {
return callback ( null , null ) ;
}
var result = { } ;
result . name = modules . episode . data . index . name ;
result . movies = [ ] ;
for ( var i = 0 , num1 = list . updates . length ; i < num1 ; i ++ ) {
for ( var j = 0 , num2 = movies . length ; j < num2 ; j ++ ) {
if (
parseInt ( list . updates [ i ] . serial . kinopoisk _id ) ===
parseInt ( movies [ j ] . kp _id )
) {
var serial _moon = JSON . stringify ( list . updates [ i ] ) || '' ;
serial _moon = JSON . parse ( serial _moon ) || { } ;
var serial _base = JSON . stringify ( movies [ j ] ) || '' ;
serial _base = JSON . parse ( serial _base ) || { } ;
var season _num = /season=([0-9]{1,4})/i . exec (
serial _moon . episode _iframe _url
) ;
var episode _num = /episode=([0-9]{1,4})/i . exec (
serial _moon . episode _iframe _url
) ;
if ( ! season _num || ! episode _num ) continue ;
var season _url = parseInt ( season _num [ 1 ] ) ;
var episode _url = parseInt ( episode _num [ 1 ] ) ;
var translate _url = parseInt ( serial _moon . serial . translator _id ) ;
var translate = serial _moon . serial . translator
? serial _moon . serial . translator
: modules . episode . data . default ;
var premiere =
serial _moon . added _at &&
! isNaN ( new Date ( serial _moon . added _at ) . getFullYear ( ) )
? moment ( serial _moon . added _at . slice ( 0 , 10 ) ) . format ( 'LL' )
: '' ;
season _url = season _url <= 9 ? '0' + season _url : season _url ;
episode _url = episode _url <= 9 ? '0' + episode _url : episode _url ;
translate _url = translate _url ? '_' + translate _url : '' ;
serial _base . translate = translate ;
serial _base . season = season _num [ 1 ] ;
serial _base . episode = episode _num [ 1 ] ;
serial _base . premiere = premiere ;
serial _base . year = premiere
? new Date ( premiere ) . getFullYear ( )
: new Date ( ) . getFullYear ( ) ;
serial _base . url =
serial _base . url +
'/s' +
season _url +
'e' +
episode _url +
translate _url ;
serial _base . year2 =
serial _base . season && serial _base . episode
? serial _base . season +
' ' +
config . l . season +
' ' +
serial _base . episode +
' ' +
config . l . episode
: serial _base . year ;
serial _base . year3 =
serial _base . season && serial _base . episode
? 'S' + serial _base . season + 'E' + serial _base . episode
: serial _base . year ;
result . movies . push ( serial _base ) ;
}
}
}
var sort _result = [ ] ;
unique : for ( var k = 0 , num3 = result . movies . length ; k < num3 ; k ++ ) {
if ( modules . episode . data . index . latest ) {
for ( var l = 0 , num4 = sort _result . length ; l < num4 ; l ++ ) {
if (
parseInt ( sort _result [ l ] . kp _id ) ===
parseInt ( result . movies [ k ] . kp _id ) &&
modules . episode . data . index . count > k
) {
sort _result [ l ] = result . movies [ k ] ;
continue unique ;
}
}
}
if ( modules . episode . data . index . count > k ) {
sort _result . push ( result . movies [ k ] ) ;
}
}
result . movies = sort _result ;
callback ( null , [ result ] ) ;
}
) ;
} ) ;
/ * *
* Get request on url .
*
* @ param { String } url
* @ param { Callback } callback
* /
function getReq ( url , callback ) {
request (
{ timeout : 500 , agent : false , pool : { maxSockets : 100 } , url : url } ,
function ( error , response , body ) {
var result = body ? tryParseJSON ( body ) : { } ;
try {
if ( error || response . statusCode !== 200 || result . error ) {
console . log ( url , error . code || '' , result . error || '' ) ;
return callback ( 'Moonwalk/Iframe request error.' ) ;
}
callback ( null , result ) ;
} catch ( err ) {
callback ( null , err ) ;
}
}
) ;
}
/ * *
* Valid JSON .
*
* @ param { String } jsonString
* /
function tryParseJSON ( jsonString ) {
try {
var o = JSON . parse ( jsonString ) ;
if ( o && typeof o === 'object' ) {
return o ;
}
} catch ( e ) { }
return { } ;
}
}
/ * *
* Adding a page episodes list id = "#episodesList" .
*
* @ param { String } [ type ]
* @ return { Object }
* /
function codeEpisode ( type ) {
var code = { } ;
code . episodes =
'function cp_episodes(){var a=document.querySelector("#episodesList");if(!a)return!1;var b=a.dataset.id||1,c=new XMLHttpRequest;c.open("GET","/episode.json?id="+b,!0),c.onload=function(b){if(4===c.readyState)if(200===c.status){var d=JSON.parse(c.responseText),e=d[Object.keys(d)[0]];for(var f in e)if(e.hasOwnProperty(f)){var g=document.createElement("ul"),h=document.createElement("li"),i=document.createElement("li"),j=document.createElement("span");/укр/i.test(f)?h.setAttribute("style","opacity:.8;list-style-type:none;cursor:pointer;float:none;border-radius:5px;padding:5px;background:repeating-linear-gradient(180deg, #001b38, #001b38 50%, #4c4000 50%, #4c4000 100%);color:#fff;margin:10px auto;"):/^с у б |^sub/i.test(f)?h.setAttribute("style","opacity:.8;list-style-type:none;cursor:pointer;float:none;border-radius:5px;padding:5px;background:linear-gradient(0deg, #121121, #121121), repeating-linear-gradient(180deg, #350a0f, #350a0f 7.7%, #323232 7.7%, #323232 15.4%);background-size: 40% 53.85%, 100% 100%;background-repeat: no-repeat;background-position: top left;color:#fff;margin:10px auto;"):h.setAttribute("style","opacity:.8;list-style-type:none;cursor:pointer;float:none;border-radius:5px;padding:5px;background:repeating-linear-gradient(180deg, #323232, #323232 33.3%, #001032 33.3%, #001032, #001032 66.6%, #400b07 66.6%, #400b07);color:#fff;margin:10px auto;");g.setAttribute("style","margin:0;padding:0;float:none"),h.setAttribute("class","cinemapress_li"),h.setAttribute("data-click",f),i.setAttribute("style","list-style-type:none;display:none;float:none;margin:0"),i.setAttribute("data-show",f),h.textContent="► "+f,j.setAttribute("style","float:right"),j.textContent="▼",h.appendChild(j),g.appendChild(h);var k=document.createElement("ul");k.setAttribute("style","float:none;margin:0 0 0 20px;");for(var l in e[f])if(e[f].hasOwnProperty(l)){var m=document.createElement("li"),n=document.createElement("li"),o=document.createElement("span");m.setAttribute("style","list-style-type:none;cursor:pointer;float:none;border-radius:5px;padding:5px;background:#666;color:#fff;margin:10px auto;"),m.setAttribute("class","cinemapress_li"),m.setAttribute("data-click",f+l),n.setAttribute("style","list-style-type:none;display:none;float:none;margin:0"),n.setAttribute("data-show",f+l),m.textContent="► "+l,o.setAttribute("style","float:right"),o.textContent="▼",m.appendChild(o),k.appendChild(m);var p=document.createElement("ul");p.setAttribute("style","float:none;margin:0 0 0 20px;");for(var q in e[f][l])if(e[f][l].hasOwnProperty(q)){var r=document.createElement("li"),s=document.createElement("a");r.setAttribute("style","list-style-type:none;float:none;margin:0"),s.setAttribute("href",e[f][l][q].url),s.setAttribute("target","_blank"),s.setAttribute("style","text-decoration:none;float:none"),s.textContent="► "+e[f][l][q].translate+" "+e[f][l][q].season+" "+e[f][l][q].episode,h.textContent = "► " + e[f][l][q].translate + " " + e[f][l][q].season + " " + e[f][l][q].episode,m.textContent = "► " + e[f][l][q].translate + " " + e[f][l][q].season,r.appendChild(s),p.appendChild(r)}n.appendChild(p),k.appendChild(n)}i.appendChild(k),g.appendChild(i),a.appendChild(g);var t=document.querySelectorAll(".episodesListBlock");if(t&&t.length)for(var u=0;u<t.length;u++)t[u].style.display="block"}var v=document.querySelectorAll(".cinemapress_li");if(v&&v.length)for(var w=0;w<v.length;w++)v[w].addEventListener("click",function(){var a=document.querySelector("li[data-show=\'"+this.dataset.click+"\']");a.style.display="block"==a.style.display?"none":"block"})}},c.onerror=function(a){console.error(c.statusText)},c.send(null)}document.addEventListener("DOMContentLoaded",cp_episodes);' ;
code . serials =
'function cp_serials(){var a=document.querySelector("#serialsList");if(!a)return!1;var b=new XMLHttpRequest;b.open("GET","/episode.json",!0),b.onload=function(c){if(4===b.readyState)if(200===b.status){var d=JSON.parse(b.responseText),e=document.createElement("ul");for(var f in d)if(d.hasOwnProperty(f))for(var g in d[f])if(d[f].hasOwnProperty(g))for(var h in d[f][g])if(d[f][g].hasOwnProperty(h))for(var i in d[f][g][h])if(d[f][g][h].hasOwnProperty(i)){var j=document.createElement("li"),k=document.createElement("a");j.setAttribute("style","list-style-type:none"),k.setAttribute("href",d[f][g][h][i].url),k.innerHTML=d[f][g][h][i].title_ru+" "+d[f][g][h][i].season+" "+d[f][g][h][i].episode+" ["+d[f][g][h][i].translate+"]",j.appendChild(k),e.appendChild(j)}a.appendChild(e);var l=document.querySelectorAll(".serialsListBlock");if(l&&l.length)for(var m=0;m<l.length;m++)l[m].style.display="block"}else console.error(b.statusText)},b.onerror=function(a){console.error(b.statusText)},b.send(null)}document.addEventListener("DOMContentLoaded",cp_serials);' ;
return type
? '<script>' + code [ type ] + '</script>'
: '<script>' + code . episodes + code . serials + '</script>' ;
}
/ * *
* Parse data episode .
*
* @ param { String } type
* @ param { Object } options
* @ return { Object }
* /
function parseEpisode ( type , options ) {
if ( arguments . length === 1 ) {
options = { } ;
options . domain = '' + config . domain ;
2019-10-09 03:51:57 +02:00
options . origin =
config . protocol + '' + config . subdomain + '' + config . domain ;
2019-10-01 21:34:02 +02:00
}
var regexpEpisode = new RegExp (
'^s([0-9]{1,4})e([0-9]{1,4})(_([0-9]{1,3})|)$' ,
'ig'
) ;
var execEpisode = regexpEpisode . exec ( type ) ;
var serial = { } ;
serial . season =
execEpisode && execEpisode [ 1 ] ? '' + parseInt ( execEpisode [ 1 ] ) : '' ;
serial . episode =
execEpisode && execEpisode [ 2 ] ? '' + parseInt ( execEpisode [ 2 ] ) : '' ;
serial . translate _id =
execEpisode && execEpisode [ 4 ] ? '' + parseInt ( execEpisode [ 4 ] ) : '' ;
serial . translate = modules . episode . data . default ;
var translators = modules . episode . data . source
? require ( '../files/' + modules . episode . data . source + '.json' )
: require ( '../files/translators.json' ) ;
if ( translators && translators . length ) {
for ( var i = 0 , len = translators . length ; i < len ; i ++ ) {
if ( parseInt ( translators [ i ] . id ) === parseInt ( serial . translate _id ) ) {
serial . translate = translators [ i ] . name ;
break ;
}
}
}
return serial ;
}
module . exports = {
code : codeEpisode ,
parse : parseEpisode ,
index : indexEpisode
} ;