<commit_before>
import urllib, zipfile, os, shutil, gtfs_parser, glob, uuid
from flask import jsonify, request
from app import app, db, models
from sets import Set
from datetime import datetime

def decode(url):
    return urllib.unquote(url).decode('utf8')

@app.route('/agency', methods=['GET'])
def get_agency():
    agencies = models.Agency.query.all()
    return jsonify({ 'agencies' : [a.serialize() for a in agencies] })

@app.route('/stops', methods=['GET'])
def get_stops():
    stops = None
    trip_id = decode(request.args.get('trip_id', ''))
    if len(trip_id) == 0:
        stops = models.Stop.query.all()
    else:
        trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
        if not trip is None:
            stops = trip.stops
        else:
            return jsonify({ '404' : 'No Stops Found' })
    return jsonify({ 'stops' : [s.serialize() for s in stops] })

@app.route('/routes', methods=['GET'])
def get_routes():
    routes = None
    valid_trips = None
    n = 1
    try:
        n = int(request.args.get('next', 1))
    except ValueError:
        return jsonify({ '404' : 'Cannot parse \'next\' parameter'}), 404
    if len(request.args.keys()) > 0:
        # filter routes by the provided URL parameters
        lat1 = request.args.get('lat1', 999)
        lon1 = request.args.get('lon1', 999)
        lat2 = request.args.get('lat2', 999)
        lon2 = request.args.get('lon2', 999)
        if lat1 == 999 or lon1 == 999 or lat2 == 999 or lon2 == 999:
            # the parameters provided cannot be used to filter, so return error
            return jsonify({ '404' : 'Bad URL Parameters'}), 404
        else:
            stop_times = []
            start = decode(request.args.get('start', ''))
            stop = decode(request.args.get('stop', ''))
            
            if len(stop) > 0 and len(start) == 0:
                # the parameters provided cannot be used to filter, so return error
                return jsonify({ '404' : 'Cannot have end time without start time'}), 404
            elif len(start) == 0 and len(stop) == 0:
                # filter by latitude and longitude only
                stop_times = models.StopTime.query.filter(models.StopTime.stop_lon >= lon1, models.StopTime.stop_lon <= lon2, models.StopTime.stop_lat >= lat1, models.StopTime.stop_lat <= lat2)
            else:
                start_time = None
                stop_time = None
                try:
                    start_time = gtfs_parser.datetime_from_string(start)
                    if len(stop) > 0:
                        stop_time = gtfs_parser.datetime_from_string(stop)
                except:
                    return jsonify({ '404' : 'Cannot parse time'}), 404
                if not stop_time is None:
                    # filter within a range of time
                    stop_times = models.StopTime.query.filter(models.StopTime.stop_lon >= lon1, models.StopTime.stop_lon <= lon2, models.StopTime.stop_lat >= lat1, models.StopTime.stop_lat <= lat2, models.StopTime.arrival_time >= start_time, models.StopTime.departure_time <= stop_time)
                else:
                    # filter from initial time only
                    stop_times = models.StopTime.query.filter(models.StopTime.stop_lon >= lon1, models.StopTime.stop_lon <= lon2, models.StopTime.stop_lat >= lat1, models.StopTime.stop_lat <= lat2, models.StopTime.arrival_time >= start_time)
                
            stop_times = array_from_query(stop_times)
            stop_times.sort(key = lambda st: st.arrival_time, reverse = False)
            trips = []
            for stop_time in stop_times:
                trips.append(stop_time.trip)
            trips = unique_array(trips)
            
            filtered_routes = Set()
            for trip in trips:
                filtered_routes.add(trip.route)
            routes = filtered_routes
            valid_trips = trips
    else:
        # otherwise, no URL parameters are provided, so return all routes
        routes = models.Route.query.all()
        
    return jsonify({ 'routes' : [r.serialize(valid_trips, n) for r in routes] })
def array_from_query(q):
    a = []
    for item in q:
        a.append(item)
    return a
def unique_array(regular_array): # Order preserving
  seen = set()
  return [x for x in regular_array if x not in seen and not seen.add(x)]
  
@app.route('/trips', methods=['GET'])
def get_trips():
    trips = models.Trip.query.all()
    return jsonify({ 'trips' : [t.serialize() for t in trips] })
    
@app.route('/experiences', methods=['GET'])
def get_experiences():
    experiences = None
    trip_id = decode(request.args.get('trip_id', ''))
    route_id = decode(request.args.get('route_id', ''))
    if len(trip_id) == 0 and len(route_id) == 0:
        experiences = models.Experience.query.all()
    elif len(route_id) == 0:
        # only trip_id provided
        trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
        if trip is None:
            return jsonify({ '404' : 'Invalid Trip ID' })
        experiences = models.Experience.query.filter(models.Experience.trip == trip)
    else:
        # route_id provided (will take precedence over trip_id if both are provided)
        route = models.Route.query.filter(models.Route.route_id == route_id).first()
        if route is None:
            return jsonify({ '404' : 'Invalid Route ID' })
        experiences = models.Experience.query.filter(models.Experience.route == route)
    return jsonify({ 'experiences' : [e.serialize() for e in experiences] })
    
@app.route('/experiences', methods=['POST'])
def create_experience():
    trip_id = decode(request.args.get('trip_id', ''))
    comment = decode(request.args.get('comment', ''))
    quality = request.args.get('quality', -1)
    open_seats = request.args.get('open_seats', -1)
    if len(trip_id) == 0:
        return jsonify({ '404' : 'Must provide a Trip ID for the experience' })
    trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
    if trip is None:
        return jsonify({ '404' : 'Invalid Trip ID' })
    experience = models.Experience(comment = comment, quality = quality, open_seats = open_seats, trip = trip, route = trip.route)
    try:
        db.session.add(experience)
        db.session.commit()
        experience.experience_id = str(experience.id) # the object's ID isn't set until it is added to the DB
        db.session.add(experience)
        db.session.commit()
        return jsonify(experience.serialize()), 200
    except Exception, e:
        return jsonify({ 'Error' : e.description })
    
@app.route('/locations', methods=['GET'])
def get_locations():
    locations = None
    trip_id = decode(request.args.get('trip_id', ''))
    route_id = decode(request.args.get('route_id', ''))
    if len(trip_id) == 0 and len(route_id) == 0:
        locations = models.Location.query.all()
    elif len(route_id) == 0:
        # only trip_id provided
        trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
        if trip is None:
            return jsonify({ '404' : 'Invalid Trip ID' })
        locations = models.Location.query.filter(models.Location.trip == trip)
    else:
        # route_id provided (will take precedence over trip_id if both are provided)
        route = models.Route.query.filter(models.Route.route_id == route_id).first()
        if route is None:
            return jsonify({ '404' : 'Invalid Route ID' })
        locations = models.Location.query.filter(models.Location.route == route)<commit_msg>more debugging...<commit_after>
import urllib, zipfile, os, shutil, gtfs_parser, glob, uuid
from flask import jsonify, request
from app import app, db, models
from sets import Set
from datetime import datetime

def decode(url):
    return urllib.unquote(url).decode('utf8')

@app.route('/agency', methods=['GET'])
def get_agency():
    agencies = models.Agency.query.all()
    return jsonify({ 'agencies' : [a.serialize() for a in agencies] })

@app.route('/stops', methods=['GET'])
def get_stops():
    stops = None
    trip_id = decode(request.args.get('trip_id', ''))
    if len(trip_id) == 0:
        stops = models.Stop.query.all()
    else:
        trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
        if not trip is None:
            stops = trip.stops
        else:
            return jsonify({ '404' : 'No Stops Found' })
    return jsonify({ 'stops' : [s.serialize() for s in stops] })

@app.route('/routes', methods=['GET'])
def get_routes():
    routes = None
    valid_trips = None
    n = 1
    try:
        n = int(request.args.get('next', 1))
    except ValueError:
        return jsonify({ '404' : 'Cannot parse \'next\' parameter'}), 404
    if len(request.args.keys()) > 0:
        # filter routes by the provided URL parameters
        lat1 = request.args.get('lat1', 999)
        lon1 = request.args.get('lon1', 999)
        lat2 = request.args.get('lat2', 999)
        lon2 = request.args.get('lon2', 999)
        if lat1 == 999 or lon1 == 999 or lat2 == 999 or lon2 == 999:
            # the parameters provided cannot be used to filter, so return error
            return jsonify({ '404' : 'Bad URL Parameters'}), 404
        else:
            stop_times = []
            start = decode(request.args.get('start', ''))
            stop = decode(request.args.get('stop', ''))
            
            if len(stop) > 0 and len(start) == 0:
                # the parameters provided cannot be used to filter, so return error
                return jsonify({ '404' : 'Cannot have end time without start time'}), 404
            elif len(start) == 0 and len(stop) == 0:
                # filter by latitude and longitude only
                stop_times = models.StopTime.query.filter(models.StopTime.stop_lon >= lon1, models.StopTime.stop_lon <= lon2, models.StopTime.stop_lat >= lat1, models.StopTime.stop_lat <= lat2)
            else:
                start_time = None
                stop_time = None
                try:
                    start_time = gtfs_parser.datetime_from_string(start)
                    if len(stop) > 0:
                        stop_time = gtfs_parser.datetime_from_string(stop)
                except:
                    return jsonify({ '404' : 'Cannot parse time'}), 404
                if not stop_time is None:
                    # filter within a range of time
                    stop_times = models.StopTime.query.filter(models.StopTime.stop_lon >= lon1, models.StopTime.stop_lon <= lon2, models.StopTime.stop_lat >= lat1, models.StopTime.stop_lat <= lat2, models.StopTime.arrival_time >= start_time, models.StopTime.departure_time <= stop_time)
                else:
                    # filter from initial time only
                    stop_times = models.StopTime.query.filter(models.StopTime.stop_lon >= lon1, models.StopTime.stop_lon <= lon2, models.StopTime.stop_lat >= lat1, models.StopTime.stop_lat <= lat2, models.StopTime.arrival_time >= start_time)
                
            stop_times = array_from_query(stop_times)
            stop_times.sort(key = lambda st: st.arrival_time, reverse = False)
            trips = []
            for stop_time in stop_times:
                trips.append(stop_time.trip)
            trips = unique_array(trips)
            
            filtered_routes = Set()
            for trip in trips:
                filtered_routes.add(trip.route)
            routes = filtered_routes
            valid_trips = trips
    else:
        # otherwise, no URL parameters are provided, so return all routes
        routes = models.Route.query.all()
        
    return jsonify({ 'routes' : [r.serialize(valid_trips, n) for r in routes] })
def array_from_query(q):
    a = []
    for item in q:
        a.append(item)
    return a
def unique_array(regular_array): # Order preserving
  seen = set()
  return [x for x in regular_array if x not in seen and not seen.add(x)]
  
@app.route('/trips', methods=['GET'])
def get_trips():
    trips = models.Trip.query.all()
    return jsonify({ 'trips' : [t.serialize() for t in trips] })
    
@app.route('/experiences', methods=['GET'])
def get_experiences():
    experiences = None
    trip_id = decode(request.args.get('trip_id', ''))
    route_id = decode(request.args.get('route_id', ''))
    if len(trip_id) == 0 and len(route_id) == 0:
        experiences = models.Experience.query.all()
    elif len(route_id) == 0:
        # only trip_id provided
        trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
        if trip is None:
            return jsonify({ '404' : 'Invalid Trip ID' })
        experiences = models.Experience.query.filter(models.Experience.trip == trip)
    else:
        # route_id provided (will take precedence over trip_id if both are provided)
        route = models.Route.query.filter(models.Route.route_id == route_id).first()
        if route is None:
            return jsonify({ '404' : 'Invalid Route ID' })
        experiences = models.Experience.query.filter(models.Experience.route == route)
    return jsonify({ 'experiences' : [e.serialize() for e in experiences] })
    
@app.route('/experiences', methods=['POST'])
def create_experience():
    trip_id = decode(request.args.get('trip_id', ''))
    comment = decode(request.args.get('comment', ''))
    quality = request.args.get('quality', -1)
    open_seats = request.args.get('open_seats', -1)
    if len(trip_id) == 0:
        return jsonify({ '404' : 'Must provide a Trip ID for the experience' })
    trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
    if trip is None:
        return jsonify({ '404' : 'Invalid Trip ID' })
    experience = models.Experience(comment = comment, quality = quality, open_seats = open_seats, trip = trip, route = trip.route)
    return jsonify({ 'Made it...' : 'to here' })
    db.session.add(experience)
    db.session.commit()
    experience.experience_id = str(experience.id) # the object's ID isn't set until it is added to the DB
    db.session.add(experience)
    db.session.commit()
    return jsonify(experience.serialize()), 200
    
@app.route('/locations', methods=['GET'])
def get_locations():
    locations = None
    trip_id = decode(request.args.get('trip_id', ''))
    route_id = decode(request.args.get('route_id', ''))
    if len(trip_id) == 0 and len(route_id) == 0:
        locations = models.Location.query.all()
    elif len(route_id) == 0:
        # only trip_id provided
        trip = models.Trip.query.filter(models.Trip.trip_id == trip_id).first()
        if trip is None:
            return jsonify({ '404' : 'Invalid Trip ID' })
        locations = models.Location.query.filter(models.Location.trip == trip)
    else:
        # route_id provided (will take precedence over trip_id if both are provided)
        route = models.Route.query.filter(models.Route.route_id == route_id).first()
        if route is None:
            return jsonify({ '404' : 'Invalid Route ID' })
        locations = models.Location.query.filter(models.Location.route == route)
{'fraction_non_alphanumeric': 0.12727633931527713, 'fraction_numerical': 0.015230779418581551, 'mean_word_length': 2.4821305049573437, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1815786', 'n_tokens_mistral': 4748, 'n_tokens_neox': 4512, 'n_words': 1200}