]> Raphaël G. Git Repositories - airbundle/blob - Command/WeatherCommand.php
Version 0.6.0
[airbundle] / Command / WeatherCommand.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Command;
13
14 use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand;
15 use Doctrine\Persistence\ManagerRegistry;
16
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Output\OutputInterface;
19 use Symfony\Component\Filesystem\Exception\IOException;
20 use Symfony\Component\Filesystem\Filesystem;
21
22 use Rapsys\AirBundle\Entity\Session;
23
24 class WeatherCommand extends DoctrineCommand {
25 //Set failure constant
26 const FAILURE = 1;
27
28 ///Set success constant
29 const SUCCESS = 0;
30
31 ///Set Tidy config
32 private $config = [
33 //Mostly useless in fact
34 'indent' => true,
35 //Required to simplify simplexml transition
36 'output-xml' => true,
37 //Required to avoid xml errors
38 'quote-nbsp' => false,
39 //Required to fix code
40 'clean' => true
41 ];
42
43 ///Set accuweather uris
44 private $accuweather = [
45 //Hourly uri
46 'hourly' => [
47 75001 => 'https://www.accuweather.com/en/fr/paris-01-louvre/75001/hourly-weather-forecast/179142_pc?day=',
48 75004 => 'https://www.accuweather.com/en/fr/paris-04-hotel-de-ville/75004/hourly-weather-forecast/179145_pc?day=',
49 75005 => 'https://www.accuweather.com/en/fr/paris-05-pantheon/75005/hourly-weather-forecast/179146_pc?day=',
50 75006 => 'https://www.accuweather.com/fr/fr/paris-06-luxembourg/75006/hourly-weather-forecast/179147_pc?day=',
51 75007 => 'https://www.accuweather.com/en/fr/paris-07-palais-bourbon/75007/hourly-weather-forecast/179148_pc?day=',
52 75009 => 'https://www.accuweather.com/en/fr/paris-09-opera/75009/hourly-weather-forecast/179150_pc?day=',
53 75010 => 'https://www.accuweather.com/en/fr/paris-10-entrepot/75010/hourly-weather-forecast/179151_pc?day=',
54 75012 => 'https://www.accuweather.com/en/fr/paris-12-reuilly/75012/hourly-weather-forecast/179153_pc?day=',
55 75013 => 'https://www.accuweather.com/en/fr/paris-13-gobelins/75013/hourly-weather-forecast/179154_pc?day=',
56 75015 => 'https://www.accuweather.com/en/fr/paris-15-vaugirard/75015/hourly-weather-forecast/179156_pc?day=',
57 75019 => 'https://www.accuweather.com/en/fr/paris-19-buttes-chaumont/75019/hourly-weather-forecast/179160_pc?day=',
58 75116 => 'https://www.accuweather.com/en/fr/paris-16-passy/75116/hourly-weather-forecast/179246_pc?day='
59 ],
60 //Daily uri
61 'daily' => [
62 75001 => 'https://www.accuweather.com/en/fr/paris-01-louvre/75001/10-day-weather-forecast/179142_pc?page=%7Bpage%7D',
63 75004 => 'https://www.accuweather.com/en/fr/paris-04-hotel-de-ville/75004/10-day-weather-forecast/179145_pc?page=%7Bpage%7D',
64 75005 => 'https://www.accuweather.com/en/fr/paris-05-pantheon/75005/10-day-weather-forecast/179146_pc?page=%7Bpage%7D',
65 75006 => 'https://www.accuweather.com/fr/fr/paris-06-luxembourg/75006/10-day-weather-forecast/179147_pc?page=%7Bpage%7D',
66 75007 => 'https://www.accuweather.com/en/fr/paris-07-palais-bourbon/75007/10-day-weather-forecast/179148_pc?page=%7Bpage%7D',
67 75009 => 'https://www.accuweather.com/en/fr/paris-09-opera/75009/10-day-weather-forecast/179150_pc?page=%7Bpage%7D',
68 75010 => 'https://www.accuweather.com/en/fr/paris-10-entrepot/75010/10-day-weather-forecast/179151_pc?page=%7Bpage%7D',
69 75012 => 'https://www.accuweather.com/en/fr/paris-12-reuilly/75012/10-day-weather-forecast/179153_pc?page=%7Bpage%7D',
70 75013 => 'https://www.accuweather.com/en/fr/paris-13-gobelins/75013/10-day-weather-forecast/179154_pc?page=%7Bpage%7D',
71 75015 => 'https://www.accuweather.com/en/fr/paris-15-vaugirard/75015/10-day-weather-forecast/179156_pc?page=%7Bpage%7D',
72 75019 => 'https://www.accuweather.com/en/fr/paris-19-buttes-chaumont/75019/10-day-weather-forecast/179160_pc?page=%7Bpage%7D',
73 75116 => 'https://www.accuweather.com/en/fr/paris-16-passy/75116/10-day-weather-forecast/179246_pc?page=%7Bpage%7D'
74 ]
75 ];
76
77 ///Set curl handler
78 private \CurlHandle|false $ch = false;
79
80 ///Weather command constructor
81 public function __construct(private ManagerRegistry $doctrine, private Filesystem $filesystem) {
82 parent::__construct($doctrine);
83 }
84
85 ///Configure attribute command
86 protected function configure():void {
87 //Configure the class
88 $this
89 //Set name
90 ->setName('rapsysair:weather')
91 //Set description shown with bin/console list
92 ->setDescription('Updates session rain and temperature fields')
93 //Set description shown with bin/console --help airlibre:attribute
94 ->setHelp('This command updates session rain and temperature fields in next three days')
95 //Add daily and hourly aliases
96 ->setAliases(['rapsysair:weather:daily', 'rapsysair:weather:hourly']);
97 }
98
99 ///Process the attribution
100 protected function execute(InputInterface $input, OutputInterface $output): int {
101 //Kernel object
102 $kernel = $this->getApplication()->getKernel();
103
104 //Tmp directory
105 $tmpdir = $kernel->getContainer()->getParameter('kernel.project_dir').'/var/cache/weather';
106
107 //Set tmpdir
108 //XXX: worst case scenario we have 3 files per zipcode plus daily
109 if (!is_dir($tmpdir)) {
110 try {
111 //Create dir
112 $this->filesystem->mkdir($tmpdir, 0775);
113 } catch (IOException $exception) {
114 //Display error
115 echo 'Create dir '.$exception->getPath().' failed'."\n";
116
117 //Exit with failure
118 exit(self::FAILURE);
119 }
120 }
121
122 //Cleanup kernel
123 unset($kernel);
124
125 //Tidy object
126 $tidy = new \tidy();
127
128 //Init zipcodes array
129 $zipcodes = [];
130
131 //Init types
132 $types = [];
133
134 //Process hourly accuweather
135 if (($command = $input->getFirstArgument()) == 'rapsysair:weather:hourly' || $command == 'rapsysair:weather') {
136 //Fetch hourly sessions to attribute
137 $types['hourly'] = $this->doctrine->getRepository(Session::class)->findAllPendingHourlyWeather();
138
139 //Iterate on each session
140 foreach($types['hourly'] as $sessionId => $session) {
141 //Get zipcode
142 $zipcode = $session->getLocation()->getZipcode();
143
144 //Get start
145 $start = $session->getStart();
146
147 //Set start day
148 $day = $start->diff((new \DateTime('now'))->setTime(0, 0, 0))->d + 1;
149
150 //Check if zipcode is set
151 if (!isset($zipcodes[$zipcode])) {
152 $zipcodes[$zipcode] = [];
153 }
154
155 //Check if zipcode date is set
156 if (!isset($zipcodes[$zipcode][$day])) {
157 $zipcodes[$zipcode][$day] = [ $sessionId => $sessionId ];
158 } else {
159 $zipcodes[$zipcode][$day][$sessionId] = $sessionId;
160 }
161
162 //Get stop
163 $stop = $session->getStop();
164
165 //Set stop day
166 $day = $stop->diff((new \DateTime('now'))->setTime(0, 0, 0))->d + 1;
167
168 //Skip 4th day
169 //XXX: accuweather only allow until 3rd day
170 if ($day >= 4) {
171 continue;
172 }
173
174 //Check if zipcode date is set
175 if (!isset($zipcodes[$zipcode][$day])) {
176 $zipcodes[$zipcode][$day] = [ $sessionId => $sessionId ];
177 } else {
178 $zipcodes[$zipcode][$day][$sessionId] = $sessionId;
179 }
180 }
181 }
182
183 //Process daily accuweather
184 if ($command == 'rapsysair:weather:daily' || $command == 'rapsysair:weather') {
185 //Fetch daily sessions to attribute
186 $types['daily'] = $this->doctrine->getRepository(Session::class)->findAllPendingDailyWeather();
187
188 //Iterate on each session
189 foreach($types['daily'] as $sessionId => $session) {
190 //Get zipcode
191 $zipcode = $session->getLocation()->getZipcode();
192
193 //Get start
194 $start = $session->getStart();
195
196 //Set start day
197 $day = 'daily';
198
199 //Check if zipcode is set
200 if (!isset($zipcodes[$zipcode])) {
201 $zipcodes[$zipcode] = [];
202 }
203
204 //Check if zipcode date is set
205 if (!isset($zipcodes[$zipcode][$day])) {
206 $zipcodes[$zipcode][$day] = [ $sessionId => $sessionId ];
207 } else {
208 $zipcodes[$zipcode][$day][$sessionId] = $sessionId;
209 }
210 }
211 }
212
213 //Init curl
214 $this->curl_init($tmpdir);
215
216 //Init data array
217 $data = [];
218
219 //Iterate on zipcodes
220 foreach($zipcodes as $zipcode => $days) {
221 //Iterate on days
222 foreach($days as $day => $null) {
223 //Try to load content from cache
224 if (!is_file($file = $tmpdir.'/'.$zipcode.'.'.$day.'.html') || stat($file)['ctime'] <= (time() - ($day == 'daily' ? 4 : 2)*3600) || ($content = file_get_contents($file)) === false) {
225 //Prevent timing detection
226 //XXX: from 0.1 to 5 seconds
227 usleep(rand(1,50) * 100000);
228
229 //Get content
230 //TODO: for daily we may load data for requested quarter of the day
231 $content = $this->curl_get($day == 'daily' ? $this->accuweather['daily'][$zipcode] : $this->accuweather['hourly'][$zipcode].$day);
232
233 //Store it
234 if (file_put_contents($tmpdir.'/'.$zipcode.'.'.$day.'.html', $content) === false) {
235 //Display error
236 echo 'Write to '.$tmpdir.'/'.$zipcode.'.'.$day.'.html failed'."\n";
237
238 //Exit with failure
239 exit(self::FAILURE);
240 }
241 }
242
243 //Parse string
244 $tidy->parseString($content, $this->config, 'utf8');
245
246 //Fix error buffer
247 //XXX: don't care about theses errors, tidy is here to fix...
248 #if (!empty($tidy->errorBuffer)) {
249 # var_dump($tidy->errorBuffer);
250 # die('Tidy errors');
251 #}
252
253 //Load simplexml
254 //XXX: trash all xmlns= broken tags
255 $sx = new \SimpleXMLElement(str_replace(['xmlns=', 'xlink:href='], ['xns=', 'href='], (string)$tidy));
256
257 //Process daily
258 if ($day == 'daily') {
259 //Iterate on each link containing data
260 foreach($sx->xpath('//a[contains(@class,"daily-forecast-card")]') as $node) {
261 //Get date
262 $dsm = trim((string)$node->div[0]->h2[0]->span[1]);
263
264 //Get temperature
265 $temperature = str_replace('°', '', (string)$node->div[0]->div[0]->span[0]);
266
267 //Get rainrisk
268 $rainrisk = trim(str_replace('%', '', (string)$node->div[1]))/100;
269
270 //Store data
271 $data[$zipcode][$dsm]['daily'] = [
272 'temperature' => $temperature,
273 'rainrisk' => $rainrisk
274 ];
275 }
276 //Process hourly
277 } else {
278 //Iterate on each div containing data
279 #(string)$sx->xpath('//div[@class="hourly-card-nfl"]')[0]->attributes()->value
280 #/html/body/div[1]/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/div/h2/span[1]
281 foreach($sx->xpath('//div[@data-shared="false"]') as $node) {
282 //Get hour
283 $hour = trim(str_replace(' h', '', (string)$node->div[0]->div[0]->div[0]->div[0]->div[0]->h2[0]));
284
285 //Compute dsm from day (1=d,2=d+1,3=d+2)
286 $dsm = (new \DateTime('+'.($day - 1).' day'))->format('d/m');
287
288 //Get temperature
289 $temperature = str_replace('°', '', (string)$node->div[0]->div[0]->div[0]->div[0]->div[1]);
290
291 //Get realfeel
292 $realfeel = trim(str_replace(['RealFeel®', '°'], '', (string)$node->div[0]->div[0]->div[0]->div[1]->div[0]->div[0]->div[0]));
293
294 //Get rainrisk
295 $rainrisk = floatval(str_replace('%', '', trim((string)$node->div[0]->div[0]->div[0]->div[2]->div[0]))/100);
296
297 //Set rainfall to 0 (mm)
298 $rainfall = 0;
299
300 //Iterate on each entry
301 //TODO: wind and other infos are present in $node->div[1]->div[0]->div[1]->div[0]->p
302 foreach($node->div[1]->div[0]->div[1]->div[0]->p as $p) {
303 //Lookup for rain entry if present
304 if (in_array(trim((string)$p), ['Rain', 'Pluie'])) {
305 //Get rainfall
306 $rainfall = floatval(str_replace(' mm', '', (string)$p->span[0]));
307 }
308 }
309
310 //Store data
311 $data[$zipcode][$dsm][$hour] = [
312 'temperature' => $temperature,
313 'realfeel' => $realfeel,
314 'rainrisk' => $rainrisk,
315 'rainfall' => $rainfall
316 ];
317 }
318 }
319
320 //Cleanup
321 unset($sx);
322 }
323 }
324
325 //Iterate on types
326 foreach($types as $type => $sessions) {
327 //Iterate on each type
328 foreach($sessions as $sessionId => $session) {
329 //Get zipcode
330 $zipcode = $session->getLocation()->getZipcode();
331
332 //Get start
333 $start = $session->getStart();
334
335 //Daily type
336 if ($type == 'daily') {
337 //Set period
338 $period = [ $start ];
339 //Hourly type
340 } else {
341 //Get stop
342 $stop = $session->getStop();
343
344 //Compute period
345 $period = new \DatePeriod(
346 //Start from begin
347 $start,
348 //Iterate on each hour
349 new \DateInterval('PT1H'),
350 //End with begin + length
351 $stop
352 );
353 }
354
355 //Set meteo
356 $meteo = [
357 'rainfall' => null,
358 'rainrisk' => null,
359 'realfeel' => [],
360 'realfeelmin' => null,
361 'realfeelmax' => null,
362 'temperature' => [],
363 'temperaturemin' => null,
364 'temperaturemax' => null
365 ];
366
367 //Iterate on the period
368 foreach($period as $time) {
369 //Set dsm
370 $dsm = $time->format('d/m');
371
372 //Set hour
373 $hour = $type=='daily'?$type:$time->format('H');
374
375 //Check data availability
376 //XXX: sometimes startup delay causes weather data to be unavailable for session first hour
377 if (!isset($data[$zipcode][$dsm][$hour])) {
378 //Skip unavailable data
379 continue;
380 }
381
382 //Set info alias
383 $info = $data[$zipcode][$dsm][$hour];
384
385 //Check if rainrisk is higher
386 if ($meteo['rainrisk'] === null || $info['rainrisk'] > $meteo['rainrisk']) {
387 //Set highest rain risk
388 $meteo['rainrisk'] = floatval($info['rainrisk']);
389 }
390
391 //Check if rainfall is set
392 if (isset($info['rainfall'])) {
393 //Set rainfall sum
394 $meteo['rainfall'] += floatval($info['rainfall']);
395 }
396
397 //Add temperature
398 $meteo['temperature'][$hour] = $info['temperature'];
399
400 //Hourly type
401 if ($type != 'daily') {
402 //Check min temperature
403 if ($meteo['temperaturemin'] === null || $info['temperature'] < $meteo['temperaturemin']) {
404 $meteo['temperaturemin'] = floatval($info['temperature']);
405 }
406
407 //Check max temperature
408 if ($meteo['temperaturemax'] === null || $info['temperature'] > $meteo['temperaturemax']) {
409 $meteo['temperaturemax'] = floatval($info['temperature']);
410 }
411 }
412
413 //Check if realfeel is set
414 if (isset($info['realfeel'])) {
415 //Add realfeel
416 $meteo['realfeel'][$hour] = $info['realfeel'];
417
418 //Check min realfeel
419 if ($meteo['realfeelmin'] === null || $info['realfeel'] < $meteo['realfeelmin']) {
420 $meteo['realfeelmin'] = floatval($info['realfeel']);
421 }
422
423 //Check max realfeel
424 if ($meteo['realfeelmax'] === null || $info['realfeel'] > $meteo['realfeelmax']) {
425 $meteo['realfeelmax'] = floatval($info['realfeel']);
426 }
427 }
428 }
429
430 //Check if rainfall is set and differ
431 if ($session->getRainfall() !== $meteo['rainfall']) {
432 //Set rainfall
433 $session->setRainfall($meteo['rainfall']);
434 }
435
436 //Check if rainrisk differ
437 if ($session->getRainrisk() !== $meteo['rainrisk']) {
438 //Set rainrisk
439 $session->setRainrisk($meteo['rainrisk']);
440 }
441
442 //Check realfeel array
443 if ($meteo['realfeel'] !== []) {
444 //Compute realfeel
445 $realfeel = floatval(round(array_sum($meteo['realfeel'])/count($meteo['realfeel']),1));
446
447 //Check if realfeel differ
448 if ($session->getRealfeel() !== $realfeel) {
449 //Set average realfeel
450 $session->setRealfeel($realfeel);
451 }
452
453 //Check if realfeelmin differ
454 if ($session->getRealfeelmin() !== $meteo['realfeelmin']) {
455 //Set realfeelmin
456 $session->setRealfeelmin($meteo['realfeelmin']);
457 }
458
459 //Check if realfeelmax differ
460 if ($session->getRealfeelmax() !== $meteo['realfeelmax']) {
461 //Set realfeelmax
462 $session->setRealfeelmax($meteo['realfeelmax']);
463 }
464 }
465
466 //Check temperature array
467 if ($meteo['temperature'] !== []) {
468 //Compute temperature
469 $temperature = floatval(round(array_sum($meteo['temperature'])/count($meteo['temperature']),1));
470
471 //Check if temperature differ
472 if ($session->getTemperature() !== $temperature) {
473 //Set average temperature
474 $session->setTemperature($temperature);
475 }
476
477 //Check if temperaturemin differ
478 if ($session->getTemperaturemin() !== $meteo['temperaturemin']) {
479 //Set temperaturemin
480 $session->setTemperaturemin($meteo['temperaturemin']);
481 }
482
483 //Check if temperaturemax differ
484 if ($session->getTemperaturemax() !== $meteo['temperaturemax']) {
485 //Set temperaturemax
486 $session->setTemperaturemax($meteo['temperaturemax']);
487 }
488 }
489 }
490 }
491
492 //Flush to get the ids
493 $this->doctrine->getManager()->flush();
494
495 //Return success
496 return self::SUCCESS;
497 }
498
499 /**
500 * Init curl handler
501 *
502 * @return bool|void Return success or exit
503 */
504 function curl_init(string $tmpdir) {
505 //Init curl
506 if (($this->ch = curl_init()) === false) {
507 //Display error
508 echo 'Curl init failed: '.curl_error($this->ch)."\n";
509 //Exit with failure
510 exit(self::FAILURE);
511 }
512
513 //Set curl options
514 if (
515 curl_setopt_array(
516 $this->ch,
517 [
518 //Force http2
519 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
520 //Accept encoding
521 CURLOPT_ACCEPT_ENCODING => '',
522 //Set http headers
523 CURLOPT_HTTPHEADER => [
524 //XXX: it seems that you can disable akamai fucking protection with Pragma: akamai-x-cache-off
525 //XXX: see https://support.globaldots.com/hc/en-us/articles/115003996705-Akamai-Pragma-Headers-overview
526 #'Pragma: akamai-x-cache-off',
527 //XXX: working curl command
528 #curl --http2 --cookie file.jar --cookie-jar file.jar -v -i -k -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Language: en-GB,en;q=0.9' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'Host: www.accuweather.com' -H 'Pragma: no-cache' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' 'https://www.accuweather.com/'
529 //Set accept
530 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
531 //Set encoding
532 'Accept-Encoding: gzip, deflate, br, zstd',
533 //Set accept language
534 'Accept-Language: en-GB,en;q=0.9',
535 //Disable cache
536 'Cache-Control: no-cache',
537 //Keep connection alive
538 #XXX: to drop? 'Connection: keep-alive',
539 //Do not track
540 'Dnt: 1',
541 //Disable cache
542 'Pragma: no-cache',
543 //Priority
544 'Priority: u=0, i',
545 //Sec ch ua
546 'Sec-CH-UA: "Chromium";v="148", "Google Chrome";v="148", "Not/A)Brand";v="99"',
547 //Sec ch ua mobile
548 'Sec-CH-Mobile: ?0',
549 //Sec ch ua platform
550 'Sec-CH-UA-Platform: "Windows"',
551 //Sec fetch dest
552 'Sec-Fetch-Dest: document',
553 //Sec fetch mode
554 'Sec-Fetch-Mode: navigate',
555 //Sec fetch site
556 'Sec-Fetch-Site: none',
557 //Sec fetch user
558 'Sec-Fetch-User: ?1',
559 //Force secure requests
560 'Upgrade-Insecure-Requests: 1',
561 //Set user agent
562 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36',
563 //Force akamai cookie
564 //XXX: seems to come from http request
565 #'Cookie: AKA_A2=A',
566 //TODO: add akamai cookie ?
567 //XXX: see https://www.ford.eu/cookie-guide
568 #ak_bmsc: ajouter ? valable un jour ?
569 #request-context: appId=cid-v1:69db91ae-4014-4da6-962b-6e1205fccce5
570 #< permissions-policy: browsing-topics=()
571 #permissions-policy: browsing-topics=()
572 #< vary: Accept-Encoding
573 #vary: Accept-Encoding
574 #< content-encoding: gzip
575 #content-encoding: gzip
576 #< date: Mon, 23 Mar 2026 22:36:43 GMT
577 #date: Mon, 23 Mar 2026 22:36:43 GMT
578 #< content-length: 39349
579 #content-length: 39349
580 #* Added cookie awx_id="1062821a-a1f2-42be-82ec-859a2aac16ef" for domain www.accuweather.com, path /, expire 1805841403
581 #< set-cookie: awx_id=1062821a-a1f2-42be-82ec-859a2aac16ef; expires=Tue, 23 Mar 2027 22:36:43 GMT; path=/; secure; httponly
582 #set-cookie: awx_id=1062821a-a1f2-42be-82ec-859a2aac16ef; expires=Tue, 23 Mar 2027 22:36:43 GMT; path=/; secure; httponly
583 #* Added cookie awx_session_essential="id%3A614c5979-6977-444e-a900-c0834e6602fb%7CpageView%3A1" for domain www.accuweather.com, path /, expire 1774306003
584 #< set-cookie: awx_session_essential=id%3A614c5979-6977-444e-a900-c0834e6602fb%7CpageView%3A1; expires=Mon, 23 Mar 2026 22:46:43 GMT; path=/; secure; samesite=lax; httponly
585 #set-cookie: awx_session_essential=id%3A614c5979-6977-444e-a900-c0834e6602fb%7CpageView%3A1; expires=Mon, 23 Mar 2026 22:46:43 GMT; path=/; secure; samesite=lax; httponly
586
587 #* Replaced cookie awx_id="1062821a-a1f2-42be-82ec-859a2aac16ef" for domain www.accuweather.com, path /, expire 1805841454
588 #< set-cookie: awx_id=1062821a-a1f2-42be-82ec-859a2aac16ef; expires=Tue, 23 Mar 2027 22:37:34 GMT; path=/; secure; httponly
589 #set-cookie: awx_id=1062821a-a1f2-42be-82ec-859a2aac16ef; expires=Tue, 23 Mar 2027 22:37:34 GMT; path=/; secure; httponly
590 #* Replaced cookie awx_session_essential="id%3A614c5979-6977-444e-a900-c0834e6602fb%7CpageView%3A2" for domain www.accuweather.com, path /, expire 1774306054
591 #< set-cookie: awx_session_essential=id%3A614c5979-6977-444e-a900-c0834e6602fb%7CpageView%3A2; expires=Mon, 23 Mar 2026 22:47:34 GMT; path=/; secure; samesite=lax; httponly
592 #set-cookie: awx_session_essential=id%3A614c5979-6977-444e-a900-c0834e6602fb%7CpageView%3A2; expires=Mon, 23 Mar 2026 22:47:34 GMT; path=/; secure; samesite=lax; httponly
593 #< strict-transport-security: max-age=15768000 ; includeSubDomains ; preload
594 #strict-transport-security: max-age=15768000 ; includeSubDomains ; preload
595 #* Added cookie ak_bmsc="010A87851EDC882D1FE84B535E75D0C6~000000000000000000000000000000~YAAQfOIlF3lI//WcAQAAZqrYHB/DJCb/+t9xHWxhT1EDW/Qe8778baYOLuqlhfzRUU1m6YXWfZjRJlfkpMYg5WdzrV2Y0WtHuiTknE9/AMA9MTh82uI4z4xr3Rv3TeZjG+ggc05Sp2rKiyYjHxgq7ZN9fHAFS0Mqab+DTtTtAZaSCeBc96tb+hg0c3TEXZSDRStGppvA1ejPTO1Q3jZ+PIXZ0NzR3LN4DcpmvgtcB1A4vIsIuXhqbsvz6ns2XdKIYgvrdY6v0mJA6+EbAv+O/u0WRlcM1akq5+Cahmm8lgpy52DRuq82lwLR0JwtUHc+JjukuY9bWSi595VZvehTyhnjXHqSY7M1qhpJ9IVR9wFRWWFo5SSOZpEW5xsl+n2Aph4XHkuEKxc2TnqXHA4fdgM=" for domain accuweather.com, path /, expire 1774312654
596 #< set-cookie: ak_bmsc=010A87851EDC882D1FE84B535E75D0C6~000000000000000000000000000000~YAAQfOIlF3lI//WcAQAAZqrYHB/DJCb/+t9xHWxhT1EDW/Qe8778baYOLuqlhfzRUU1m6YXWfZjRJlfkpMYg5WdzrV2Y0WtHuiTknE9/AMA9MTh82uI4z4xr3Rv3TeZjG+ggc05Sp2rKiyYjHxgq7ZN9fHAFS0Mqab+DTtTtAZaSCeBc96tb+hg0c3TEXZSDRStGppvA1ejPTO1Q3jZ+PIXZ0NzR3LN4DcpmvgtcB1A4vIsIuXhqbsvz6ns2XdKIYgvrdY6v0mJA6+EbAv+O/u0WRlcM1akq5+Cahmm8lgpy52DRuq82lwLR0JwtUHc+JjukuY9bWSi595VZvehTyhnjXHqSY7M1qhpJ9IVR9wFRWWFo5SSOZpEW5xsl+n2Aph4XHkuEKxc2TnqXHA4fdgM=; Domain=.accuweather.com; Path=/; Expires=Tue, 24 Mar 2026 00:37:34 GMT; Max-Age=7200; HttpOnly
597 #set-cookie: ak_bmsc=010A87851EDC882D1FE84B535E75D0C6~000000000000000000000000000000~YAAQfOIlF3lI//WcAQAAZqrYHB/DJCb/+t9xHWxhT1EDW/Qe8778baYOLuqlhfzRUU1m6YXWfZjRJlfkpMYg5WdzrV2Y0WtHuiTknE9/AMA9MTh82uI4z4xr3Rv3TeZjG+ggc05Sp2rKiyYjHxgq7ZN9fHAFS0Mqab+DTtTtAZaSCeBc96tb+hg0c3TEXZSDRStGppvA1ejPTO1Q3jZ+PIXZ0NzR3LN4DcpmvgtcB1A4vIsIuXhqbsvz6ns2XdKIYgvrdY6v0mJA6+EbAv+O/u0WRlcM1akq5+Cahmm8lgpy52DRuq82lwLR0JwtUHc+JjukuY9bWSi595VZvehTyhnjXHqSY7M1qhpJ9IVR9wFRWWFo5SSOZpEW5xsl+n2Aph4XHkuEKxc2TnqXHA4fdgM=; Domain=.accuweather.com; Path=/; Expires=Tue, 24 Mar 2026 00:37:34 GMT; Max-Age=7200; HttpOnly
598 #* Added cookie bm_mi="F052B40D9957D87932CF91B3BCD53103~YAAQfOIlF3pI//WcAQAAZqrYHB8MhsqiidkryWWG/FELf+Jop+KjoERXKsZW/oxZ+c6ldIx5C8iwD7ReaDYhYufdjAYrxGXN6LkLRd17iwN/5USm1sOBD3V1XD9K4ftXO6eBsjIxI9lfZvY8KN/HCAMLPn68AbLWCDNyNuku9oOc4cr4CqrTVdoz73o4crl1BVwm2bIxeXGnG5FhznRy3+99KgY6IjinBcHKaUIATLOgrvjwkTKB19/m4DWOpp805vVbaa6EdhuTAM2KjaNK6DUx9WR+GVWBQ63X0SJ+4lAT2qVWCWrfVWmDijgunHoTmQvbJHliBpapOvUe3ShoZOMromAbzvfuSO//PIwGNPEgvq8FDD19nNyZCAArgTkg11pXAgH8jaZRon811vKVu3+nyNM=~1" for domain accuweather.com, path /, expire 1
599 #< set-cookie: bm_mi=F052B40D9957D87932CF91B3BCD53103~YAAQfOIlF3pI//WcAQAAZqrYHB8MhsqiidkryWWG/FELf+Jop+KjoERXKsZW/oxZ+c6ldIx5C8iwD7ReaDYhYufdjAYrxGXN6LkLRd17iwN/5USm1sOBD3V1XD9K4ftXO6eBsjIxI9lfZvY8KN/HCAMLPn68AbLWCDNyNuku9oOc4cr4CqrTVdoz73o4crl1BVwm2bIxeXGnG5FhznRy3+99KgY6IjinBcHKaUIATLOgrvjwkTKB19/m4DWOpp805vVbaa6EdhuTAM2KjaNK6DUx9WR+GVWBQ63X0SJ+4lAT2qVWCWrfVWmDijgunHoTmQvbJHliBpapOvUe3ShoZOMromAbzvfuSO//PIwGNPEgvq8FDD19nNyZCAArgTkg11pXAgH8jaZRon811vKVu3+nyNM=~1; Domain=.accuweather.com; Path=/; Expires=Mon, 23 Mar 2026 22:37:34 GMT; Max-Age=0; Secure
600 #set-cookie: bm_mi=F052B40D9957D87932CF91B3BCD53103~YAAQfOIlF3pI//WcAQAAZqrYHB8MhsqiidkryWWG/FELf+Jop+KjoERXKsZW/oxZ+c6ldIx5C8iwD7ReaDYhYufdjAYrxGXN6LkLRd17iwN/5USm1sOBD3V1XD9K4ftXO6eBsjIxI9lfZvY8KN/HCAMLPn68AbLWCDNyNuku9oOc4cr4CqrTVdoz73o4crl1BVwm2bIxeXGnG5FhznRy3+99KgY6IjinBcHKaUIATLOgrvjwkTKB19/m4DWOpp805vVbaa6EdhuTAM2KjaNK6DUx9WR+GVWBQ63X0SJ+4lAT2qVWCWrfVWmDijgunHoTmQvbJHliBpapOvUe3ShoZOMromAbzvfuSO//PIwGNPEgvq8FDD19nNyZCAArgTkg11pXAgH8jaZRon811vKVu3+nyNM=~1; Domain=.accuweather.com; Path=/; Expires=Mon, 23 Mar 2026 22:37:34 GMT; Max-Age=0; Secure
601 #
602 #<
603 ],
604 //Enable cookie
605 CURLOPT_COOKIEFILE => $tmpdir.'/cookies.txt',
606 //Disable location following
607 CURLOPT_FOLLOWLOCATION => false,
608 //Set url
609 #CURLOPT_URL => $url = 'https://www.accuweather.com/',
610 //Return headers too
611 CURLOPT_HEADER => true,
612 //Return content
613 CURLOPT_RETURNTRANSFER => true,
614
615 //XXX: debug
616 CURLINFO_HEADER_OUT => true
617 ]
618 ) === false
619 ) {
620 //Display error
621 echo 'Curl setopt array failed: '.curl_error($this->ch)."\n";
622 //Exit with failure
623 exit(self::FAILURE);
624 }
625
626 //Return success
627 return true;
628 }
629
630 /**
631 * Get url
632 *
633 * @return string|void Return url content or exit
634 */
635 function curl_get($url) {
636 //Set url to fetch
637 if (curl_setopt($this->ch, CURLOPT_URL, $url) === false) {
638 //Display error
639 echo 'Setopt for '.$url.' failed: '.curl_error($this->ch)."\n";
640
641 //Exit with failure
642 exit(self::FAILURE);
643 }
644
645 //Check return status
646 if (($response = curl_exec($this->ch)) === false) {
647 //Display error
648 echo 'Get for '.$url.' failed: '.curl_error($this->ch)."\n";
649
650 //Display sent headers
651 var_dump(curl_getinfo($this->ch, CURLINFO_HEADER_OUT));
652
653 //Display response
654 var_dump($response);
655
656 //Exit with failure
657 exit(self::FAILURE);
658 }
659
660 //Get header size
661 if (empty($hs = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE))) {
662 //Display error
663 echo 'Getinfo for '.$url.' failed: '.curl_error($this->ch)."\n";
664
665 //Display sent headers
666 var_dump(curl_getinfo($this->ch, CURLINFO_HEADER_OUT));
667
668 //Display response
669 var_dump($response);
670
671 //Exit with failure
672 exit(self::FAILURE);
673 }
674
675 //Get header
676 if (empty($header = substr($response, 0, $hs))) {
677 //Display error
678 echo 'Header for '.$url.' empty: '.curl_error($this->ch)."\n";
679
680 //Display sent headers
681 var_dump(curl_getinfo($this->ch, CURLINFO_HEADER_OUT));
682
683 //Display response
684 var_dump($response);
685
686 //Exit with failure
687 exit(self::FAILURE);
688 }
689
690 //Check request success
691 if (strlen($header) <= 10 || substr($header, 0, 10) !== 'HTTP/2 200') {
692 //Display error
693 echo 'Status for '.$url.' failed: '.curl_error($this->ch)."\n";
694
695 //Display sent headers
696 var_dump(curl_getinfo($this->ch, CURLINFO_HEADER_OUT));
697
698 //Display response
699 var_dump($header);
700
701 //Exit with failure
702 exit(self::FAILURE);
703 }
704
705 //Return content
706 return substr($response, $hs);
707 }
708 }