From d87cf076349d354b829bfe896ef11247b1182dd9 Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Sat, 04 May 2019 12:46:08 +0000 Subject: [PATCH] Use number of matched query chars when ordering stop search results --- stops/download_stops.php | 49 +++++++++++++++++++++++-------------------------- 1 files changed, 23 insertions(+), 26 deletions(-) diff --git a/stops/download_stops.php b/stops/download_stops.php index ddce8b9..4c689b6 100644 --- a/stops/download_stops.php +++ b/stops/download_stops.php @@ -1,33 +1,30 @@ <?php if(php_sapi_name() !== 'cli') die(); -$chars = 'aąbcćdeęfghijklłmnńoóprsśtuvwxyzżź0123456789'; -$len = mb_strlen($chars, 'UTF-8'); +function fetchStops($url) { + $json = file_get_contents($url); + $elements = json_decode($json, 1); + $stops = []; + foreach($elements['stops'] as $element) { + if($element['category'] == 'other') continue; + $stops[$element['shortName']] = $element['name']; + } + asort($stops); + return $stops; +} -$replacements = [ - 'Ó' => 'Ó', - 'ó' => 'ó', - 'É' => 'É', - 'é' => 'é', -]; - -$stops = []; -for($i = 0; $i < $len; $i++) { - for($j = 0; $j < $len; $j++) { - $char = mb_substr($chars, $i, 1, 'UTF-8').mb_substr($chars, $j, 1, 'UTF-8'); - $json = file_get_contents('http://www.ttss.krakow.pl/internetservice/services/lookup/autocomplete/json?query='.urlencode($char)); - $elements = json_decode($json, 1); - foreach($elements as $element) { - if($element['type'] == 'divider') continue; - if($element['type'] == 'route') continue; - if($element['type'] != 'stop') { - throw new Exception('Unknown element: '.var_export($element, 1)); - } - - $stops[$element['id']] = strtr($element['name'], $replacements); - } +function printStops($stops, $prefix) { + foreach($stops as $id => $stop) { + echo ' \''.$prefix.str_pad($id."'", 5, ' ', STR_PAD_RIGHT).' => '.var_export($stop, TRUE).','."\n"; } } -asort($stops); -var_export($stops); +$stops_tram = fetchStops('http://www.ttss.krakow.pl/internetservice/geoserviceDispatcher/services/stopinfo/stops?left=-648000000&bottom=-324000000&right=648000000&top=324000000'); +$stops_bus = fetchStops('http://91.223.13.70/internetservice/geoserviceDispatcher/services/stopinfo/stops?left=-648000000&bottom=-324000000&right=648000000&top=324000000'); + +echo '<?php'."\n"; +echo '$stops = array ('."\n"; +printStops($stops_tram, 't'); +echo "\n"; +printStops($stops_bus, 'b'); +echo ');'."\n"; -- Gitblit v1.9.1