private function getNationalData($stop) { $stopData = Stop::where('id', '=', $stop)->get(); if (!$stopData->isEmpty()) { $url = 'http://nextbus.mxdata.co.uk/nextbuses/beta'; $date = date("Y-m-d\\TH:i:s\\Z"); $id = rand(100000, 999999); $request = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Siri version="1.0" xmlns="http://www.siri.org.uk/"> <ServiceRequest> <RequestTimestamp>' . $date . '</RequestTimestamp> <RequestorRef>' . Config::get('traveline.username') . '</RequestorRef> <StopMonitoringRequest version="1.0"> <RequestTimestamp>' . $date . '</RequestTimestamp> <MessageIdentifier>' . $id . '</MessageIdentifier> <MonitoringRef>' . $stop . '</MonitoringRef> </StopMonitoringRequest> </ServiceRequest> </Siri>'; $response = Httpful::post($url)->body($request)->authenticateWith(Config::get('traveline.username'), Config::get('traveline.password'))->sendsXml()->expectsXml()->send(); $timetable = $response->body->ServiceDelivery->StopMonitoringDelivery; $standardTimetable = array(); foreach ($timetable->MonitoredStopVisit as $trip) { if (isset($trip->MonitoredVehicleJourney->MonitoredCall->ExpectedDepartureTime)) { $standardTimetable[] = ['BusName' => (string) $trip->MonitoredVehicleJourney->PublishedLineName, 'BusHeading' => (string) $trip->MonitoredVehicleJourney->DirectionName, 'ArrivalTime' => strtotime((string) $trip->MonitoredVehicleJourney->MonitoredCall->ExpectedDepartureTime)]; } else { $standardTimetable[] = ['BusName' => (string) $trip->MonitoredVehicleJourney->PublishedLineName, 'BusHeading' => (string) $trip->MonitoredVehicleJourney->DirectionName, 'ArrivalTime' => strtotime((string) $trip->MonitoredVehicleJourney->MonitoredCall->AimedDepartureTime)]; } } if (isset($standardTimetable[0]['ArrivalTime'])) { if ($standardTimetable[0]['ArrivalTime'] - time() > 0) { //cover for errors in data or redis gets angry at a negative expiry Redis::setex($stop, $standardTimetable[0]['ArrivalTime'] - time(), json_encode($standardTimetable)); } return $standardTimetable; } else { return FALSE; } } else { return FALSE; } }
/** * Fill db with the cached and curled $stops * * @var $stops array */ public function fill_db($stops) { set_time_limit(9000); ini_set('memory_limit', '2048M'); foreach ($stops as $stop => $buses) { if ($o_stop = Stop::where('name', $stop)->first()) { if (!is_array($buses)) { $o_stop->delete(); } $db_buses = $o_stop->buses; $compare_arr = array(); foreach ($db_buses as $bus) { $compare_arr[$bus->number] = array(); foreach ($bus->timetables as $timetable) { $compare_arr[$bus->number][] = $timetable->time; } } foreach ($buses as $name => $bus) { if (!isset($compare_arr[$name])) { $this->resync($o_stop, $stop, $buses); break; } elseif ($this->timetables_dont_match($compare_arr[$name], $bus)) { $this->resync($o_stop, $stop, $buses); break; } else { // if nothing was changed, continue with synchronization continue; } } } else { $n_stop = $this->create_stop($stop); foreach ($buses as $b_index => $bus) { $n_bus = $this->create_bus($bus, $n_stop, $b_index); if (isset($bus['working_days'])) { foreach ($bus['working_days'] as $t_index => $time) { $this->create_timetable('working_day', $time, $n_bus); } } if (isset($bus['sunday'])) { foreach ($bus['sunday'] as $t_index => $time) { $this->create_timetable('sunday', $time, $n_bus); } } if (isset($bus['holiday'])) { foreach ($bus['holiday'] as $t_index => $time) { $this->create_timetable('holiday', $time, $n_bus); } } } } } }