def get_region(stations): get_region_url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/regione/{}' pages = [{'url': get_region_url.format(stations[s]['id'])} for s in stations] logging.info("Going to make %s requests", len(pages)) regions = call_urls(pages) for item in regions: if len(item['content'].decode("utf-8"))>0: stations[item['url'].split('/')[-1]]['region'] = item['content'].decode("utf-8") return stations
def get_coordinates(stations): get_coord_url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/dettaglioStazione/{}/{}' pages = [{'url': get_coord_url.format(stations[s]['id'], stations[s]['region'])} for s in stations if stations[s].get('region') is not None] logging.info("Going to make %s requests", len(pages)) coords = call_urls(pages) for item in coords: if len(item['content'].decode("utf-8"))>0: stations[item['url'].split('/')[-2]].update(json.loads(item['content'].decode("utf-8"))) return stations
def get_stations(): get_station_url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/cercaStazione/{}' pages = [{'url': get_station_url.format(l)} for l in 'ABCDEFGHILMNOPQRSTUV'] stations = call_urls(pages) stations = [json.loads(item['content'].decode("utf-8")) for item in stations if len(item['content'].decode("utf-8"))>0] result = {} for item in stations: for subitem in item: result[subitem['id']]=subitem return result
def get_position(stations, more=''): get_coord_url = 'http://www.datasciencetoolkit.org/maps/api/geocode/json?address={}' + more + '&?id{}' pages = [{'url': get_coord_url.format(stations[s]['nomeLungo'], stations[s]['id'])} for s in stations if stations[s].get('lat') is None] logging.info("Going to make %s requests", len(pages)) position = call_urls(pages) for item in position: content = json.loads(item['content'].decode("utf-8")) if len(content)>0 and content['status']=='OK': id = item['url'].split('?')[-1].replace('id','') stations[id]['lon'] = content['results'][0]['geometry']['location']['lng'] stations[id]['lat'] = content['results'][0]['geometry']['location']['lat'] return stations
def get_starting_station(train_list): get_starting_station_url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/cercaNumeroTrenoTrenoAutocomplete/{}' pages = [{'url': get_starting_station_url.format(n)} for n in train_list] starting_stations = call_urls(pages) starting_stations = ''.join([ item['content'].decode("utf-8") for item in starting_stations if len(item['content'].decode("utf-8")) > 0 ]) starting_stations = starting_stations[:-1] return starting_stations
def get_train_status_from_API(station_id_list): get_train_status_url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/andamentoTreno/{}/{}' logging.info("Processing %s trains/stations", len(station_id_list)) pages = [{ 'url': get_train_status_url.format(item[0], item[1]) } for item in station_id_list] train_status = call_urls(pages) train_status = [ item['content'].decode("utf-8") for item in train_status if len(item['content'].decode("utf-8")) > 0 ] logging.info("Returning from API the status of %s trains", len(train_status)) return (train_status)