|
| 1 | +# Copyright (c) 2001-2024, Hove and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This file is part of Navitia, |
| 4 | +# the software to build cool stuff with public transport. |
| 5 | +# |
| 6 | +# Hope you'll enjoy and contribute to this project, |
| 7 | +# powered by Hove (www.hove.com). |
| 8 | +# Help us simplify mobility and open public transport: |
| 9 | +# a non ending quest to the responsive locomotion way of traveling! |
| 10 | +# |
| 11 | +# LICENCE: This program is free software; you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU Affero General Public License as published by |
| 13 | +# the Free Software Foundation, either version 3 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU Affero General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU Affero General Public License |
| 22 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +# |
| 24 | +# Stay tuned using |
| 25 | +# twitter @navitia |
| 26 | +# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org |
| 27 | +# https://groups.google.com/d/forum/navitia |
| 28 | +# www.navitia.io |
| 29 | + |
| 30 | +from __future__ import absolute_import, print_function, unicode_literals, division |
| 31 | +import boto3 |
| 32 | +from botocore.client import Config |
| 33 | +import json |
| 34 | +import logging |
| 35 | +from jormungandr.interfaces.v1.StatedResource import StatedResource |
| 36 | +from jormungandr.interfaces.v1.make_links import create_external_link |
| 37 | +from jormungandr import i_manager, app |
| 38 | + |
| 39 | + |
| 40 | +class OpgExcludedZones(StatedResource): |
| 41 | + def __init__(self, *args, **kwargs): |
| 42 | + super().__init__(*args, **kwargs) |
| 43 | + parser_get = self.parsers["get"] |
| 44 | + parser_get.add_argument("mode", type=str, default=None, help="A geojson in polyline path as base64") |
| 45 | + parser_get.add_argument("datetime", type=str, default=None, help="Distance range of the query in meters") |
| 46 | + |
| 47 | + def get_poi_place(self, instance, json_data): |
| 48 | + place = {"embedded_type": "poi", "poi": json_data, "name": json_data.get("poi")} |
| 49 | + |
| 50 | + place["poi"]["label"] = json_data.get("poi") |
| 51 | + place["poi"]["id"] = json_data.get("poi") |
| 52 | + place["poi"]["poi_type"] = { |
| 53 | + "id": instance.olympics_forbidden_uris.poi_property_key |
| 54 | + if instance.olympics_forbidden_uris |
| 55 | + else "poi_type:site_jo2024", |
| 56 | + "name": instance.olympics_forbidden_uris.poi_property_value |
| 57 | + if instance.olympics_forbidden_uris |
| 58 | + else "Site Olympique JO2024", |
| 59 | + } |
| 60 | + return place |
| 61 | + |
| 62 | + def add_link(self, places): |
| 63 | + if not places: |
| 64 | + return places |
| 65 | + args = {'_type': 'poi', 'id': "{poi.id}", 'region': 'idfm-jo', 'rel': 'pois', 'templated': True} |
| 66 | + link = create_external_link(url='v1.pois.id', **args) |
| 67 | + # ugly hack... |
| 68 | + link["href"] = link["href"].replace("%7B", "{") |
| 69 | + link["href"] = link["href"].replace("%7D", "}") |
| 70 | + return {"places": places, "links": [link]} |
| 71 | + |
| 72 | + def fetch_and_get_data(self, instance, bucket_name, folder, mode=None): |
| 73 | + if not bucket_name: |
| 74 | + return {} |
| 75 | + places = [] |
| 76 | + logger = logging.getLogger(__name__) |
| 77 | + args = {"connect_timeout": 2, "read_timeout": 2, "retries": {'max_attempts': 0}} |
| 78 | + s3_resource = boto3.resource('s3', config=Config(**args)) |
| 79 | + try: |
| 80 | + my_bucket = s3_resource.Bucket(bucket_name) |
| 81 | + for obj in my_bucket.objects.filter(Prefix="{}/".format(folder)): |
| 82 | + if not obj.key.endswith('.json'): |
| 83 | + continue |
| 84 | + try: |
| 85 | + file_content = obj.get()['Body'].read().decode('utf-8') |
| 86 | + json_content = json.loads(file_content) |
| 87 | + if json_content.get('instance') != instance.name: |
| 88 | + continue |
| 89 | + if mode is not None: |
| 90 | + if mode not in json_content.get("modes"): |
| 91 | + continue |
| 92 | + place = self.get_poi_place(instance, json_content) |
| 93 | + places.append(place) |
| 94 | + |
| 95 | + except Exception: |
| 96 | + logger.exception("Error on OpgExcludedZones") |
| 97 | + continue |
| 98 | + except Exception: |
| 99 | + logger.exception("Error on OpgExcludedZones") |
| 100 | + return {} |
| 101 | + return self.add_link(places) |
| 102 | + |
| 103 | + def get(self, region=None, lon=None, lat=None): |
| 104 | + args = self.parsers["get"].parse_args() |
| 105 | + region_str = i_manager.get_region(region, lon, lat) |
| 106 | + instance = i_manager.instances[region_str] |
| 107 | + |
| 108 | + return ( |
| 109 | + self.fetch_and_get_data( |
| 110 | + instance=instance, |
| 111 | + bucket_name=app.config.get(str("ASGARD_S3_BUCKET")), |
| 112 | + folder="excluded_zones", |
| 113 | + mode=args['mode'], |
| 114 | + ), |
| 115 | + 200, |
| 116 | + ) |
0 commit comments