-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAunoController.php
253 lines (239 loc) · 7.01 KB
/
AunoController.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php declare(strict_types=1);
namespace Nadybot\User\Modules\AUNO_MODULE;
use function Amp\async;
use function Amp\Future\{await};
use Amp\Http\Client\{HttpClientBuilder, Request};
use Amp\TimeoutCancellation;
use Exception;
use League\Uri\{Modifier};
use Nadybot\Core\ParamClass\PItem;
use Nadybot\Core\{
Attributes as NCA,
CmdContext,
ModuleInstance,
Safe,
Text,
};
use Nadybot\Modules\ITEMS_MODULE\{
ItemSearchResult,
ItemsController,
};
/**
* @author Nadyita (RK5) <nadyita@hodorraid.org>
*/
#[
NCA\Instance(),
NCA\DefineCommand(
command: 'auno',
accessLevel: 'guest',
description: 'Search for comments on auno',
)
]
class AunoController extends ModuleInstance {
#[NCA\Inject]
private HttpClientBuilder $http;
#[NCA\Inject]
private ItemsController $itemsController;
/**
* Get the Auno object for an item by a search string, or show a choice dialogue
*
* @param string $search The string to search for
* @param CmdContext $context Where to send the reply yo
*
* @return AunoItem|null Either the item, or null if error or multiple choices presented
*/
public function getItemFromSearch(string $search, CmdContext $context): ?AunoItem {
// If this is a search string, search the item database for low and high ql
$findings = $this->itemsController->findItemsFromLocal($search, null);
// Remove duplicates
$lookup = [];
$findings = array_filter(
$findings,
static function (ItemSearchResult $item) use (&$lookup): bool {
if (isset($lookup["{$item->lowid}-{$item->highid}"])) {
return false;
}
return $lookup["{$item->lowid}-{$item->highid}"] = true;
}
);
// Nothing found? Errooooor
if (count($findings) === 0) {
$msg = "No items found matching <highlight>{$search}<end>.";
$context->reply($msg);
return null;
} elseif (count($findings) > 1) {
// If we found more than 1 item, check if there is an exact match first
$exactFindings = array_values(array_filter($findings, static function (ItemSearchResult $item): bool {
return $item->numExactMatches === 100;
}));
// If we didn't find an exact match, ask which one to use
if (count($exactFindings) !== 1) {
$blob = "Search: <highlight>{$search}<end>\n";
$num = count($findings);
foreach ($findings as $item) {
$itemLink = $item->getLink($item->highql);
$blob .= '[' . Text::makeChatcmd('See Comments', "/tell <myname> auno {$itemLink}") . '] '.
"{$itemLink}\n";
}
if ($num === $this->itemsController->maxitems) {
$blob .= "\n\n<highlight>*Results have been limited to the first ".
"{$this->itemsController->maxitems} results.<end>";
}
$blob .= "\n\n";
$link = Text::makeBlob("Item Search Results ({$num})", $blob, 'Choose item for which to display comments');
$context->reply($link);
return null;
}
$findings = $exactFindings;
}
$item = new AunoItem(
lowId: $findings[0]->lowid,
highId: $findings[0]->highid,
ql: $findings[0]->highql,
name: $findings[0]->name,
);
return $item;
}
/**
* Find an Auno Item by search term or pasted text
*
* @param string $search The text/object to search for
* @param CmdContext $context Where to send the replies to
*
* @return AunoItem|null The search object or null
*/
public function getItem(string $search, CmdContext $context): ?AunoItem {
try {
$item = new PItem($search);
return new AunoItem(
lowId: $item->lowID,
highId: $item->highID,
ql: $item->ql,
name: $item->name,
);
} catch (Exception) {
return $this->getItemFromSearch($search, $context);
}
}
/** Search auno for comments on an item */
#[NCA\HandlesCommand('auno')]
public function aunoCommand(CmdContext $context, string $search): void {
$item = $this->getItem($search, $context);
if ($item === null) {
return;
}
$jobs = [];
$jobs['low']= async($this->getAunoComments(...), $item->lowId);
// Download auno comments for low ID and high ID (if it's different to lowID) and merge them into 1
$comments = $this->getAunoComments($item->lowId);
if ($item->lowId !== $item->highId) {
$jobs['high'] = async($this->getAunoComments(...), $item->highId);
}
$jobComments = await($jobs);
$comments = $this->mergeComments(...$jobComments['low'], ...($jobComments['high']??[]));
// Display them
$itemLink = $this->makeItem($item);
if (count($comments) === 0) {
$msg = 'No comments found on auno.org for ' . $itemLink;
$context->reply($msg);
return;
}
$blobs = [];
$commentNum = 0;
foreach ($comments as $comment) {
$blobs []= sprintf(
"%02d - <highlight>%s<end> - <orange>%s<end>\n%s",
++$commentNum,
$comment->time,
$comment->user,
$comment->comment,
);
}
$blob = implode("\n\n<pagebreak>", $blobs);
$msg = Text::makeBlob(
count($blobs) . ' comments',
$blob,
count($blobs) . ' Auno comments for ' . $itemLink
);
$msg .= " found on Auno for {$itemLink}";
$context->reply($msg);
}
/**
* Merge all given comments together
*
* @return list<AunoComment>
*/
public function mergeComments(AunoComment ...$comments): array {
usort($comments, static function (AunoComment $a, AunoComment $b): int {
return strcmp($a->time, $b->time);
});
return $comments;
}
/**
* Load comments from AUNO for a specific item ID
*
* @param int $itemId The ID of the item
*
* @return list<AunoComment> The parsed commments
*/
public function getAunoComments(int $itemId): array {
$uri = Modifier::from('https://auno.org/ao/db.php')
->appendQueryParameters(['id' => $itemId]);
$request = new Request($uri->getUriString());
$client = $this->http->build();
$request->setTcpConnectTimeout(5);
$request->setTlsHandshakeTimeout(5);
$request->setTransferTimeout(5);
$response = $client->request($request, new TimeoutCancellation(10));
/** @var list<AunoComment> */
$comments = [];
if ($response->getStatus() !== 200) {
return $comments;
}
$body = $response->getBody()->buffer(new TimeoutCancellation(10));
if ($body === '') {
return $comments;
}
$matches = Safe::pregMatch(
"|<legend>Comments</legend>\s*<table class='list' style='width: 100%'>(.+?)</table>|s",
$body,
);
if (count($matches) < 2) {
return $comments;
}
$pages = Safe::pregMatchAll(
'|'.
"<span style='text-decoration: underline; font-size: 110%'>\s*".
"(?<user>.+?) @ (?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2})\s*".
"</span>\s*".
"<br />\s*".
"<div style='margin-bottom: 20px'>\s*".
"(?<comment>.*?)\s*".
'</div>'.
'|s',
$matches[1],
);
if (count($pages) < 1) {
return $comments;
}
for ($i = 0; $i < count($pages[0]); $i++) {
$comment = new AunoComment(
user: $pages['user'][$i],
time: $pages['time'][$i],
comment: $pages['comment'][$i],
);
$comments []= $comment->cleanComment();
}
return $comments;
}
/**
* Make a link to an item
*
* @param AunoItem $item The item to link to
*
* @return string The <a href...> link
*/
public function makeItem(AunoItem $item): string {
return Text::makeItem($item->lowId, $item->highId, $item->ql, $item->name);
}
}