<commit_before>#!/usr/bin/env python3
from models import Location, Stop, POI, Address
from models import TimeAndPlace, Platform, RealtimeTime
from models import Trip, Coordinates, TicketList, TicketData
from models import Ride, Line, LineType, LineTypes, Way, WayType
from datetime import datetime, timedelta
import xml.etree.ElementTree as ET
from .base import API
import requests
import re
import math


class EFA(API):
    name = 'efa'
    base_url = None
    country_by_id = ()
    ifopt_platforms = False
    ifopt_stopid_digits = False
    train_station_suffixes = {}
    train_station_lines = LineTypes(('train', 'urban'))

    def get_stop(self, stop: Stop, must_get_departures=False):
        assert isinstance(stop, Stop)
        return self.get_stop_rides(stop)

    def get_stop_rides(self, stop: Stop):
        return self._departure_monitor_request(stop)

    def search_trips(self, triprequest: Trip.Request):
        return self._trip_request(triprequest)

    def get_trip(self, trip: Stop):
        pass

    # Internal methods start here
    def _post(self, endpoint, data):
        text = requests.post(self.base_url + endpoint, data=data).text
        open('dump.xml', 'w').write(text)
        return ET.fromstring(text)

    def _convert_location(self, location: Location, wrap=''):
        """ Convert a Location into POST parameters for the EFA Requests """
        myid = self._my_data(location)

        city = location.city

        if location.name is None:
            if location.coords is not None:
                r = {'type': 'coord', 'name': '%.6f:%.6f:WGS84' % (reversed(location.coords))}
            else:
                r = {'type': 'stop', 'place': city, 'name': ''}
        elif isinstance(location, Stop):
            if myid is not None:
                r = {'type': 'stop', 'place': None, 'name': str(myid)}
            elif 'ifopt' in location._ids and None not in location._ids['ifopt'] and location.country is not None:
                r = {'type': 'stop', 'place': None, 'name': '%s:%s:%s' % ((location.country, ) + location._ids['ifopt'])}
            else:
                r = {'type': 'stop', 'place': city, 'name': location.name}
        elif isinstance(location, Address):
            r = {'type': 'address', 'place': city, 'name': location.name}
        elif isinstance(location, POI):
            if myid is not None:
                r = {'type': 'poiID', 'name': str(myid)}
            else:
                r = {'type': 'poi', 'place': city, 'name': location.name}
        elif isinstance(location, Location):
            r = {'type': 'any', 'place': city, 'name': location.name if location.name else None}
        else:
            raise NotImplementedError

        if r['place'] is None:
            del r['place']

        if wrap:
            r = {wrap % n: v for n, v in r.items()}

        return r

    def _trip_request(self, triprequest: Trip.Request):
        """ Searches connections/Trips; Returns a SearchResult(Trip) """
        now = datetime.now()

        assert triprequest.walk_speed in ('slow', 'normal', 'fast')

        linetypes = triprequest.linetypes
        if linetypes is None:
            linetypes = LineTypes()
        assert isinstance(linetypes, LineTypes)

        departure = triprequest.departure
        arrival = triprequest.arrival
        assert departure is None or isinstance(departure, RealtimeTime) or isinstance(departure, datetime)
        assert arrival is None or isinstance(arrival, RealtimeTime) or isinstance(arrival, datetime)

        if isinstance(departure, datetime):
            departure = RealtimeTime(departure)
        if isinstance(arrival, datetime):
            arrival = RealtimeTime(arrival)

        if departure is not None:
            deparr = 'dep'
            time_ = departure.livetime
        elif arrival is not None:
            deparr = 'arr'
            time_ = arrival.livetime
        else:
            deparr = 'dep'
            time_ = now

        max_changes = triprequest.max_changes
        if max_changes is None:
            max_changes = 9

        post = {
            'changeSpeed': triprequest.walk_speed,
            'command': '',
            'coordOutputFormat': 'WGS84',
            'imparedOptionsActive': 1,
            'includedMeans': 'checkbox',
            'itOptionsActive': 1,
            'itdDateDay': time_.day,
            'itdDateMonth': time_.month,
            'itdDateYear': time_.year,
            'itdTimeHour': time_.hour,
            'itdTimeMinute': time_.minute,
            'itdTripDateTimeDepArr': deparr,
            'language': 'de',
            'locationServerActive': 1,
            'maxChanges': max_changes,
            'name_via': '',  # .decode('utf-8').encode('iso-8859-1'),
            'nextDepsPerLeg': 1,
            'place_via': '',  # decode('utf-8').encode('iso-8859-1'),
            'ptOptionsActive': 1,
            'requestID': 0,
            'routeType': 'LEASTTIME',  # {'speed':'LEASTTIME', 'waittime':'LEASTINTERCHANGE', 'distance':'LEASTWALKING'}[select_interchange_by],
            'sessionID': 0,
            'type_via': 'stop',
            'useRealtime': 1,
            'outputFormat': 'XML',
            'debug': 1,
        }

        # if use_realtime: post['useRealtime'] = 1

        if 'train' in linetypes:
            post['inclMOT_0'] = 'on'

        if 'train.longdistance.highspeed' in linetypes:
            post['lineRestriction'] = 400
        elif 'train.longdistance' in linetypes:
            post['lineRestriction'] = 401
        else:
            post['lineRestriction'] = 403

        if 'urban' in linetypes:
            post['inclMOT_1'] = 'on'

        if 'metro' in linetypes:
            post['inclMOT_2'] = 'on'
            post['inclMOT_3'] = 'on'

        if 'tram' in linetypes:
            post['inclMOT_4'] = 'on'

        if 'bus.city' in linetypes:
            post['inclMOT_5'] = 'on'

        if 'bus.regional' in linetypes:
            post['inclMOT_6'] = 'on'

        if 'bus.express' in linetypes:
            post['inclMOT_7'] = 'on'

        if 'suspended' in linetypes:
            post['inclMOT_8'] = 'on'

        if 'ship' in linetypes:
            post['inclMOT_9'] = 'on'

        if 'dialable' in linetypes:
            post['inclMOT_10'] = 'on'

        if 'other' in linetypes:
            post['inclMOT_11'] = 'on'

        if triprequest.wayduration_origin or triprequest.wayduration_destination:
            post['useProxFootSearch'] = 1

        waytypes = {'walk': 100, 'bike': 104, 'car': 104, 'taxi': 105}
        post['trITDepMOT'] = waytypes[str(triprequest.waytype_origin)]
        post['trITArrMOT'] = waytypes[str(triprequest.waytype_destination)]

        post['trITDepMOTvalue%d' % post['trITDepMOT']] = triprequest.wayduration_origin.total_seconds() // 60
        post['trITArrMOTvalue%d' % post['trITArrMOT']] = triprequest.wayduration_destination.total_seconds() // 60

        if triprequest.with_bike:
            post['bikeTakeAlong'] = 1

        if triprequest.wheelchair:
            post['wheelchair'] = 1

        if triprequest.low_floor_only:<commit_msg>bike=101<commit_after>#!/usr/bin/env python3
from models import Location, Stop, POI, Address
from models import TimeAndPlace, Platform, RealtimeTime
from models import Trip, Coordinates, TicketList, TicketData
from models import Ride, Line, LineType, LineTypes, Way, WayType
from datetime import datetime, timedelta
import xml.etree.ElementTree as ET
from .base import API
import requests
import re
import math


class EFA(API):
    name = 'efa'
    base_url = None
    country_by_id = ()
    ifopt_platforms = False
    ifopt_stopid_digits = False
    train_station_suffixes = {}
    train_station_lines = LineTypes(('train', 'urban'))

    def get_stop(self, stop: Stop, must_get_departures=False):
        assert isinstance(stop, Stop)
        return self.get_stop_rides(stop)

    def get_stop_rides(self, stop: Stop):
        return self._departure_monitor_request(stop)

    def search_trips(self, triprequest: Trip.Request):
        return self._trip_request(triprequest)

    def get_trip(self, trip: Stop):
        pass

    # Internal methods start here
    def _post(self, endpoint, data):
        text = requests.post(self.base_url + endpoint, data=data).text
        open('dump.xml', 'w').write(text)
        return ET.fromstring(text)

    def _convert_location(self, location: Location, wrap=''):
        """ Convert a Location into POST parameters for the EFA Requests """
        myid = self._my_data(location)

        city = location.city

        if location.name is None:
            if location.coords is not None:
                r = {'type': 'coord', 'name': '%.6f:%.6f:WGS84' % (reversed(location.coords))}
            else:
                r = {'type': 'stop', 'place': city, 'name': ''}
        elif isinstance(location, Stop):
            if myid is not None:
                r = {'type': 'stop', 'place': None, 'name': str(myid)}
            elif 'ifopt' in location._ids and None not in location._ids['ifopt'] and location.country is not None:
                r = {'type': 'stop', 'place': None, 'name': '%s:%s:%s' % ((location.country, ) + location._ids['ifopt'])}
            else:
                r = {'type': 'stop', 'place': city, 'name': location.name}
        elif isinstance(location, Address):
            r = {'type': 'address', 'place': city, 'name': location.name}
        elif isinstance(location, POI):
            if myid is not None:
                r = {'type': 'poiID', 'name': str(myid)}
            else:
                r = {'type': 'poi', 'place': city, 'name': location.name}
        elif isinstance(location, Location):
            r = {'type': 'any', 'place': city, 'name': location.name if location.name else None}
        else:
            raise NotImplementedError

        if r['place'] is None:
            del r['place']

        if wrap:
            r = {wrap % n: v for n, v in r.items()}

        return r

    def _trip_request(self, triprequest: Trip.Request):
        """ Searches connections/Trips; Returns a SearchResult(Trip) """
        now = datetime.now()

        assert triprequest.walk_speed in ('slow', 'normal', 'fast')

        linetypes = triprequest.linetypes
        if linetypes is None:
            linetypes = LineTypes()
        assert isinstance(linetypes, LineTypes)

        departure = triprequest.departure
        arrival = triprequest.arrival
        assert departure is None or isinstance(departure, RealtimeTime) or isinstance(departure, datetime)
        assert arrival is None or isinstance(arrival, RealtimeTime) or isinstance(arrival, datetime)

        if isinstance(departure, datetime):
            departure = RealtimeTime(departure)
        if isinstance(arrival, datetime):
            arrival = RealtimeTime(arrival)

        if departure is not None:
            deparr = 'dep'
            time_ = departure.livetime
        elif arrival is not None:
            deparr = 'arr'
            time_ = arrival.livetime
        else:
            deparr = 'dep'
            time_ = now

        max_changes = triprequest.max_changes
        if max_changes is None:
            max_changes = 9

        post = {
            'changeSpeed': triprequest.walk_speed,
            'command': '',
            'coordOutputFormat': 'WGS84',
            'imparedOptionsActive': 1,
            'includedMeans': 'checkbox',
            'itOptionsActive': 1,
            'itdDateDay': time_.day,
            'itdDateMonth': time_.month,
            'itdDateYear': time_.year,
            'itdTimeHour': time_.hour,
            'itdTimeMinute': time_.minute,
            'itdTripDateTimeDepArr': deparr,
            'language': 'de',
            'locationServerActive': 1,
            'maxChanges': max_changes,
            'name_via': '',  # .decode('utf-8').encode('iso-8859-1'),
            'nextDepsPerLeg': 1,
            'place_via': '',  # decode('utf-8').encode('iso-8859-1'),
            'ptOptionsActive': 1,
            'requestID': 0,
            'routeType': 'LEASTTIME',  # {'speed':'LEASTTIME', 'waittime':'LEASTINTERCHANGE', 'distance':'LEASTWALKING'}[select_interchange_by],
            'sessionID': 0,
            'type_via': 'stop',
            'useRealtime': 1,
            'outputFormat': 'XML',
            'debug': 1,
        }

        # if use_realtime: post['useRealtime'] = 1

        if 'train' in linetypes:
            post['inclMOT_0'] = 'on'

        if 'train.longdistance.highspeed' in linetypes:
            post['lineRestriction'] = 400
        elif 'train.longdistance' in linetypes:
            post['lineRestriction'] = 401
        else:
            post['lineRestriction'] = 403

        if 'urban' in linetypes:
            post['inclMOT_1'] = 'on'

        if 'metro' in linetypes:
            post['inclMOT_2'] = 'on'
            post['inclMOT_3'] = 'on'

        if 'tram' in linetypes:
            post['inclMOT_4'] = 'on'

        if 'bus.city' in linetypes:
            post['inclMOT_5'] = 'on'

        if 'bus.regional' in linetypes:
            post['inclMOT_6'] = 'on'

        if 'bus.express' in linetypes:
            post['inclMOT_7'] = 'on'

        if 'suspended' in linetypes:
            post['inclMOT_8'] = 'on'

        if 'ship' in linetypes:
            post['inclMOT_9'] = 'on'

        if 'dialable' in linetypes:
            post['inclMOT_10'] = 'on'

        if 'other' in linetypes:
            post['inclMOT_11'] = 'on'

        if triprequest.wayduration_origin or triprequest.wayduration_destination:
            post['useProxFootSearch'] = 1

        waytypes = {'walk': 100, 'bike': 101, 'car': 104, 'taxi': 105}
        post['trITDepMOT'] = waytypes[str(triprequest.waytype_origin)]
        post['trITArrMOT'] = waytypes[str(triprequest.waytype_destination)]

        post['trITDepMOTvalue%d' % post['trITDepMOT']] = triprequest.wayduration_origin.total_seconds() // 60
        post['trITArrMOTvalue%d' % post['trITArrMOT']] = triprequest.wayduration_destination.total_seconds() // 60

        if triprequest.with_bike:
            post['bikeTakeAlong'] = 1

        if triprequest.wheelchair:
            post['wheelchair'] = 1

        if triprequest.low_floor_only:
{'fraction_non_alphanumeric': 0.12530678073066404, 'fraction_numerical': 0.01030783255031204, 'mean_word_length': 2.2245082523174315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2516797', 'n_tokens_mistral': 4482, 'n_tokens_neox': 4261, 'n_words': 1067}