CinemaPress/lib/CP_cron.js

1457 lines
40 KiB
JavaScript
Raw Normal View History

2019-10-01 21:34:02 +02:00
'use strict';
/**
* Node dependencies.
*/
var adop = require('adop');
2019-10-01 21:34:02 +02:00
var exec = require('child_process').exec;
var request = require('request');
var sinoni = require('sinoni');
var async = require('async');
2019-11-05 23:41:20 +01:00
var util = require('util');
var td = new util.TextDecoder('utf8', { fatal: true });
2019-10-01 21:34:02 +02:00
var path = require('path');
var fs = require('fs');
var Imap = require('imap');
var MP = require('mailparser-mit').MailParser;
2019-11-05 23:41:20 +01:00
var nodemailer = require('nodemailer');
2019-11-15 04:08:38 +01:00
var mimemessage = require('mimemessage');
2019-10-01 21:34:02 +02:00
/**
* Global env.
*/
try {
var p = tryParseJSON(
fs.readFileSync(
path.join(path.dirname(__filename), '..', 'process.json'),
'utf8'
)
);
var e = p.apps[0].env;
for (var prop in e) {
if (e.hasOwnProperty(prop)) {
process.env[prop] = e[prop];
}
}
} catch (err) {
return console.log('NOT FILE PROCESS DATA');
}
/**
* Module dependencies.
*/
var CP_get = require('./CP_get.min');
var CP_save = require('./CP_save.min');
var CP_cache = require('./CP_cache');
var CP_translit = require('../lib/CP_translit');
var CP_structure = require('../lib/CP_structure');
2019-10-01 21:34:02 +02:00
/**
* Configuration dependencies.
*/
var config = require('../config/production/config');
var modules = require('../config/production/modules');
/**
* Route dependencies.
*/
var movie = require('../routes/paths/movie');
2020-02-24 00:05:10 +01:00
var force = process.argv.slice(2)[0] || false;
2020-01-24 03:05:46 +01:00
var timeZone = new Date();
2019-10-01 21:34:02 +02:00
2019-11-16 04:02:42 +01:00
var hour = new Date(timeZone).getHours() + 1;
console.log(timeZone);
2019-11-16 03:26:17 +01:00
2019-10-01 21:34:02 +02:00
var active = {
num: 0,
process: {}
};
/**
* Parser new movie ids.
2019-10-01 21:34:02 +02:00
*/
if (
modules.content.status &&
modules.content.data.scraper &&
modules.content.data.scraper.length
) {
2019-10-01 21:34:02 +02:00
active.num++;
active.process.scraper = true;
2019-10-01 21:34:02 +02:00
async.eachOfLimit(
2020-02-24 00:05:10 +01:00
modules.content.data.scraper.split('\n'),
2019-10-01 21:34:02 +02:00
1,
function(task, index, callback) {
var parse = task
.replace(/(^\s*)|(\s*)$/g, '')
.replace(/\s*~\s*/g, '~')
.split('~');
if (task.charAt(0) === '#' || parse.length < 5) {
return callback();
}
var p = {
every: parseInt(parse[0]),
url: parse[1],
regex_url: parse[2],
regex_id: parse[3],
collection: parse[4]
};
if (hour % p.every) return callback();
request(
{
url: p.url,
method: 'GET',
timeout: 15000,
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/79.0.' +
(Math.floor(Math.random() * 600) + 1) +
'.100 Safari/537.36'
}
},
function(error, response, body) {
if (error || response.statusCode !== 200 || !body) {
console.error(task, (error && error.code) || '', body);
return callback();
}
var ids = [];
var urls = [];
if (p.regex_url) {
var u;
var regex_url = new RegExp(p.regex_url, 'ig');
while ((u = regex_url.exec(body)) !== null) {
if (/(\/\/|http)/i.test(u[1])) {
urls.push(u[1]);
} else if (/\//i.test(u[1])) {
urls.push(
require('url').parse(p.url).protocol +
'//' +
require('url').parse(p.url).host +
u[1]
);
} else {
var s = p.url.split('/');
s.pop();
var slash = s.join('/');
urls.push(slash + '/' + u[1]);
2019-10-01 21:34:02 +02:00
}
}
}
if (!urls.length) urls.push(p.url);
console.log(urls, urls.length);
async.eachOfLimit(
urls,
1,
function(url, i, callback) {
request(
{
url: url,
method: 'GET',
timeout: 15000,
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/54.0.' +
(Math.floor(Math.random() * 600) + 1) +
'.100 Safari/537.36'
2019-10-01 21:34:02 +02:00
}
},
function(error2, response2, body2) {
if (error2 || response2.statusCode !== 200 || !body2) {
console.error(task, error2.code, body2);
return callback();
}
var found;
var regex_id = new RegExp(p.regex_id, 'ig');
while ((found = regex_id.exec(body2)) !== null) {
if (found[1] && ids.indexOf(found[1]) === -1) {
ids.push(found[1]);
}
}
2019-10-18 04:56:31 +02:00
setTimeout(function() {
callback();
}, Math.floor(Math.random() * 2000 + 1000));
}
);
},
function() {
console.log('IDs:', ids);
if (!ids.length) return callback();
saveData(
p.collection,
ids.map(function(id) {
return { kinopoisk_id: id };
}),
function() {
callback();
2019-10-01 21:34:02 +02:00
}
);
}
);
}
);
2019-10-01 21:34:02 +02:00
},
function() {
2019-10-01 21:34:02 +02:00
active.num--;
active.process.scraper = false;
2019-10-01 21:34:02 +02:00
}
);
2019-11-16 02:59:07 +01:00
} else {
console.log(
'NOT START:',
'CONTENT',
2019-11-16 02:59:07 +01:00
'status',
!!modules.content.status,
'scraper',
modules.content.data.scraper.length
2019-11-16 02:59:07 +01:00
);
2019-10-01 21:34:02 +02:00
}
/**
* API new movie ids.
2019-10-01 21:34:02 +02:00
*/
if (
modules.content.status &&
modules.content.data.custom &&
modules.content.data.custom.length
2019-10-01 21:34:02 +02:00
) {
active.num++;
active.process.content = true;
async.eachOfLimit(
modules.content.data.custom,
2019-10-01 21:34:02 +02:00
1,
function(task, index, callback) {
var parse = task
.replace(/(^\s*)|(\s*)$/g, '')
.replace(/\s*~\s*/g, '~')
.split('~');
if (task.charAt(0) === '#' || parse.length < 3) {
2019-10-01 21:34:02 +02:00
return callback();
}
var obj = [
{
name: 'kinopoisk_id',
path: parse[1],
type: 'number'
}
];
if (parse[3]) {
obj.push({
name: 'translate',
path: parse[3],
type: 'string'
});
}
if (parse[4]) {
obj.push({
name: 'quality',
path: parse[4],
type: 'string'
});
}
request(
{
url: parse[0],
method: 'GET',
timeout: 15000
},
function(error, response, body) {
if (error || response.statusCode !== 200 || !body) {
console.error(task, (error && error.code) || '', body);
return callback();
2019-10-28 01:01:31 +01:00
}
var movies = [];
var sorting = [];
var all = adop(tryParseJSON(body), obj);
var ids = all.reduce(function(r, a) {
if (sorting.indexOf(parseInt(a['kinopoisk_id'])) === -1) {
sorting.push(parseInt(a['kinopoisk_id']));
}
r[a['kinopoisk_id']] = r[a['kinopoisk_id']] || [];
r[a['kinopoisk_id']].push(a);
return r;
}, {});
Object.keys(ids)
.filter(function(id) {
return parseInt(id);
})
.forEach(function(id) {
var m = { kinopoisk_id: parseInt(id) };
if (ids[id][0] && ids[id][0].translate) {
2020-02-24 00:05:10 +01:00
var reg = /[^(]+\s*\([^)]+\)\s*\(([^)]+)\)/i.exec(
ids[id][0].translate
);
m.translate = reg && reg[1] ? reg[1] : ids[id][0].translate;
2019-10-28 01:01:31 +01:00
}
if (ids[id][0] && ids[id][0].quality) {
m.quality = ids[id][0].quality;
2019-10-28 01:01:31 +01:00
}
movies.push(m);
2019-10-28 01:01:31 +01:00
});
movies.sort(function(a, b) {
return (
sorting.indexOf(a.kinopoisk_id) - sorting.indexOf(b.kinopoisk_id)
);
});
if (!movies.length) return callback();
saveData(parse[2], movies, function() {
callback();
2019-10-01 21:34:02 +02:00
});
}
);
},
function() {
active.num--;
active.process.content = false;
}
);
} else {
console.log(
'NOT START:',
'CONTENT',
'status',
!!modules.content.status,
'custom',
modules.content.data.custom.length
);
}
/**
* API new episodes.
*/
if (
modules.episode.status &&
modules.episode.data.index.custom &&
modules.episode.data.index.custom.length
) {
active.num++;
active.process.episode = true;
async.eachOfLimit(
modules.episode.data.index.custom,
1,
function(task, index, callback) {
var parse = task
.replace(/(^\s*)|(\s*)$/g, '')
.replace(/\s*~\s*/g, '~')
.split('~');
if (task.charAt(0) === '#' || parse.length < 4) {
return callback();
}
var obj = [
{
name: 'kinopoisk_id',
path: parse[1],
type: 'number'
},
{
name: 'season',
path: parse[2],
type: 'number'
},
{
name: 'episode',
path: parse[3],
type: 'number'
}
];
if (parse[4]) {
obj.push({
name: 'translate',
path: parse[4],
type: 'string'
});
}
request(
{
url: parse[0],
method: 'GET',
timeout: 15000
},
function(error, response, body) {
if (error || response.statusCode !== 200 || !body) {
console.error(task, (error && error.code) || '', body);
return callback();
}
var results = [];
var sorting = [];
var all = adop(tryParseJSON(body), obj);
var ids = all.reduce(function(r, a) {
if (sorting.indexOf(a['kinopoisk_id']) === -1) {
sorting.push(a['kinopoisk_id']);
}
r[a['kinopoisk_id']] = r[a['kinopoisk_id']] || [];
r[a['kinopoisk_id']].push(a);
return r;
}, {});
Object.keys(ids)
.filter(function(id) {
return id && id !== '0';
})
.forEach(function(id) {
var t = ids[id]
.sort(function(a, b) {
return parseFloat(b.season) - parseFloat(a.season);
})
.filter(function(episode, i, a) {
return a[0].season <= episode.season;
})
.sort(function(a, b) {
return parseFloat(b.episode) - parseFloat(a.episode);
})
.filter(function(episode, i, a) {
return a[0].episode === episode.episode;
});
var translates = Object.assign(t[0], {
translate_url: t[0].translate,
translate: t
.map(function(episode) {
return episode.translate;
})
.join(' ')
});
results.push(translates);
});
results.sort(function(a, b) {
return (
sorting.indexOf(a.kinopoisk_id) - sorting.indexOf(b.kinopoisk_id)
);
});
if (!results.length) return callback();
var query_id = results
.map(function(result) {
return result.kinopoisk_id;
})
.join('|');
CP_get.movies(
{
query_id: query_id
},
results.length,
'',
1,
true,
function(err, ms) {
if (err) {
console.log('[CP_get.movies]', query_id, err);
return callback(null);
}
if (!ms || !ms.length) {
console.log('[CP_get.movies]', query_id, 'NOT SERIES');
return callback(null);
}
var episodes = sortingIds(results, ms).map(function(episode) {
episode.url =
episode.url +
'/s' +
episode.season +
'e' +
episode.episode +
(episode.translate
? '_' +
CP_translit.text(
episode.translate_url,
undefined,
'translate'
)
: '');
episode.pathname =
episode.pathname +
'/s' +
episode.season +
'e' +
episode.episode +
(episode.translate
? '_' +
CP_translit.text(
episode.translate_url,
undefined,
'translate'
)
: '');
episode.year2 =
episode.season +
' ' +
config.l.season +
' ' +
episode.episode +
' ' +
config.l.episode;
episode.year3 = 'S' + episode.season + 'E' + episode.episode;
return episode;
});
fs.writeFileSync(
path.join(
path.dirname(__filename),
'..',
'files',
'episodes.json'
),
JSON.stringify(episodes, null, 2)
);
callback();
}
2019-10-01 21:34:02 +02:00
);
}
);
},
function() {
active.num--;
active.process.episode = false;
2019-10-01 21:34:02 +02:00
}
);
2019-11-16 02:59:07 +01:00
} else {
console.log(
'NOT START:',
'EPISODE',
2019-11-16 02:59:07 +01:00
'status',
!!modules.episode.status,
'custom',
modules.episode.data.index.custom.length
2019-11-16 02:59:07 +01:00
);
2019-10-01 21:34:02 +02:00
}
/**
* Publish new movies.
*/
2020-01-20 03:29:03 +01:00
if (
config.publish.every.hours &&
config.publish.every.movies &&
!(hour % config.publish.every.hours)
) {
2019-10-01 21:34:02 +02:00
active.num++;
active.process.publish = true;
CP_get.publishIds(function(err, ids) {
2019-11-16 04:02:42 +01:00
var log = '';
2019-10-01 21:34:02 +02:00
if (!ids) {
2019-11-16 04:02:42 +01:00
log = '[publish] Not Movies.';
2019-10-01 21:34:02 +02:00
config.publish.every.hours = 0;
config.publish.every.movies = 0;
} else if (
ids.start_id === config.publish.start &&
ids.stop_id === config.publish.stop
) {
2019-11-16 04:02:42 +01:00
log = '[publish] All movies published.';
2019-10-01 21:34:02 +02:00
config.publish.every.hours = 0;
config.publish.every.movies = 0;
} else {
2019-11-16 04:02:42 +01:00
log = '[publish] New IDs: ' + ids.start_id + ' - ' + ids.stop_id;
2019-10-01 21:34:02 +02:00
config.publish.start = ids.start_id;
config.publish.stop = ids.stop_id;
}
if (modules.rewrite.status && modules.rewrite.data.token) {
2019-11-16 04:02:42 +01:00
active.num++;
active.process.rewrite = true;
2019-10-01 21:34:02 +02:00
CP_get.movies(
{ query_id: ids.soon_id.join('|') },
ids.soon_id.length,
'',
1,
false,
function(err, movies) {
if (err) {
active.num--;
active.process.rewrite = '[CP_get.movies] ERR';
return console.log('[CP_get.movies]', err);
}
if (movies && movies.length) {
movies = movies.filter(function(m) {
return m.description && m.description.length >= 100;
});
async.eachOfLimit(
movies,
1,
function(movie, i, callback) {
sinoni({
token: modules.rewrite.data.token,
double: modules.rewrite.data.double,
unique: modules.rewrite.data.unique,
text: movie.description,
lang: config.language
})
.then(function(res) {
if (typeof res.percent !== 'undefined') {
var custom = {};
if (movie.custom) {
custom = JSON.parse(movie.custom);
} else {
custom.unique = true;
}
custom.rewrite = {};
if (typeof res.percent !== 'undefined') {
custom.rewrite.percent = res.percent;
}
if (typeof res.words !== 'undefined') {
custom.rewrite.words = res.words;
}
if (typeof res.spam !== 'undefined') {
custom.rewrite.spam = res.spam;
}
if (typeof res.water !== 'undefined') {
custom.rewrite.water = res.water;
}
movie.custom = JSON.stringify(custom);
}
if (typeof res.rewrite !== 'undefined') {
movie.description = res.rewrite;
CP_save.save(movie, 'rt', function(err) {
callback(err);
});
} else {
callback();
}
})
.catch(function(err) {
callback(err);
});
},
function(err) {
if (err) console.log('[sinoni]', err);
active.num--;
active.process.rewrite = false;
}
);
} else {
active.num--;
active.process.rewrite = 'Movies 0';
}
}
);
} else {
2019-11-16 04:02:42 +01:00
console.log(
'NOT START:',
'REWRITE',
'status',
!!modules.rewrite.status,
'token',
!!modules.rewrite.data.token
);
2019-10-01 21:34:02 +02:00
}
2019-11-16 04:02:42 +01:00
CP_save.save(config, 'config', function(err) {
if (err) {
console.log('[CP_save.save]', err);
} else {
console.log(log);
}
active.num--;
active.process.publish = false;
});
2019-10-01 21:34:02 +02:00
});
2019-11-16 02:59:07 +01:00
} else {
console.log(
'NOT START:',
'PUBLISH',
'hours',
!!config.publish.every.hours,
'movies',
!!config.publish.every.movies
);
2019-10-01 21:34:02 +02:00
}
2019-10-09 03:51:57 +02:00
/**
* Create random subdomain.
*/
2019-11-16 02:59:07 +01:00
if (config.random && hour === 4) {
2019-10-09 03:51:57 +02:00
active.num++;
active.process.random = true;
2019-10-22 03:51:18 +02:00
config.subdomain = dayToLetter();
2019-10-09 03:51:57 +02:00
CP_save.save(config, 'config', function(err) {
if (err) console.log('[CP_save.save]', err);
active.num--;
active.process.random = false;
});
2019-11-16 02:59:07 +01:00
} else {
console.log(
'NOT START:',
'RANDOM',
'config',
!!config.random,
'hour',
'4 !=',
hour
);
2019-10-09 03:51:57 +02:00
}
2019-10-01 21:34:02 +02:00
/**
* Delete abuse movies.
*/
if (
modules.abuse.data.imap.user &&
modules.abuse.data.imap.password &&
modules.abuse.data.imap.host &&
2020-01-24 03:05:46 +01:00
hour === 3
2019-10-01 21:34:02 +02:00
) {
active.num++;
active.process.imap = true;
2019-11-05 23:41:20 +01:00
var options_imap = JSON.stringify(modules.abuse.data.imap);
options_imap = JSON.parse(options_imap);
options_imap.tls = options_imap.tls !== 0;
delete options_imap.from;
delete options_imap.subdomain;
var options_smtp = JSON.stringify(modules.abuse.data.smtp);
options_smtp = JSON.parse(options_smtp);
options_smtp.secure = options_smtp.secure !== 0;
if (options_smtp.dkim) {
options_smtp.dkim = JSON.parse(options_smtp.dkim);
} else {
delete options_smtp.dkim;
}
var message = options_smtp.message;
delete options_smtp.message;
2019-10-01 21:34:02 +02:00
2019-10-22 04:35:15 +02:00
var date = new Date();
2019-11-14 02:34:24 +01:00
var search = [
2020-01-24 03:05:46 +01:00
[
'or',
['SINCE', formatDate(new Date(date.setDate(date.getDate() - 1)))],
['UNSEEN']
]
2019-11-14 02:34:24 +01:00
];
2019-10-22 04:35:15 +02:00
if (modules.abuse.data.imap.from && modules.abuse.data.imap.from.length) {
if (modules.abuse.data.imap.from.length === 1) {
modules.abuse.data.imap.from.forEach(function(email) {
search.push(['FROM', email.trim()]);
});
} else if (modules.abuse.data.imap.from.length > 1) {
2019-11-14 02:34:24 +01:00
var search_or = [];
search_or.push('or');
modules.abuse.data.imap.from.forEach(function(email) {
search_or.push(['FROM', email.trim()]);
2019-11-14 02:34:24 +01:00
});
search.push(search_or);
} else {
search.push(['ALL']);
2019-11-14 02:34:24 +01:00
}
2019-11-14 01:04:54 +01:00
}
2019-10-22 04:35:15 +02:00
2019-11-05 23:41:20 +01:00
var headers_ = [];
var transporter =
options_smtp && options_smtp.host
? nodemailer.createTransport(options_smtp)
: null;
var imap = new Imap(options_imap);
2019-10-01 21:34:02 +02:00
imap.once('ready', function() {
2019-11-13 01:12:07 +01:00
imap.openBox('INBOX', false, function(err) {
2019-11-05 23:41:20 +01:00
if (err) {
console.error('imap.openBox', err);
return imap.end();
}
2019-10-22 04:35:15 +02:00
imap.search(search, function(err, results) {
2019-11-05 23:41:20 +01:00
if (err || !results || !results.length) {
2019-11-14 01:04:54 +01:00
console.log('imap.search', search, err || results);
2019-11-05 23:41:20 +01:00
return imap.end();
}
var f = imap.fetch(results, { bodies: '', markSeen: true });
2019-10-22 04:35:15 +02:00
f.on('message', function(msg) {
var parser = new MP();
2019-11-14 23:52:18 +01:00
parser.on('end', function(data) {
var to =
data['from'] && data['from'][0] && data['from'][0]['address']
? data['from'][0]['address']
: '';
var subject = data['subject'] ? data['subject'] : '';
var text = data['text']
? data['text']
: data['html']
? data['html']
.replace(/<blockquote>[^]*<\/blockquote>/gi, '')
.replace(/<q>[^]*<\/q>/gi, '')
: '';
var html = data['html']
2019-11-14 23:52:18 +01:00
? data['html']
.replace(/<blockquote>[^]*?<\/blockquote>/gi, '')
.replace(/<q>[^]*?<\/q>/gi, '')
2019-11-14 23:52:18 +01:00
: data['text']
2019-12-03 02:06:09 +01:00
? data['text']
: '';
if (to && subject && (text || html)) {
2019-11-14 23:52:18 +01:00
headers_.push({
to: to,
subject: subject,
text: text,
html: html
2019-11-14 23:52:18 +01:00
});
2019-10-22 04:35:15 +02:00
}
});
msg.on('body', function(stream) {
stream.on('data', function(chunk) {
2019-11-05 23:41:20 +01:00
parser.write(td.decode(chunk));
2019-10-01 21:34:02 +02:00
});
2019-10-22 04:35:15 +02:00
});
msg.once('end', function() {
parser.end();
});
});
f.once('error', function(err) {
2019-11-05 23:41:20 +01:00
console.error('f.once', err);
2019-10-22 04:35:15 +02:00
});
f.once('end', function() {
imap.end();
});
});
2019-10-01 21:34:02 +02:00
});
});
imap.once('error', function(err) {
2019-11-05 23:41:20 +01:00
console.error('imap.once', err);
2019-10-01 21:34:02 +02:00
active.num--;
active.process.imap = false;
});
imap.once('end', function() {
2019-11-14 23:52:18 +01:00
if (headers_ && headers_.length) {
2019-11-05 23:41:20 +01:00
var save = false;
async.eachOfLimit(
headers_,
1,
2019-11-14 23:52:18 +01:00
function(d, i, callback) {
2019-11-05 23:41:20 +01:00
var re = new RegExp(
config.domain +
2020-01-13 23:36:23 +01:00
'(\\/mobile-version|\\/tv-version|\\/app-version|)\\/[a-z0-9._-]*?' +
2019-11-05 23:41:20 +01:00
(config.urls.slash === '/' ? '\\/' : '-') +
2019-12-05 02:14:37 +01:00
'[a-z0-9._-]*',
2019-11-05 23:41:20 +01:00
'ig'
);
var urls = headers_[i].html
2019-12-05 02:14:37 +01:00
.replace(/<br>/gi, '')
.replace(/\n/gi, '')
.replace(/\r\n/gi, '')
.replace(/\r/gi, '')
.match(re);
if (!urls) {
2019-11-14 23:52:18 +01:00
console.log('NOT ID', headers_[i]);
return callback();
} else {
2019-11-14 23:52:18 +01:00
var unique = urls.filter(function(v, i, a) {
return a.indexOf(v) === i;
});
console.log('URLS', unique);
}
2019-11-05 23:41:20 +01:00
var send = false;
2019-11-29 22:56:55 +01:00
headers_[i].id = [];
2019-11-14 23:52:18 +01:00
unique.forEach(function(u) {
2019-11-05 23:41:20 +01:00
var id = movie.id(u);
if (id >= 1 && id <= 99999999) {
2019-11-05 23:41:20 +01:00
send = !!transporter;
if (!(headers_[i].id.indexOf('' + id) + 1)) {
headers_[i].id.push(id.toString());
}
if (modules.abuse.data.movies.indexOf('' + id) + 1) {
console.log('ABUSE ONLY EMAIL', headers_[i].to, id, send);
} else {
console.log('ABUSE', headers_[i].to, id, send);
modules.abuse.data.movies.unshift('' + id);
save = true;
}
2019-11-05 23:41:20 +01:00
}
});
console.log('SEND', send, headers_[i].id);
2019-11-05 23:41:20 +01:00
if (send) {
send = false;
var dateNow = new Date();
2019-12-03 02:06:09 +01:00
var dateString =
('0' + dateNow.getDate()).slice(-2) +
2019-12-03 02:06:09 +01:00
'-' +
('0' + (dateNow.getMonth() + 1)).slice(-2) +
2019-12-03 02:06:09 +01:00
'-' +
dateNow.getFullYear() +
2019-12-03 02:06:09 +01:00
' ' +
('0' + dateNow.getHours()).slice(-2) +
2019-12-03 02:06:09 +01:00
':' +
('0' + dateNow.getMinutes()).slice(-2);
var mailMessage =
2019-12-03 02:06:09 +01:00
message && message.indexOf('[id]') + 1
? message.replace(/\[id]/gi, headers_[i].id.join(','))
: message;
2019-11-15 04:08:38 +01:00
var mailOptions = {
name: options_smtp.auth.user.split('@')[1],
2019-11-15 04:08:38 +01:00
from: options_smtp.auth.user,
to: headers_[i].to,
textEncoding: 'base64',
date: dateNow,
2019-11-15 04:08:38 +01:00
subject: 'RE: ' + headers_[i].subject,
text: (
mailMessage
.replace(/<br>/gi, '\n')
.replace(/(<([^>]+)>)/gi, '') +
'\n\n' +
'---- ' +
dateString +
' <' +
headers_[i].to +
'> ----\n' +
headers_[i].text
.replace(/<br>/gi, '\n')
.replace(/(<([^>]+)>)/gi, '')
).replace(/(^\s*)|(\s*)$/g, ''),
html: (
mailMessage +
2019-11-29 22:56:55 +01:00
'<br><br>' +
2019-12-03 02:06:09 +01:00
'---- ' +
dateString +
' <' +
headers_[i].to +
'> ----<br>' +
2019-11-29 00:02:25 +01:00
'<blockquote>' +
headers_[i].html.replace(/\n/g, '<br>') +
2019-11-15 17:17:51 +01:00
'</blockquote>'
)
.replace(/\s+/g, ' ')
.replace(/(^\s*)|(\s*)$/g, '')
2019-11-15 04:08:38 +01:00
};
2019-11-27 00:06:45 +01:00
setTimeout(function() {
console.log(
'START sendMail',
headers_[i].to,
headers_[i].subject
);
2019-11-27 00:06:45 +01:00
transporter.sendMail(mailOptions, function(err) {
if (err) console.error('sendMail', err);
console.log(
'REPLY',
headers_[i].to,
'RE: ' + headers_[i].subject
);
2019-12-03 02:06:09 +01:00
if (
!err &&
modules.abuse.data.smtp.host.indexOf('yandex') + 1
) {
2019-11-27 00:06:45 +01:00
var imap2 = new Imap(options_imap);
imap2.once('ready', function() {
imap2.openBox('Sent', false, function(err, box) {
if (err) {
console.error('openBox Sent', err);
imap2.end();
callback();
}
let msg, htmlEntity;
msg = mimemessage.factory({
contentType: 'multipart/alternate',
body: []
});
htmlEntity = mimemessage.factory({
contentType: 'text/html;charset=utf-8',
body: mailOptions.html
});
msg.header(
'Message-ID',
'<' +
2019-12-03 02:06:09 +01:00
Math.random()
.toString(36)
.substr(2, 5) +
'>'
2019-11-27 00:06:45 +01:00
);
msg.header('From', mailOptions.from);
msg.header('To', mailOptions.to);
msg.header('Subject', mailOptions.subject);
msg.body.push(htmlEntity);
imap2.append(
msg.toString(),
{
mailbox: 'Sent',
flags: ['Seen']
2019-11-27 00:06:45 +01:00
},
function(err) {
if (err) {
console.error('APPEND', err);
}
imap2.end();
2019-11-15 04:08:38 +01:00
}
2019-11-27 00:06:45 +01:00
);
});
2019-11-15 04:08:38 +01:00
});
2019-11-27 00:06:45 +01:00
imap2.once('error', function(err) {
console.error('SENT FOLDER', err);
callback();
});
imap2.once('end', function() {
console.error('SENT', mailOptions.to, mailOptions.subject);
callback();
});
imap2.connect();
2019-12-06 22:11:53 +01:00
} else {
callback();
2019-11-27 00:06:45 +01:00
}
});
2020-01-13 23:36:23 +01:00
}, 180000 * i);
console.log('TIMEOUT', headers_[i].subject, 180000 * i, 'sec');
2019-11-05 23:41:20 +01:00
} else {
callback();
}
},
function(err) {
if (err) console.error(err);
if (save) {
CP_save.save(modules, 'modules', function(err) {
if (err) console.log('[CP_save.save]', err);
if (modules.abuse.data.imap.subdomain) {
config.subdomain = dayToLetter();
CP_save.save(config, 'config', function(err) {
if (err) console.log('[CP_save.save]', err);
active.num--;
active.process.imap = false;
});
} else {
active.num--;
active.process.imap = false;
}
});
} else {
2019-10-22 03:51:18 +02:00
active.num--;
active.process.imap = false;
2019-11-05 23:41:20 +01:00
}
2019-10-22 03:51:18 +02:00
}
2019-11-05 23:41:20 +01:00
);
2019-10-01 21:34:02 +02:00
} else {
active.num--;
active.process.imap = false;
2019-11-14 23:52:18 +01:00
console.log('headers:', headers_.length, 'data:', headers_.length);
2019-10-01 21:34:02 +02:00
}
});
imap.connect();
2020-01-24 03:05:46 +01:00
function formatDate(date) {
var monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return monthNames[monthIndex] + ' ' + day + ', ' + ' ' + year;
}
2019-11-16 02:59:07 +01:00
} else {
console.log(
'NOT START:',
'IMAP',
'user',
!!modules.abuse.data.imap.user,
'password',
!!modules.abuse.data.imap.password,
'host',
!!modules.abuse.data.imap.host,
'3 !=',
2019-11-16 02:59:07 +01:00
hour
);
2019-10-01 21:34:02 +02:00
}
2020-02-24 00:05:10 +01:00
/**
* Sitemap generator.
*/
if (force || !(hour % 11)) {
active.num++;
active.process.sitemap = true;
var years = [];
for (var i = 1890; i <= new Date(timeZone).getFullYear() + 10; i++) {
years.push('' + i);
}
var start = new Date();
2020-02-24 00:05:10 +01:00
async.eachOfLimit(
years,
1,
function(year, key, callback) {
CP_get.movies({ year: year }, -1, 'kinopoisk-vote-up', 1, true, function(
err,
movies
) {
if (err) return callback(err);
var render = {};
render.urls = [];
if (movies && movies.length) {
for (var i = 0; i < movies.length; i++) {
if (
!config.urls.noindex ||
!(
movies[i].url.indexOf(
'/' + config.urls.noindex + config.urls.slash
) + 1
)
) {
render.urls[render.urls.length] = {
loc:
config.protocol +
(config.botdomain || config.subdomain) +
config.domain +
movies[i].pathname,
2020-02-24 00:05:10 +01:00
lastmod:
movies[i].custom && movies[i].custom.lastmod
? movies[i].custom.lastmod.substr(0, 10)
: ''
};
}
}
if (new Date() - start < 2000) {
start = new Date();
callback(null, render);
} else {
console.log('Large query', new Date() - start, 'ms');
setTimeout(function() {
start = new Date();
callback(null, render);
}, 10000);
}
2020-02-24 00:05:10 +01:00
} else {
return callback(null, render);
}
var dir = path.join(
__dirname,
'..',
'themes',
config.theme,
'views',
config.urls.sitemap
);
fs.mkdirSync(dir, { recursive: true });
fs.writeFile(
path.join(dir, 'year-' + year + '.json'),
JSON.stringify(render, null, 2),
function(err) {
if (err) console.error('Write File Error:', err);
}
);
});
},
function() {
active.num--;
active.process.sitemap = false;
}
);
} else {
console.log('NOT START:', 'SITEMAP', 'hour', '11 %', hour);
}
/**
* Sitemap generator.
*/
if (force || !(hour % 23)) {
active.num++;
active.process.categories = true;
var categories = ['year', 'genre', 'country', 'actor', 'director'];
async.eachOfLimit(
categories,
1,
function(category, key, callback) {
var query = {};
query[category] = '!_empty';
CP_get.movies(query, -2, 'kinopoisk-vote-up', 1, false, function(
err,
movies
) {
if (err) return callback(err);
if (movies && movies.length && category === 'year') {
movies.push({
year: new Date().getFullYear() + ''
});
}
var render = CP_structure.categories(category, movies);
callback(null, render);
var file = path.join(__dirname, '..', 'files', category + '.json');
fs.writeFile(file, JSON.stringify(render, null, 2), function(err) {
if (err) console.error('Write File Error:', err);
});
});
},
function() {
active.num--;
active.process.categories = false;
}
);
} else {
console.log('NOT START:', 'CATEGORIES', 'hour', '23 %', hour);
}
2019-10-01 21:34:02 +02:00
/**
* Save data.
2019-10-01 21:34:02 +02:00
*
* @param {String} collection
* @param {Object} movies
2019-10-01 21:34:02 +02:00
* @param {Callback} callback
*/
function saveData(collection, movies, callback) {
CP_get.contents({ content_url: collection }, 1, 1, false, function(
2019-10-01 21:34:02 +02:00
err,
contents
) {
if (err) {
console.log('[CP_get.contents]', collection, err);
2019-10-01 21:34:02 +02:00
return callback(null);
}
if (contents && contents.length && contents[0].id) {
var content = contents[0];
var query_id = movies
.map(function(id) {
return id.kinopoisk_id;
})
.join('|');
2019-10-01 21:34:02 +02:00
CP_get.movies(
{
query_id: query_id
2019-10-01 21:34:02 +02:00
},
movies.length,
2019-10-01 21:34:02 +02:00
'',
1,
false,
function(err, ms) {
2019-10-01 21:34:02 +02:00
if (err) {
console.log('[CP_get.movies]', collection, err);
2019-10-01 21:34:02 +02:00
return callback(null);
}
if (ms && ms.length) {
movies = sortingIds(movies, ms);
if (!movies.length) return callback(null);
2020-02-24 00:05:10 +01:00
var content_movies = content.content_movies
.replace(/^,*|,*$/, '')
.split(',');
movies.reverse().forEach(function(m) {
if (!m.kp_id) return;
return content_movies.unshift('' + m.kp_id);
2019-10-01 21:34:02 +02:00
});
content.content_movies = content_movies
.filter(function(value, index, self) {
return self.indexOf(value) === index;
2019-10-01 21:34:02 +02:00
})
.slice(0, 200)
2019-10-01 21:34:02 +02:00
.join(',');
CP_save.save(content, 'content', function(err, result) {
2019-10-01 21:34:02 +02:00
console.log(
'[CP_save.save]',
collection,
content.content_movies,
2019-10-01 21:34:02 +02:00
err,
result
);
2020-02-24 00:39:45 +01:00
CP_cache.flush(function(err) {
if (err) {
if ((err + '').indexOf('not available') === -1) {
console.error(err);
2019-10-01 21:34:02 +02:00
}
}
2020-02-24 00:39:45 +01:00
async.eachOfLimit(
movies,
1,
function(movie, key, callback) {
if (!movie.translate && !movie.quality)
return callback(null);
var rt = {};
rt.id = movie.kp_id;
rt.kp_id = movie.kp_id;
rt.translate = movie.translate;
rt.quality = movie.quality.toUpperCase();
rt.duplicate = true;
try {
var c = movie.custom ? JSON.parse(movie.custom) : {};
c.unique = c.unique ? c.unique : false;
rt.custom = JSON.stringify(c);
} catch (e) {
rt.custom = JSON.stringify({ unique: false });
}
CP_save.save(rt, 'rt', function() {
return callback(null);
});
},
function() {
return callback(null);
}
);
});
2019-10-01 21:34:02 +02:00
});
} else {
console.log('[movies]', collection, err, movies);
2019-10-01 21:34:02 +02:00
return callback(null);
}
}
);
} else {
console.log('[contents]', collection, err, contents);
2019-10-01 21:34:02 +02:00
callback(null);
}
});
}
/**
* Valid JSON.
*
* @param {String} jsonString
*/
function tryParseJSON(jsonString) {
try {
var o = JSON.parse(jsonString);
if (o && typeof o === 'object') {
return o;
}
} catch (e) {}
return {};
}
/**
* Sort films are turned by id list.
*
* @param {Object} updates
2019-10-01 21:34:02 +02:00
* @param {Object} movies
* @return {Array}
*/
function sortingIds(updates, movies) {
2019-10-01 21:34:02 +02:00
var result = [];
for (var u = 0; u < updates.length; u++) {
var kp1 = parseInt(('' + updates[u].kinopoisk_id).replace(/[^0-9]/g, ''));
for (var m = 0; m < movies.length; m++) {
var kp2 = parseInt(movies[m].kp_id);
if (kp1 === kp2) {
movies[m].quality = updates[u].quality ? updates[u].quality : '';
movies[m].translate = updates[u].translate ? updates[u].translate : '';
if (updates[u].season) movies[m].season = updates[u].season;
if (updates[u].episode) movies[m].episode = updates[u].episode;
if (updates[u].translate_url)
movies[m].translate_url = updates[u].translate_url;
result.push(movies[m]);
2019-10-01 21:34:02 +02:00
}
}
}
return result;
}
2019-10-22 03:51:18 +02:00
/**
* Current day to letter.
*/
function dayToLetter() {
var now = new Date();
var year = now.getFullYear();
var start = new Date(year, 0, 0);
var diff = now - start;
var oneDay = 86400000;
var day = ('00' + Math.floor(diff / oneDay)).slice(-3);
var letter1 = ['a', 'e', 'i', 'o', 'u'];
2019-12-09 03:07:33 +01:00
var letter2 = ['b', 'c', 'd', 'p', 's', 'w', 'v', 'n', 'l', 'm'];
2019-12-08 22:26:32 +01:00
if (config.domain.split('.')[0].length % 2) {
letter1 = letter1.reverse();
}
if (config.domain.split('.')[1].length % 2) {
letter2 = letter2.reverse();
}
2019-12-09 01:08:45 +01:00
var result =
2019-10-22 03:51:18 +02:00
letter2[parseInt(day[0])] +
letter1[parseInt(day[1]) % 5] +
letter2[parseInt(day[2])] +
2019-12-09 03:07:33 +01:00
(config.random === 3 ? '' : letter1[year % 5]);
if (parseInt(day[1]) >= 5) {
result =
letter1[parseInt(day[0]) % 5] +
letter2[parseInt(day[1])] +
letter1[parseInt(day[2]) % 5] +
(config.random === 3 ? '' : letter2[year % 10]);
}
2019-12-09 01:08:45 +01:00
if (now.getDate() % 2) {
result = result
.split('')
.reverse()
.join('');
}
2019-12-09 03:07:33 +01:00
return result + '.';
2019-10-22 03:51:18 +02:00
}
2019-10-01 21:34:02 +02:00
/**
* Check active process.
*/
2019-12-06 22:11:53 +01:00
var interval = 0;
var sint = setInterval(function() {
if (!(interval % 10)) {
2019-10-01 21:34:02 +02:00
console.log(active.num, active.process);
}
if (active.num <= 0) {
2019-12-06 22:11:53 +01:00
clearInterval(sint);
console.log(active.num, active.process);
2019-10-01 21:34:02 +02:00
if (Object.keys(active.process).length === 0) return;
var rand = Math.floor(Math.random() * 10) + 10;
console.log('Reload after', rand * 1000, 'sec');
setTimeout(function() {
exec('pm2 reload ' + config.domain, function(err, stdout, stderr) {
if (err || stderr) console.error(err || stderr);
console.log('Flush memcached');
CP_cache.flush(function(err) {
2020-01-13 00:57:33 +01:00
if (err) {
if ((err + '').indexOf('not available') === -1) {
console.error(err);
}
}
2019-10-22 02:48:07 +02:00
return process.exit(0);
2019-10-01 21:34:02 +02:00
});
});
}, rand * 1000);
}
2019-12-06 22:11:53 +01:00
interval++;
2019-10-01 21:34:02 +02:00
}, 1000);