Ejemplo n.º 1
0
def get_preference(pref, jpid):
    """For getting list of stops/addresses for a route"""

    if pref == "address":
        return Db().get_addresses(jpid)
    else:
        return Db().get_stop_id(jpid)
Ejemplo n.º 2
0
def get_distance_and_predict_with_model(jpid, source, destination, date_time):
    """Get travel time of route"""

    distances = Db().get_distance(jpid, source, destination)
    travel_times = get_travel_time(jpid, distances.loc[0, "Distance"],
                                   distances.loc[1, "Distance"], date_time[0])

    return travel_times
Ejemplo n.º 3
0
def find_time_bus_arrives(travel_times, date_time, jpid, source, destination):
    """Find the actual time the bus arrives"""

    time_to_source = travel_times[1]
    time_cat = get_time_cat(date_time[1])

    time_bus_arrives = Db().get_bus_time_for_map(str(jpid), int(source),
                                                 int(destination),
                                                 float(time_to_source),
                                                 str(time_cat))

    return time_bus_arrives
Ejemplo n.º 4
0
def possible_routes(srcLat, srcLon, destLat, destLon, searchPreference,
                    dateTime):
    """getting all possible routes, from best to worst from point A to point B"""

    dateTime = dateTime.split(",")

    routes = Db().get_best_route(srcLat, srcLon, destLat, destLon)
    try:
        best_routes = get_three_best_routes(routes, searchPreference, dateTime)
    except IndexError:
        best_routes = "No Journey Found"

    # Get the address for map display purposes
    try:
        for i in range(len(best_routes)):
            #address is a dataframe, hency the use of .loc
            address = Db().get_single_address(best_routes[i][2]).loc[0,
                                                                     "Address"]
            best_routes[i].append(address)
    except IndexError:
        # In case the source is outside Dublin
        best_routes = "No Journey Found"

    return json.dumps(best_routes, ensure_ascii=False)
Ejemplo n.º 5
0
def get_model_answer(jpid, source, destination, dateTime):
    """Get estimated travel time"""

    try:
        distances = Db().get_distance(jpid, source, destination)

    except:
        distances = 'NA'

    try:
        travel_time = get_travel_time(jpid, distances.loc[0, "Distance"],
                                      distances.loc[1, "Distance"], dateTime)

    except:
        travel_time = "Not Known"

    return json.dumps(travel_time)
def scrape_fare(jpid, stop1, stop2, direction):
    """Scrapes the current live price information from dublinbus.ie"""

    # Get line ID and stop numbers of those stops
    df = Db().get_stop_numbers(jpid, stop1, stop2)
    lineid = df.loc[0, "Line_ID"]

    # Convert upper cases to lower cases letter
    lineid = lineid.lower()

    stop_number1 = df.loc[0, "Stop_number"]
    stop_number1 = int(stop_number1) + 1
    stop_number2 = int(df.loc[1, "Stop_number"]) + 1

    try:
        # Change direction parsing for url but sometimes o and I are switched
        # Must account for this and try both ways
        if direction == '0' or direction == 0:
            direction = 'I'
        else:
            direction = 'O'

        article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=" + str(
            lineid) + "&direction=" + str(direction) + "&board=" + str(
                stop_number1) + "&alight=" + str(stop_number2)

        return json.dumps(get_prices(article_url))

    except:
        if direction == '0' or direction == 0:
            direction = 'O'
        else:
            direction = 'I'

        article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=" + str(
            lineid) + "&direction=" + str(direction) + "&board=" + str(
                stop_number1) + "&alight=" + str(stop_number2)

        return json.dumps(get_prices(article_url))
Ejemplo n.º 7
0
def retrieve_gps(jpid, srcStop, destStop):
    """Retrieves gps coordinates of a given journey pattern id"""

    return Db().get_gps(jpid, srcStop, destStop)
Ejemplo n.º 8
0
def get_start_end_addresses(lineId):
    """For getting list of Journey Pattern ID's at startup"""

    return Db().get_first_and_last_address(lineId)
Ejemplo n.º 9
0
def get_selected_timetable(lineId):
    """for getting the timetable for the selected route"""

    return Db().get_selected_route_timetable(lineId)
Ejemplo n.º 10
0
def get_routes_timetable():
    """For getting list of journey id's for the timetable"""

    return Db().get_line_ids()
Ejemplo n.º 11
0
def get_routes():
    """ For getting list of Journey Pattern ID's at startup """

    return Db().get_line_ids()
Ejemplo n.º 12
0
def get_bus_timetable(jpidTruncated, srcStop, destStop, hour, minute, sec,
                      sourceTime, timeCat):
    """Returns selected timetable Mon-Fri, Sat & Sun for selected route"""

    return Db().get_bus_time(jpidTruncated, srcStop, destStop, hour, minute,
                             sec, sourceTime, timeCat)
Ejemplo n.º 13
0
def get_three_routes_based_on_fare(data):
    """Return the three most inexpensive routes"""

    routes = list()

    for index, journey in data.items():
        fare = "3.10 Euros"
        jpid = journey['JPID_Source']
        stop1 = journey['STOP_ID_Source']
        stop2 = journey['Stop_ID_Destination']
        direction = jpid[4:5]

        try:
            # get lineid and stop numbers of those stops
            df = Db().get_stop_numbers(jpid, stop1, stop2)
            lineid = df.loc[0, "Line_ID"]

            # convert upper cases to lower cases letter
            lineid = lineid.lower()

            stop_number1 = df.loc[0, "Stop_number"]
            stop_number1 = int(stop_number1) + 1
            stop_number2 = int(df.loc[1, "Stop_number"]) + 1

            try:
                # change direction parsing for url but sometimes o and I are switched
                # must account for this and try both ways
                if direction == '0' or direction == 0:
                    direction = 'I'
                else:
                    direction = 'O'

                article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=" + str(
                    lineid) + "&direction=" + str(direction) + "&board=" + str(
                        stop_number1) + "&alight=" + str(stop_number2)

                fare = get_prices(article_url)["Adult Leap"]

            except Exception as e:
                if direction == '0' or direction == 0:
                    direction = 'O'
                else:
                    direction = 'I'

                article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=" + str(
                    lineid) + "&direction=" + str(direction) + "&board=" + str(
                        stop_number1) + "&alight=" + str(stop_number2)

                fare = get_prices(article_url)["Adult Leap"]

        except Exception as e:
            if stop_number2 - stop_number1 <= 10:
                article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=140&direction=I&board=0&alight=10"

            elif stop_number2 - stop_number1 <= 30:
                article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=140&direction=I&board=0&alight=31"

            else:
                article_url = "https://www.dublinbus.ie/Fare-Calculator/Fare-Calculator-Results/?routeNumber=140&direction=I&board=9&alight=46"
                fare = get_prices(article_url)["Adult Leap"]
            pass

        routes.append([fare, jpid, stop1, stop2])

    routes = sort_function(routes)  # Sort it by the next to arrive

    if len(routes) == 2:
        return [routes[0], routes[1]]
    elif len(routes) == 1:
        return [routes[0]]
    elif len(routes) == 0:
        return "No Journey Found"
    else:
        return [routes[0], routes[1], routes[2]]