tzkmx
8/17/2017 - 11:39 PM

GraphQL Query to get "Hoy no Circula" from SEMOVI

GraphQL Query to get "Hoy no Circula" from SEMOVI

<?php

require __DIR__ . '/vendor/autoload.php';

function strip_of($subject, $to_strip) {
    $search = [ $to_strip, ' ', "\t", "\n" ];
    return str_replace($search, ['','','',''], $subject);
}

use Goutte\Client;

$client = new Client();

$crawler = $client->request("GET", "http://semovi.cdmx.gob.mx");

$widget = $crawler->filter('#Level-1 .Widget-circulate--sm')->first();

$inclusive = $widget->filter('.Widget-box')->first()->extract('inclu-text')[0];

$date = trim($widget->filter('.Title h4')->first()->text());

$hoynocircula = $widget->filter('.Widget-child-item .Widget-label--lg')->first();
$hoynocircula = [
    'terminacion' => $hoynocircula->filter('strong')->text(),
    'color' => strip_of($hoynocircula->extract('class'), 'Widget-label--lg'),
];

$verifican = $widget->filter('.Widget-child-item')->last()->filter('.Widget-label-half')
	->each(function($node, $i){
             return [
                 'terminacion' => $node->filter('strong')->text(),
                 'color' => strip_of($node->filter('.Widget-label--lg')->extract('class'), 'Widget-label--lg'),
             ];
        });

$sabado = $widget->filter('.Widget-child-item')->eq(1)->filter('.Widget-label--lg')
    ->each(function($node, $i){return trim($node->text());});

echo json_encode(compact('inclusive', 'date', 'hoynocircula', 'verifican', 'sabado'), JSON_PRETTY_PRINT);
{
  page(url: "http://semovi.cdmx.gob.mx/") {
    query(selector: "#Level-1 .Widget-circulate--sm") {
      inclusive: query(selector: ".Widget-box") {
        text: attr(name: "inclu-text")
      }
      date: query(selector: ".Title:first h4") {
        text: content
      }
      hoynocircula: query(selector: ".Widget-child-item:first .Widget-label--lg") {
        terminacion: query(selector: "strong") {
          text
        }
        color: attr(name: "class")
      }
      verifican: query(selector: ".Widget-child-item:last .Widget-label-half") {
        terminacion: query(selector: "strong") {
          text
        }
        color: query(selector: ".Widget-label--lg") {
          attr(name: "class")
        }
      }
      sabado: query(selector: ".Widget-child-item:nth-child(2) .Widget-label--lg") {
        clave: text
      }
    }
  }
}
http://gdom.graphene-python.org/graphql?query={%0A page(url%3A "http%3A%2F%2Fsemovi.cdmx.gob.mx%2F") {%0A query(selector%3A "%23Level-1 .Widget-circulate--sm") {%0A inclusive%3A query(selector%3A ".Widget-box") {%0A text%3A attr(name%3A "inclu-text")%0A }%0A date%3A query(selector%3A ".Title%3Afirst h4") {%0A text%3A content%0A }%0A hoynocircula%3A query(selector%3A ".Widget-child-item%3Afirst .Widget-label--lg") {%0A terminacion%3A query(selector%3A "strong") {%0A text%0A }%0A color%3A attr(name%3A "class")%0A }%0A verifican%3A query(selector%3A ".Widget-child-item%3Alast .Widget-label-half") {%0A terminacion%3A query(selector%3A "strong") {%0A text%0A }%0A color%3A query(selector%3A ".Widget-label--lg") {%0A attr(name%3A "class")%0A }%0A }%0A sabado%3A query(selector%3A ".Widget-child-item%3Anth-child(2) .Widget-label--lg") {%0A clave%3A text%0A }%0A }%0A }%0A}%0A
{
  "data": {
    "page": {
      "query": [
        {
          "inclusive": [
            {
              "text": "Hoy Jueves 17 de agosto de 2017, no circulan vehículos con terminación 1 y 2. Este mes verifican vehículos con terminación 5 y 6 y con terminación 7 y 8."
            }
          ],
          "date": [
            {
              "text": "\n                        Jueves 17 de agosto de 2017\n                    "
            }
          ],
          "hoynocircula": [
            {
              "terminacion": [
                {
                  "text": "1 y 2"
                }
              ],
              "color": "Widget-label--lg Calc-verde"
            }
          ],
          "verifican": [
            {
              "terminacion": [
                {
                  "text": "5 y 6"
                }
              ],
              "color": [
                {
                  "attr": "Widget-label--lg Calc-amarilla"
                }
              ]
            },
            {
              "terminacion": [
                {
                  "text": "7 y 8"
                }
              ],
              "color": [
                {
                  "attr": "Widget-label--lg Calc-rosa"
                }
              ]
            }
          ],
          "sabado": [
            {
              "clave": "H1: Impar"
            },
            {
              "clave": "H2: Todos"
            }
          ]
        }
      ]
    }
  }
}