Exemplo n.º 1
0
 function getFares()
 {
     $this->first_timestamp = $this->getTimestampWithoutTime(time() + $this->days_offset * 24 * 60 * 60);
     $this->last_timestamp = $this->first_timestamp + $this->days_to_check * 24 * 60 * 60;
     $last_request_time = 0;
     echo "IMPORT SESSION ID: " . $this->session_id . $this->nl;
     echo "OFFSET DAYS: " . $this->days_offset . $this->nl;
     echo "DAYS TO CHECK: " . $this->days_to_check . $this->nl;
     echo "START DATE: " . date("d-m-Y", $this->first_timestamp) . $this->nl;
     echo "END DATE: " . date("d-m-Y", $this->last_timestamp) . $this->nl;
     echo "REMOVING OLD RAW FARES" . $this->nl;
     $this->truncateTable('ryan_raw');
     foreach ($this->trips as $trip) {
         $this->fares = array();
         $Origin = $trip[0];
         $Destination = $trip[1];
         echo "TRIP {$Origin} --> {$Destination} " . $this->nl;
         for ($i = $this->first_timestamp; $i <= $this->last_timestamp; $i = $i + $this->FlexDaysOut * 24 * 60 * 60) {
             echo "CURRENT DATE PROCESSING: " . date("d-m-Y", $i) . $this->nl;
             //GOOD BOY
             $time_since_last_request = microtime(TRUE) - $last_request_time;
             if ($time_since_last_request < $this->wait_before_next_page_request) {
                 usleep(($this->wait_before_next_page_request - $time_since_last_request) * 1000000);
             }
             $last_request_time = microtime(TRUE);
             $DateOut = date("Y-m-d", $i);
             $url = $this->url . "&Origin={$Origin}&Destination={$Destination}&DateOut={$DateOut}&FlexDaysOut={$this->FlexDaysOut}";
             $curl_obj = new curl();
             $curl_obj->setRandomUserAgent();
             $ret = $curl_obj->getContent($url);
             if ($ret['errno'] != 0) {
                 echo "AN ERROR HAS OCCURRED DURING CURL: " . $ret['errno'] . " - " . $ret['error'] . " - " . $url . $this->nl . $this->nl;
             }
             if (!$this->decodeJson($ret['content'])) {
                 echo "AN ERROR HAS OCCURRED DURING JSON DECODE: " . json_last_error() . " - " . json_last_error_msg() . $this->nl . $this->nl;
             }
             if (!$this->storeFaresInArray($ret['content'])) {
                 echo "AN ERROR HAS OCCURRED DURING FARES STORING: " . $this->last_error . $this->nl . $this->nl;
             }
         }
         //for each group of days until the end of the period
         $this->fares['metadata']['Origin'] = $Origin;
         $this->fares['metadata']['Destination'] = $Destination;
         $this->fares['metadata']['ts'] = time();
         echo "SAVING TRIP FARES TO DB" . $this->nl;
         $this->storeFaresInDB();
         echo "TRIP PROCESSED" . $this->nl;
         $this->fares = NULL;
         unset($this->fares);
     }
     //for each trip
     echo "UPDATING DEPARTURES INFORMATION" . $this->nl;
     $this->updateFaresDeparturesInfo();
     echo "UPDATING FARES TO NUMBERS" . $this->nl;
     $this->updateFaresValuesToNumbers();
     echo "UPDATING TIMESTAMPS" . $this->nl;
     $this->updateTimestamps();
     echo "REMOVING OLD TRIPS" . $this->nl;
     $this->removeOldTrips();
     echo "REMOVING OLD AGGREGATED DATA" . $this->nl;
     $this->truncateTable("ryan_data");
     echo "GENERATING NEW AGGREGATED DATA" . $this->nl;
     $this->aggregateData();
     echo "PROCESS COMPLETED" . $this->nl . $this->nl;
 }