3 namespace Rapsys\AirBundle\Command
;
5 use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand
;
6 use Doctrine\Persistence\ManagerRegistry
;
7 use Symfony\Component\Console\Input\InputInterface
;
8 use Symfony\Component\Console\Output\OutputInterface
;
9 use Symfony\Component\Filesystem\Exception\IOException
;
10 use Symfony\Component\Filesystem\Filesystem
;
12 use Rapsys\AirBundle\Entity\Session
;
14 class WeatherCommand
extends DoctrineCommand
{
15 //Set failure constant
18 ///Set success constant
23 //Mostly useless in fact
25 //Required to simplify simplexml transition
27 //Required to avoid xml errors
28 'quote-nbsp' => false,
29 //Required to fix code
33 ///Set accuweather uris
34 private $accuweather = [
37 75001 => 'https://www.accuweather.com/en/fr/paris-01-louvre/75001/hourly-weather-forecast/179142_pc?day=',
38 75004 => 'https://www.accuweather.com/en/fr/paris-04-hotel-de-ville/75004/hourly-weather-forecast/179145_pc?day=',
39 75005 => 'https://www.accuweather.com/en/fr/paris-05-pantheon/75005/hourly-weather-forecast/179146_pc?day=',
40 75006 => 'https://www.accuweather.com/fr/fr/paris-06-luxembourg/75006/hourly-weather-forecast/179147_pc?day=',
41 75007 => 'https://www.accuweather.com/en/fr/paris-07-palais-bourbon/75007/hourly-weather-forecast/179148_pc?day=',
42 75009 => 'https://www.accuweather.com/en/fr/paris-09-opera/75009/hourly-weather-forecast/179150_pc?day=',
43 75013 => 'https://www.accuweather.com/en/fr/paris-13-gobelins/75013/hourly-weather-forecast/179154_pc?day=',
44 75015 => 'https://www.accuweather.com/en/fr/paris-15-vaugirard/75015/hourly-weather-forecast/179156_pc?day=',
45 75019 => 'https://www.accuweather.com/en/fr/paris-19-buttes-chaumont/75019/hourly-weather-forecast/179160_pc?day=',
46 75116 => 'https://www.accuweather.com/en/fr/paris-16-passy/75116/hourly-weather-forecast/179246_pc?day='
50 75001 => 'https://www.accuweather.com/en/fr/paris-01-louvre/75001/daily-weather-forecast/179142_pc',
51 75004 => 'https://www.accuweather.com/en/fr/paris-04-hotel-de-ville/75004/daily-weather-forecast/179145_pc',
52 75005 => 'https://www.accuweather.com/en/fr/paris-05-pantheon/75005/daily-weather-forecast/179146_pc',
53 75006 => 'https://www.accuweather.com/fr/fr/paris-06-luxembourg/75006/daily-weather-forecast/179147_pc',
54 75007 => 'https://www.accuweather.com/en/fr/paris-07-palais-bourbon/75007/daily-weather-forecast/179148_pc',
55 75009 => 'https://www.accuweather.com/en/fr/paris-09-opera/75009/daily-weather-forecast/179150_pc',
56 75013 => 'https://www.accuweather.com/en/fr/paris-13-gobelins/75013/daily-weather-forecast/179154_pc',
57 75015 => 'https://www.accuweather.com/en/fr/paris-15-vaugirard/75015/daily-weather-forecast/179156_pc',
58 75019 => 'https://www.accuweather.com/en/fr/paris-19-buttes-chaumont/75019/daily-weather-forecast/179160_pc',
59 75116 => 'https://www.accuweather.com/en/fr/paris-16-passy/75116/daily-weather-forecast/179246_pc'
66 ///Set manager registry
72 ///Weather command constructor
73 public function __construct(ManagerRegistry
$doctrine, Filesystem
$filesystem) {
74 parent
::__construct($doctrine);
77 $this->doctrine
= $doctrine;
80 $this->filesystem
= $filesystem;
83 ///Configure attribute command
84 protected function configure() {
88 ->setName('rapsysair:weather')
89 //Set description shown with bin/console list
90 ->setDescription('Updates session rain and temperature fields')
91 //Set description shown with bin/console --help airlibre:attribute
92 ->setHelp('This command updates session rain and temperature fields in next three days')
93 //Add daily and hourly aliases
94 ->setAliases(['rapsysair:weather:daily', 'rapsysair:weather:hourly']);
97 ///Process the attribution
98 protected function execute(InputInterface
$input, OutputInterface
$output) {
100 $kernel = $this->getApplication()->getKernel();
103 $tmpdir = $kernel->getContainer()->getParameter('kernel.project_dir').'/var/cache/weather';
106 //XXX: worst case scenario we have 3 files per zipcode plus daily
107 if (!is_dir($tmpdir)) {
110 $this->filesystem
->mkdir($tmpdir, 0775);
111 } catch (IOException
$exception) {
113 echo 'Create dir '.$exception->getPath().' failed'."\n";
126 //Init zipcodes array
132 //Process hourly accuweather
133 if (($command = $input->getFirstArgument()) == 'rapsysair:weather:hourly' || $command == 'rapsysair:weather') {
134 //Fetch hourly sessions to attribute
135 $types['hourly'] = $this->doctrine
->getRepository(Session
::class)->findAllPendingHourlyWeather();
137 //Iterate on each session
138 foreach($types['hourly'] as $sessionId => $session) {
140 $zipcode = $session->getLocation()->getZipcode();
143 $start = $session->getStart();
146 $day = $start->diff((new \
DateTime('now'))->setTime(0, 0, 0))->d +
1;
148 //Check if zipcode is set
149 if (!isset($zipcodes[$zipcode])) {
150 $zipcodes[$zipcode] = [];
153 //Check if zipcode date is set
154 if (!isset($zipcodes[$zipcode][$day])) {
155 $zipcodes[$zipcode][$day] = [ $sessionId => $sessionId ];
157 $zipcodes[$zipcode][$day][$sessionId] = $sessionId;
161 $stop = $session->getStop();
164 $day = $stop->diff((new \
DateTime('now'))->setTime(0, 0, 0))->d +
1;
166 //Check if zipcode date is set
167 if (!isset($zipcodes[$zipcode][$day])) {
168 $zipcodes[$zipcode][$day] = [ $sessionId => $sessionId ];
170 $zipcodes[$zipcode][$day][$sessionId] = $sessionId;
175 //Process daily accuweather
176 if ($command == 'rapsysair:weather:daily' || $command == 'rapsysair:weather') {
177 //Fetch daily sessions to attribute
178 $types['daily'] = $this->doctrine
->getRepository(Session
::class)->findAllPendingDailyWeather();
180 //Iterate on each session
181 foreach($types['daily'] as $sessionId => $session) {
183 $zipcode = $session->getLocation()->getZipcode();
186 $start = $session->getStart();
191 //Check if zipcode is set
192 if (!isset($zipcodes[$zipcode])) {
193 $zipcodes[$zipcode] = [];
196 //Check if zipcode date is set
197 if (!isset($zipcodes[$zipcode][$day])) {
198 $zipcodes[$zipcode][$day] = [ $sessionId => $sessionId ];
200 $zipcodes[$zipcode][$day][$sessionId] = $sessionId;
211 //Iterate on zipcodes
212 foreach($zipcodes as $zipcode => $days) {
214 foreach($days as $day => $null) {
215 //Try to load content from cache
216 if (!is_file($file = $tmpdir.'/'.$zipcode.'.'.$day.'.html') || stat($file)['ctime'] <= (time() - ($day == 'daily' ? 4 : 2)*3600) || ($content = file_get_contents($file)) === false) {
217 //Prevent timing detection
218 //XXX: from 0.1 to 5 seconds
219 usleep(rand(1,50) * 100000);
222 //TODO: for daily we may load data for requested quarter of the day
223 $content = $this->curl_get($day == 'daily' ? $this->accuweather
['daily'][$zipcode] : $this->accuweather
['hourly'][$zipcode].$day);
226 if (file_put_contents($tmpdir.'/'.$zipcode.'.'.$day.'.html', $content) === false) {
228 echo 'Write to '.$tmpdir.'/'.$zipcode.'.'.$day.'.html failed'."\n";
236 $tidy->parseString($content, $this->config
, 'utf8');
239 //XXX: don't care about theses errors, tidy is here to fix...
240 #if (!empty($tidy->errorBuffer)) {
241 # var_dump($tidy->errorBuffer);
242 # die('Tidy errors');
246 //XXX: trash all xmlns= broken tags
247 $sx = new \
SimpleXMLElement(str_replace(['xmlns=', 'xlink:href='], ['xns=', 'href='], $tidy));
250 if ($day == 'daily') {
251 //Iterate on each link containing data
252 foreach($sx->xpath('//a[contains(@class,"daily-forecast-card")]') as $node) {
254 $dsm = trim($node->div
[0]->h2
[0]->span
[1]);
257 $temperature = str_replace('Ā°', '', $node->div
[0]->div
[0]->span
[0]);
260 $rainrisk = str_replace('%', '', trim($node->div
[2]))/100;
263 $data[$zipcode][$dsm]['daily'] = [
264 'temperature' => $temperature,
265 'rainrisk' => $rainrisk
270 //Iterate on each div containing data
271 #(string)$sx->xpath('//div[@class="hourly-card-nfl"]')[0]->attributes()->value
272 #/html/body/div[1]/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/div/h2/span[1]
273 foreach($sx->xpath('//div[@data-shared="false"]') as $node) {
275 $hour = trim(str_replace(' h', '', $node->div
[0]->div
[0]->div
[0]->h2
[0]->span
[0]));
277 //Compute dsm from day (1=d,2=d+1,3=d+2)
278 $dsm = (new \
DateTime('+'.($day - 1).' day'))->format('d/m');
281 $temperature = str_replace('Ā°', '', $node->div
[0]->div
[0]->div
[0]->div
[0]);
284 $realfeel = trim(str_replace(['RealFeelĀ®', 'Ā°'], '', $node->div
[0]->div
[0]->div
[1]->div
[0]->div
[0]->div
[0]));
287 $rainrisk = floatval(str_replace('%', '', trim($node->div
[0]->div
[0]->div
[2]->div
[0]))/100);
289 //Set rainfall to 0 (mm)
292 //Iterate on each entry
293 //TODO: wind and other infos are present in $node->div[1]->div[0]->div[1]->div[0]->p
294 foreach($node->div
[1]->div
[0]->div
[1]->div
[1]->p
as $p) {
295 //Lookup for rain entry if present
296 if (trim($p) == 'Rain') {
298 $rainfall = floatval(str_replace(' mm', '', $p->span
[0]));
303 $data[$zipcode][$dsm][$hour] = [
304 'temperature' => $temperature,
305 'realfeel' => $realfeel,
306 'rainrisk' => $rainrisk,
307 'rainfall' => $rainfall
318 foreach($types as $type => $sessions) {
319 //Iterate on each type
320 foreach($sessions as $sessionId => $session) {
322 $zipcode = $session->getLocation()->getZipcode();
325 $start = $session->getStart();
328 if ($type == 'daily') {
330 $period = [ $start ];
334 $stop = $session->getStop();
337 $period = new \
DatePeriod(
340 //Iterate on each hour
341 new \
DateInterval('PT1H'),
342 //End with begin + length
352 'realfeelmin' => null,
353 'realfeelmax' => null,
355 'temperaturemin' => null,
356 'temperaturemax' => null
359 //Iterate on the period
360 foreach($period as $time) {
362 $dsm = $time->format('d/m');
365 $hour = $type=='daily'?$type:$time->format('H');
367 //Check data availability
368 //XXX: sometimes startup delay causes weather data to be unavailable for session first hour
369 if (!isset($data[$zipcode][$dsm][$hour])) {
370 //Skip unavailable data
375 $info = $data[$zipcode][$dsm][$hour];
377 //Check if rainrisk is higher
378 if ($meteo['rainrisk'] === null || $info['rainrisk'] > $meteo['rainrisk']) {
379 //Set highest rain risk
380 $meteo['rainrisk'] = floatval($info['rainrisk']);
383 //Check if rainfall is set
384 if (isset($info['rainfall'])) {
386 $meteo['rainfall'] +
= floatval($info['rainfall']);
390 $meteo['temperature'][$hour] = $info['temperature'];
393 if ($type != 'daily') {
394 //Check min temperature
395 if ($meteo['temperaturemin'] === null || $info['temperature'] < $meteo['temperaturemin']) {
396 $meteo['temperaturemin'] = floatval($info['temperature']);
399 //Check max temperature
400 if ($meteo['temperaturemax'] === null || $info['temperature'] > $meteo['temperaturemax']) {
401 $meteo['temperaturemax'] = floatval($info['temperature']);
405 //Check if realfeel is set
406 if (isset($info['realfeel'])) {
408 $meteo['realfeel'][$hour] = $info['realfeel'];
411 if ($meteo['realfeelmin'] === null || $info['realfeel'] < $meteo['realfeelmin']) {
412 $meteo['realfeelmin'] = floatval($info['realfeel']);
416 if ($meteo['realfeelmax'] === null || $info['realfeel'] > $meteo['realfeelmax']) {
417 $meteo['realfeelmax'] = floatval($info['realfeel']);
422 //Check if rainfall is set and differ
423 if ($session->getRainfall() !== $meteo['rainfall']) {
425 $session->setRainfall($meteo['rainfall']);
428 //Check if rainrisk differ
429 if ($session->getRainrisk() !== $meteo['rainrisk']) {
431 $session->setRainrisk($meteo['rainrisk']);
434 //Check realfeel array
435 if ($meteo['realfeel'] !== []) {
437 $realfeel = floatval(round(array_sum($meteo['realfeel'])/count($meteo['realfeel']),1));
439 //Check if realfeel differ
440 if ($session->getRealfeel() !== $realfeel) {
441 //Set average realfeel
442 #$meteo['realfeel'] = array_sum($meteo['realfeel'])/count($meteo['realfeel']);
443 $session->setRealfeel($realfeel);
446 //Check if realfeelmin differ
447 if ($session->getRealfeelmin() !== $meteo['realfeelmin']) {
449 $session->setRealfeelmin($meteo['realfeelmin']);
452 //Check if realfeelmax differ
453 if ($session->getRealfeelmax() !== $meteo['realfeelmax']) {
455 $session->setRealfeelmax($meteo['realfeelmax']);
459 //Check temperature array
460 if ($meteo['temperature'] !== []) {
461 //Compute temperature
462 $temperature = floatval(round(array_sum($meteo['temperature'])/count($meteo['temperature']),1));
464 //Check if temperature differ
465 if ($session->getTemperature() !== $temperature) {
466 //Set average temperature
467 #$meteo['temperature'] = array_sum($meteo['temperature'])/count($meteo['temperature']);
468 $session->setTemperature($temperature);
471 //Check if temperaturemin differ
472 if ($session->getTemperaturemin() !== $meteo['temperaturemin']) {
474 $session->setTemperaturemin($meteo['temperaturemin']);
477 //Check if temperaturemax differ
478 if ($session->getTemperaturemax() !== $meteo['temperaturemax']) {
480 $session->setTemperaturemax($meteo['temperaturemax']);
486 //Flush to get the ids
487 $this->doctrine
->getManager()->flush();
493 return self
::SUCCESS
;
499 * @return bool|void Return success or exit
501 function curl_init() {
503 if (($this->ch
= curl_init()) === false) {
505 echo 'Curl init failed: '.curl_error($this->ch
)."\n";
516 CURLOPT_HTTP_VERSION
=> CURL_HTTP_VERSION_2_0
,
518 CURLOPT_HTTPHEADER
=> [
519 //XXX: it seems that you can disable akamai fucking protection with Pragma: akamai-x-cache-off
520 //XXX: see https://support.globaldots.com/hc/en-us/articles/115003996705-Akamai-Pragma-Headers-overview
521 #'Pragma: akamai-x-cache-off',
522 //XXX: working curl command
523 #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/'
525 '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',
526 //Set accept language
527 'Accept-Language: en-GB,en;q=0.9',
529 'Cache-Control: no-cache',
530 //Keep connection alive
531 'Connection: keep-alive',
534 //Force secure requests
535 'Upgrade-Insecure-Requests: 1',
537 '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',
538 //Force akamai cookie
539 //XXX: seems to come from http request
543 CURLOPT_COOKIEFILE
=> '',
544 //Disable location following
545 CURLOPT_FOLLOWLOCATION
=> false,
547 #CURLOPT_URL => $url = 'https://www.accuweather.com/',
549 CURLOPT_HEADER
=> true,
551 CURLOPT_RETURNTRANSFER
=> true,
554 CURLINFO_HEADER_OUT
=> true
559 echo 'Curl setopt array failed: '.curl_error($this->ch
)."\n";
571 * @return string|void Return url content or exit
573 function curl_get($url) {
575 if (curl_setopt($this->ch
, CURLOPT_URL
, $url) === false) {
577 echo 'Setopt for '.$url.' failed: '.curl_error($this->ch
)."\n";
580 curl_close($this->ch
);
586 //Check return status
587 if (($response = curl_exec($this->ch
)) === false) {
589 echo 'Get for '.$url.' failed: '.curl_error($this->ch
)."\n";
591 //Display sent headers
592 var_dump(curl_getinfo($this->ch
, CURLINFO_HEADER_OUT
));
598 curl_close($this->ch
);
605 if (empty($hs = curl_getinfo($this->ch
, CURLINFO_HEADER_SIZE
))) {
607 echo 'Getinfo for '.$url.' failed: '.curl_error($this->ch
)."\n";
609 //Display sent headers
610 var_dump(curl_getinfo($this->ch
, CURLINFO_HEADER_OUT
));
616 curl_close($this->ch
);
623 if (empty($header = substr($response, 0, $hs))) {
625 echo 'Header for '.$url.' empty: '.curl_error($this->ch
)."\n";
627 //Display sent headers
628 var_dump(curl_getinfo($this->ch
, CURLINFO_HEADER_OUT
));
634 curl_close($this->ch
);
640 //Check request success
641 if (strlen($header) <= 10 || substr($header, 0, 10) !== 'HTTP/2 200') {
643 echo 'Status for '.$url.' failed: '.curl_error($this->ch
)."\n";
645 //Display sent headers
646 var_dump(curl_getinfo($this->ch
, CURLINFO_HEADER_OUT
));
652 curl_close($this->ch
);
659 return substr($response, $hs);
665 * @return bool Return success or failure
667 function curl_close() {
668 return curl_close($this->ch
);