-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
128 lines (119 loc) · 4.56 KB
/
test.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
<?php
require 'autoload.php';
function saveOrUpdateTiebaHotData(Connect $c, string $tieba, string $text, bool $isNew)
{
$tb = addslashes($tieba);
$h = addslashes($text);
$c->send("SELECT `id` FROM `hots` WHERE `tieba_name`=\"$tb\" AND `hot`=\"$h\"");
$exis = $c->recv();
$now = date('Y-m-d H:i:s');
$rank = $isNew ? 1 : 0;
if (empty($exis)) {
$c->send("INSERT INTO `hots`(`rank`,`tieba_name`,`hot`,`create_time`,`update_time`) VALUES ($rank,\"$tb\",\"$h\",\"$now\",\"$now\")");
} else {
$c->send("UPDATE `hots` SET `version`=`version`+1,`update_time`=\"$now\",`rank`=`rank`+$rank WHERE `id`={$exis[0]['id']}");
}
}
function saveOrUpdateUserData(Connect $c, string $text, bool $isNew)
{
$name = addslashes($text);
$c->send("SELECT `id` FROM `users` WHERE `user_name`=\"$name\"");
$exis = $c->recv();
$now = date('Y-m-d H:i:s');
$rank = $isNew ? 1 : 0;
if (empty($exis)) {
$c->send("INSERT INTO `users`(`rank`,`user_name`,`create_time`,`update_time`) VALUES ($rank,\"$name\",\"$now\",\"$now\")");
} else {
$c->send("UPDATE `users` SET `version`=`version`+1,`update_time`=\"$now\",`rank`=`rank`+$rank WHERE `id`={$exis[0]['id']}");
}
}
function saveTiezi(Connect $c, string $tiezi, string $tieba)
{
$content = addslashes($tiezi);
$name = addslashes($tieba);
$c->send("SELECT `id` FROM `posts` WHERE `tieba_name`=\"$name\" AND `post`=\"$content\"");
$exis = $c->recv();
$now = date('Y-m-d H:i:s');
if (empty($exis)) {
$c->send("INSERT INTO `posts`(`create_time`,`update_time`,`tieba_name`,`post`) VALUES (\"$now\",\"$now\",\"$name\",\"$content\")");
} else {
$c->send("UPDATE `posts` SET `version`=`version`+1,`update_time`=\"$now\" WHERE `id`={$exis[0]['id']}");
}
}
function compareForLastTiebaHotDataAndFindNew(string $tiebaId, array $hot): array
{
if (empty($hot)) {
return [];
}
$file = __DIR__ . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR . 'tieba_' . $tiebaId . '.json';
if (!is_file($file)) {
file_put_contents($file, json_encode($hot));
return [];
}
$last = json_decode(file_get_contents($file), true);
$result = [];
foreach ($hot as $posting) {
if (!in_array($posting, $last)) {
$result[] = $posting;
}
}
file_put_contents($file, json_encode($hot));
return $result;
}
function compareForLastAuthorsAndFindNew(array $authors): array
{
if (empty($authors)) {
return [];
}
$file = __DIR__ . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR . 'users.json';
if (!is_file($file)) {
file_put_contents($file, json_encode($authors));
return [];
}
$last = json_decode(file_get_contents($file), true);
$result = [];
foreach ($authors as $author) {
if (!in_array($author, $last)) {
$result[] = $author;
}
}
file_put_contents($file, json_encode($authors));
return $result;
}
if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'config.php')) {
$config = include __DIR__ . DIRECTORY_SEPARATOR . 'config.php';
} else {
$config = include __DIR__ . DIRECTORY_SEPARATOR . 'config.example.php';
}
$x = Connect::getInstance(
$config['mysqlAddress'],
$config['user'],
$config['port'],
$config['password'],
$config['database']
);
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'test.log', date('Y-m-d H:i:s') . ' 开始' . PHP_EOL, FILE_APPEND);
$x->send(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'database.sql'));
$cookie = '';
$name = $config['tieba'];
$s = new Tieba($name, $cookie);
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'test.log', date('Y-m-d H:i:s') . ' 解析数据' . PHP_EOL, FILE_APPEND);
$posts = $s->getAllText();
foreach ($s->getTiebaHot() as $k => $v_arr) {
/* $k 对应第几个贴吧,$v_arr 贴吧的热铁数组 */
$newHotPosting = compareForLastTiebaHotDataAndFindNew($k, $v_arr);
foreach ($v_arr as $kk => $v) {
saveOrUpdateTiebaHotData($x, $name[$k], $v, in_array($v, $newHotPosting));
}
foreach ($posts[$k] as $tiezi) {
saveTiezi($x, $tiezi, $name[$k]);
}
}
$allAuthors = $s->getAuthors();
$newAuthors = compareForLastAuthorsAndFindNew($allAuthors);
foreach ($allAuthors as $k => $v) {
saveOrUpdateUserData($x, $v, in_array($v, $newAuthors));
}
$idle = mt_rand(1, 5 * 60);
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'test.log', date('Y-m-d H:i:s') . ' 暂停 ' . $idle . ' 秒' . PHP_EOL, FILE_APPEND);
sleep($idle);