-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.php
310 lines (255 loc) Β· 6.57 KB
/
crawl.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
declare(strict_types=1);
$Crawler = new HelpCrawler();
$Crawler->LoadLinks();
$Crawler->DiscoverFromSearch();
$Crawler->Crawl();
$Crawler->SaveLinks();
class HelpCrawler
{
/** @var \CurlHandle */
private $CurlHandle;
/** @var array<string, ?array> */
private array $Articles = [];
/** @var \SplQueue<string> */
private \SplQueue $LinksQueue;
private string $Directory = __DIR__ . '/docs/';
private string $LinksFile = __DIR__ . '/articles.json';
private bool $IsCI;
public function __construct()
{
$this->IsCI = \getenv( 'CI' ) !== false;
$this->LinksQueue = new \SplQueue();
$this->CurlHandle = curl_init( );
curl_setopt_array( $this->CurlHandle, [
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; SteamHelpTracking; +https://github.com/SteamDatabase/SteamHelpTracking)',
CURLOPT_ENCODING => 'gzip',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
] );
}
public function LoadLinks() : void
{
if( !file_exists( $this->LinksFile ) )
{
throw new Exception( 'File does not exist: ' . $this->LinksFile );
}
$Data = file_get_contents( $this->LinksFile );
if( $Data === false )
{
throw new Exception( 'Failed to read ' . $this->LinksFile );
}
$Links = json_decode( $Data, true );
foreach( $Links as $FaqID => $FAQ )
{
if( isset( $this->Articles[ $FaqID ] ) )
{
continue;
}
$this->Articles[ $FaqID ] = $FAQ;
$this->LinksQueue->push( (string)$FaqID );
}
echo 'Loaded ' . count( $Links ) . ' known articles' . PHP_EOL;
}
public function SaveLinks() : void
{
ksort( $this->Articles, SORT_NUMERIC );
file_put_contents( $this->LinksFile, json_encode( $this->Articles, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . PHP_EOL );
echo 'Saved ' . count( $this->Articles ) . ' articles' . PHP_EOL;
}
public function Crawl() : void
{
while( !$this->LinksQueue->isEmpty() )
{
$Link = $this->LinksQueue->pop();
$Content = $this->Fetch( $Link );
if( $Content !== null )
{
$this->Process( $Content );
}
}
}
public function DiscoverFromSearch() : void
{
$Queries =
[
'"Steam" OR "Valve" OR "I" OR "you" OR "STEAM_CLAN_IMAGE" OR "https" OR "game"',
];
if( $this->IsCI )
{
echo "::group::DiscoverFromSearch" . PHP_EOL;
}
foreach( $Queries as $Query )
{
$this->DiscoverFromSearchQuery( $Query );
}
if( $this->IsCI )
{
echo "::endgroup::" . PHP_EOL;
}
}
private function DiscoverFromSearchQuery( string $Query ) : void
{
$Cursor = '';
do
{
echo 'Searching "' . $Query . '" cursor ' . $Cursor . '... ';
curl_setopt( $this->CurlHandle, CURLOPT_URL, 'https://api.steampowered.com/IClanFAQSService/SearchFAQs/v1/?' . http_build_query( [
'count' => 100,
'search_text' => $Query,
'cursor' => $Cursor,
] ) );
$Content = (string)curl_exec( $this->CurlHandle );
echo curl_getinfo( $this->CurlHandle, CURLINFO_HTTP_CODE ) . ' - ';
$Data = json_decode( $Content, true );
echo ( $Data[ 'response' ][ 'num_total_results' ] ?? 0 ) . ' results - ';
echo count( $this->Articles ) . ' total articles' . PHP_EOL;
$Cursor = $Data[ 'response' ][ 'next_cursor' ] ?? '';
if( empty( $Data[ 'response' ][ 'faqs' ] ) )
{
break;
}
foreach( $Data[ 'response' ][ 'faqs' ] as $FAQ )
{
if( isset( $this->Articles[ $FAQ[ 'articleid' ] ] ) )
{
continue;
}
$this->Articles[ $FAQ[ 'articleid' ] ] = null;
$this->LinksQueue->push( $FAQ[ 'articleid' ] );
}
}
while( !empty( $Cursor ) && $Cursor !== '*' );
}
private function Fetch( string $FaqID ) : ?array
{
echo 'Fetching ' . $FaqID . '... ';
curl_setopt( $this->CurlHandle, CURLOPT_URL, 'https://api.steampowered.com/IClanFAQSService/GetFAQ/v1/?' . http_build_query( [
'faq_id' => $FaqID,
'language' => 0,
] ) );
$Content = (string)curl_exec( $this->CurlHandle );
echo curl_getinfo( $this->CurlHandle, CURLINFO_HTTP_CODE ) . ' - ';
$Data = json_decode( $Content, true );
if( empty( $Data[ 'response' ][ 'faq' ] ) )
{
echo '[NOT FOUND]' . PHP_EOL;
return null;
}
echo $Data[ 'response' ][ 'faq' ][ 'title' ] . PHP_EOL;
return $Data[ 'response' ][ 'faq' ];
}
private function Process( array $FAQ ) : void
{
$this->Articles[ $FAQ[ 'faq_id' ] ] =
[
'title' => $FAQ[ 'title' ],
'url_code' => $FAQ[ 'url_code' ],
'version' => (int)$FAQ[ 'version' ],
'timestamp' => date( 'Y-m-d H:i:s', $FAQ[ 'timestamp' ] ),
];
$CleanContent = str_replace( [
"\r",
"[section",
"[/section]",
"[expand",
"[/expand]",
"[exclude_realm",
"[/exclude_realm]",
"[list]",
"[/list]",
"[olist]",
"[/olist]",
"[table]",
"[tr]",
"[/tr]",
"[*]",
"[/*]",
"[hr]",
"[/hr]",
"[h2]",
"[/h2]",
"[h3]",
"[/h3]",
"[h4]",
"[/h4]",
"[h5]",
"[/h5]",
],
[
"",
"\n\n[section",
"\n[/section]\n\n",
"\n\n[expand",
"\n[/expand]\n\n",
"\n[exclude_realm",
"\n[/exclude_realm]\n",
"\n\n[list]\n",
"\n[/list]\n",
"\n\n[olist]\n",
"\n[/olist]\n",
"\n\n[table]\n",
"\n[tr]\n",
"\n[/tr]\n",
"\n[*]",
" [/*]\n",
"\n[hr]",
"[/hr]\n",
"\n\n[h2]",
"[/h2]\n",
"\n\n[h3]",
"[/h3]\n",
"\n\n[h4]",
"[/h4]\n",
"\n\n[h5]",
"[/h5]\n",
], $FAQ[ 'content' ] );
$CleanContent = preg_replace( [
'/^[\t ]+|[\t ]+$/m',
'/\[\/\*]\n{2,}\[\*]/',
'/\n{2,}/',
], [
"",
"[/*]\n[*]",
"\n\n",
], $CleanContent );
$Content = "[h1]{$FAQ[ 'title' ]}[/h1]\n\n";
$Content .= trim( $CleanContent );
$Content .= "\n";
$FileName = substr( $FAQ[ 'url_code' ] . '-' . $this->TitleToSlug( $FAQ[ 'title' ] ), 0, 150 );
$FullPath = str_replace( '\\', '/', $this->Directory . $FileName . '.txt' );
// Delete old files if any
$FilesToDelete = $this->Directory . $FAQ[ 'url_code' ] . '-*.txt';
foreach( glob( $FilesToDelete ) as $FileToDelete )
{
if( str_replace( '\\', '/', $FileToDelete ) === $FullPath )
{
continue;
}
$this->Err( 'Deleting ' . $FileToDelete );
unlink( $FileToDelete );
}
file_put_contents( $FullPath, $Content );
}
/** @pure */
private function TitleToSlug( string $Title ) : string
{
$Title = strtolower( $Title );
$Title = preg_replace( '/[^\pL\d]+/u', '-', $Title ); // replace non letter or digits by dashes
$Title = preg_replace( '/[^a-z0-9\-]/', '', $Title ); // remove unwanted characters
$Title = trim( $Title, '-' ); // trim dashes
return $Title;
}
private function Err( string $Message ) : void
{
if( $this->IsCI )
{
echo "::error::" . $Message . PHP_EOL;
}
else
{
fwrite( STDERR, $Message . PHP_EOL );
}
}
}