Example #1
0
def get_stop_data(debug=False) -> List[dict]:
    """
    Get the stop data of de lijn
    :param debug: if true, get data from txt file instead
    :return: list of all stops
    """
    if debug:
        result = list()
        d = dict()
        with open("./project/delijn/dummy-stops.txt") as f:
            for line in f:
                line = line.strip()
                if '{' == line:
                    d.clear()
                elif '}' == line:
                    result.append(d.copy())
                else:
                    contents = line.split(' ', 1)
                    d[''.join(contents[0].split())] = try_convert(contents[1])

        return result
    try:
        raw_data, status = make_lijn_request("GET",
                                             "DLKernOpenData/api/v1/haltes")
        raw_data = raw_data.get("haltes")
        result = list()
        for raw_stop in raw_data:
            stop = format_stop(raw_stop)
            if stop:
                result.append(stop)
        return result
    except Exception as e:
        raise e
Example #2
0
    def get(self, region):
        """
        Get all the stops in a specified region
        :param region: region
        :return: list of stops that are in the specified region
        :exception: exceptions are thrown if region could not be converted to Region enum value
        """
        region = try_convert(region)
        if type(region) is int:
            try:
                region = Region(region)
                if region is None:
                    raise ValueError("Cannot convert int to Region")
            except Exception as e:
                return create_error(
                    500,
                    "Region {} does not exist. For possible values make http request to /regions/values"
                    .format(region),
                    extra=e.__str__()), 500
        else:
            try:
                region = Region[region.upper()]
                if region is None:
                    raise ValueError("Cannot convert string to Region")
            except Exception as e:
                return create_error(
                    500,
                    "Region {} does not exist. For possible values, make http request to /regions/values"
                    .format(region),
                    extra=e.__str__()), 500

        stops = Stop.query.filter_by(region=region).all()
        return [s.serialize() for s in stops], 200
Example #3
0
 def get(self, c_id):
     """
     Gets all ratings created by a particular creator
     :param c_id: ID of the creator
     :return: list of ratings of the creator
     """
     c_id = try_convert(c_id)
     if type(c_id) is not int:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(c_id)), 500
     ratings = StopRating.query.filter_by(created_by=c_id).all()
     return [r.serialize() for r in ratings]
Example #4
0
 def get(self, s_id):
     """
     Get the ratings of the stop with a given id
     :param s_id: id of the stop
     :return: list of ratings
     """
     s_id = try_convert(s_id)
     if type(s_id) is not int:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(s_id)), 500
     ratings = StopRating.query.filter_by(stop_id=s_id).all()
     return [r.serialize() for r in ratings], 200
Example #5
0
def get_delijn_stopNumberByLine(region, line):
    """
    Makes a request to the API of de lijn to get all stops based on the line
    :param region: region of the line
    :param line: line number of the line
    :return: list of unformatted stops
    """
    try:
        region = region.value
        region = try_convert(region)
        line = try_convert(line)
        if type(region) is str:
            region = Region[region.upper()]
        raw_data, status = make_lijn_request("GET",
                                             "/DLKernOpenData/api/v1/lijnen/{}/{}/lijnrichtingen/HEEN/haltes".format(
                                                 str(region), str(line)))
        result = list()
        raw_data = raw_data.get("haltes")
        for data in raw_data:
            result.append(int(data.get("haltenummer")))
            pass
        return result
    except Exception as e:
        return []
Example #6
0
def region_type(value):
    """
    Convert a value to a Region type
    :param value: value to convert to Region type
    :return: Region type of the value
    :exception: ValueError if value could not be converted
    """
    region = try_convert(value)
    if type(region) is int:
        region = Region(region)
    else:
        region = Region[region]
    if region is None:
        raise ValueError("Cannot convert '{}' to region".format(value))
    return region
Example #7
0
 def get(self, s_id):
     """
     Gets the average rating for a stop
     :param s_id: id of the stop
     :return: "No ratings yet" if there are no ratings, else it returns the average of the ratings
     """
     s_id = try_convert(s_id)
     if type(s_id) is not int:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(s_id)), 500
     average = db.session.query(func.avg(
         StopRating.rating)).filter_by(stop_id=s_id).scalar()
     if not average:
         return "No ratings yet"
     return float(average), 200
Example #8
0
def vehicle_type(value: Any) -> VehicleType:
    """
    Tries to convert the value to a VehicleType
    :param value: value to convert
    :return: VehicleType object
    """
    import click
    click.echo("Trying to convert this shit" + value)
    vehicle = try_convert(value)
    if type(vehicle) is int:
        vehicle = VehicleType(vehicle)
    else:
        vehicle = VehicleType[vehicle.upper()]
    if vehicle is None:
        click.echo("Hmm, it is none")
        raise ValueError("Cannot convert '{}' to vehicle type".format(value))
    return vehicle
Example #9
0
def get_vehicle_data() -> List[dict]:
    """
    Read vehicle dummy data
    :return: contents of dummy data file as list of objects
    """
    result = list()
    with open('./project/delijn/dummy-vehicles.txt') as f:
        d = dict()
        for line in f:
            line = line.strip()
            if line == '{':
                d.clear()
            elif line == '}':
                result.append(d.copy())
            else:
                contents = line.split(' ', 1)
                d[''.join(contents[0].split())] = try_convert(contents[1])
    return result
Example #10
0
 def get(self, region, line_number):
     """
     Get the stops of a given line number
     :param region: region of the line
     :param line_number: line number of the line
     :return: list of stops that are on the given line
     """
     from project.delijn.stops_by_line import get_delijn_stopNumberByLine
     import click
     region = try_convert(region)
     if type(region) is int:
         region = Region(region)
     if type(region) is str:
         region = Region[region.upper()]
     stop_numbers = get_delijn_stopNumberByLine(region, line_number)
     result = list()
     for number in stop_numbers:
         stop = Stop.query.filter_by(stop_number=number,
                                     region=region).first()
         if stop:
             result.append(stop)
     if len(result) == 0:
         return [], 200
     return [s.serialize() for s in result], 200