-
Notifications
You must be signed in to change notification settings - Fork 1
/
kprate_cron.php
141 lines (118 loc) · 4.46 KB
/
kprate_cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', true);
ini_set('html_errors', false);
set_time_limit(0);
define('DATALIFEENGINE', true);
define('ROOT_DIR', __DIR__);
define('ENGINE_DIR', ROOT_DIR . '/engine');
if (file_exists(ENGINE_DIR . '/classes/plugins.class.php')) {
include_once ENGINE_DIR . '/classes/plugins.class.php';
} else {
@ini_set('pcre.recursion_limit', 10000000 );
@ini_set('pcre.backtrack_limit', 10000000 );
@ini_set('pcre.jit', false);
@include_once (ENGINE_DIR . '/data/config.php');
require_once (ENGINE_DIR . '/classes/mysql.php');
require_once (ENGINE_DIR . '/data/dbconfig.php');
abstract class DLEPlugins {
public static function Check($source = '') {
return $source;
}
}
}
date_default_timezone_set($config['date_adjust']);
include_once (DLEPlugins::Check(ROOT_DIR . '/language/' . $config['langs'] . '/website.lng'));
setlocale(LC_NUMERIC, "C");
require_once (DLEPlugins::Check(ENGINE_DIR . '/modules/functions.php'));
if (!isset($argv)) {
echo 'php -f ' . __DIR__ . $_SERVER['SCRIPT_NAME'] . ' > ' . ROOT_DIR . '/engine/data/kprate.log 2>&1 &';
die();
}
/* В правой части указать имя соответствующего доп.поля */
$fields = [
'kinopoisk_id' => 'kinopoisk_id', //ID кинопоиска
'kp_rate' => 'kp_rate', //рейтинг кинопоиска
'kp_votes' => 'kp_votes', //количество голосов кинопоиска
'kp_rate_vote' => '', //сборная строка вида: "7.87 (4568)"
'imdb_rate' => 'imdb_rate', //рейтинг imdb
'imdb_votes' => 'imdb_votes', //количество голосов imdb
'imdb_rate_vote'=> '', //сборная строка вида: "8.765 (56874)"
];
function xfieldsdatasave(array $xfields): string
{
global $db;
$filecontents = [];
foreach ($xfields as $xfielddataname => $xfielddatavalue) {
if ($xfielddatavalue === '') continue;
$xfielddataname = str_replace( "|", "|", $xfielddataname);
$xfielddataname = str_replace( "\r\n", "__NEWL__", $xfielddataname);
$xfielddatavalue = str_replace( "|", "|", $xfielddatavalue);
$xfielddatavalue = str_replace( "\r\n", "__NEWL__", $xfielddatavalue);
$filecontents[] = "$xfielddataname|$xfielddatavalue";
}
$filecontents = $db->safesql(join('||', $filecontents ));
return $filecontents;
}
function getContent(int $kinopoisk_id): string
{
$ch = curl_init('https://rating.kinopoisk.ru/' . $kinopoisk_id . '.xml');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
]);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function getRatings(int $kinopoisk_id): array
{
$ratings = [
'kp_rate' => '',
'kp_votes' => '',
'imdb_rate' => '',
'imdb_votes' => '',
];
$xml_data = getContent($kinopoisk_id);
if (preg_match("#<kp_rating num_vote=\"(\d+)\">(.+?)</kp_rating>#i", $xml_data, $data)) {
if (($rate = (float)$data[2]) > 0) {
$ratings['kp_rate'] = $rate;
$ratings['kp_votes'] = (int)$data[1];
}
}
if (preg_match("#<imdb_rating num_vote=\"(\d+)\">(.+?)</imdb_rating>#i", $xml_data, $data)) {
if (($rate = (float)$data[2]) > 0) {
$ratings['imdb_rate'] = $rate;
$ratings['imdb_votes'] = (int)$data[1];
}
}
return $ratings;
}
$sql = $db->query(sprintf('SELECT id, xfields FROM %s_post', PREFIX));
while ($row = $db->get_row($sql)) {
$xfields = xfieldsdataload(stripslashes($row['xfields']));
if ($kinopoisk_id = (int)$xfields[$fields['kinopoisk_id']]) {
$ratings = getRatings($kinopoisk_id);
//$ratings['kp_rate'] = round($ratings['kp_rate'], 2); //округлять рейтинг до 2го знака после запятой
//$ratings['imdb_rate'] = round($ratings['imdb_rate'], 2);
foreach ($ratings as $k => $v) {
$fields[$k] && $xfields[$fields[$k]] = $v;
}
if ($fields['kp_rate_vote'] && $ratings['kp_votes']) {
$xfields[$fields['kp_rate_vote']] = sprintf('%s (%d)', $ratings['kp_rate'], $ratings['kp_votes']);
}
if ($fields['imdb_rate_vote'] && $ratings['imdb_votes']) {
$xfields[$fields['imdb_rate_vote']] = sprintf('%s (%d)', $ratings['imdb_rate'], $ratings['imdb_votes']);
}
$filecontents = xfieldsdatasave($xfields);
$db->query(sprintf('UPDATE %s_post SET xfields="%s" WHERE id = %d', PREFIX, $filecontents, $row['id']));
$db->close();
echo $row['id'] . ' - ' . json_encode($ratings) . PHP_EOL;
usleep(500000);
}
}
echo 'Готово';