<commit_before>from flask import Flask, request, redirect
import twilio.twiml
import googlemaps, requests, os

app = Flask(__name__)

gmaps = googlemaps.Client(key=os.environ['GOOGLEMAPSKEY'])

@app.route("/", methods=['GET', 'POST'])
def lookup():
    message_body = request.values.get('Body')

    print(message_body)

    geocode_result = gmaps.geocode(message_body) #TODO: Auto retries but needs something to catch errors

    if (len(geocode_result) == 0):
        message_response = "This is not a valid address. Please try again and make sure to send an address including a street number, city, and state."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)
    elif (len(geocode_result) > 1):
        message_response = "There are multiple address results for this address. Please try again and make sure to send an address including a street number, city, and state."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)

    if (geocode_result[0]['types'] != ['street_address']):
        message_response = "Sometimes a city or zip code has multiple districts. Please try again and make sure to send an address including a street number, city, and state."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)

    lat = str(geocode_result[0]['geometry']['location']['lat'])
    lng = str(geocode_result[0]['geometry']['location']['lng'])

    try:
        national = requests.get("https://congress.api.sunlightfoundation.com/legislators/locate?latitude=" + lat + "&longitude=" + lng)
        national.raise_for_status()
        national_json = national.json()
    except requests.exceptions.RequestException as e: 
        print(e)
        message_response = "This is awkward. There was an error. Please enter your address again."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)

    try:
        state = requests.get("https://openstates.org/api/v1/legislators/geo/?lat=" + lat +"&long=" + lng)
        state.raise_for_status()
        state_json = state.json()    
    except requests.exceptions.RequestException as e: 
        print(e)
        message_response = "This is awkward. There was an error. Please enter your address again."
        resp = twilio.twiml.Response()<commit_msg>make requests better<commit_after>from flask import Flask, request, redirect
import twilio.twiml
import googlemaps, requests, os

app = Flask(__name__)

gmaps = googlemaps.Client(key=os.environ['GOOGLEMAPSKEY'])

@app.route("/", methods=['GET', 'POST'])
def lookup():
    message_body = request.values.get('Body')

    print(message_body)

    geocode_result = gmaps.geocode(message_body) #TODO: Auto retries but needs something to catch errors

    if (len(geocode_result) == 0):
        message_response = "This is not a valid address. Please try again and make sure to send an address including a street number, city, and state."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)
    elif (len(geocode_result) > 1):
        message_response = "There are multiple address results for this address. Please try again and make sure to send an address including a street number, city, and state."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)

    if (geocode_result[0]['types'] != ['street_address']):
        message_response = "Sometimes a city or zip code has multiple districts. Please try again and make sure to send an address including a street number, city, and state."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)

    lat = str(geocode_result[0]['geometry']['location']['lat'])
    lng = str(geocode_result[0]['geometry']['location']['lng'])

    try:
        url = "https://congress.api.sunlightfoundation.com/legislators/locate"
        payload = {"latitude" : lat, "longitude" : lng}
        national = requests.get(url, payload)
        national.raise_for_status()
        national_json = national.json()
    except requests.exceptions.RequestException as e: 
        print(e)
        message_response = "This is awkward. There was an error. Please enter your address again."
        resp = twilio.twiml.Response()
        resp.message(message_response)
        return str(resp)

    try:
        url = "https://openstates.org/api/v1/legislators/geo/"
        payload = {"lat" : lat, "long" : lng}
        state = requests.get(url, payload)
        state.raise_for_status()
        state_json = state.json()    
    except requests.exceptions.RequestException as e: 
        print(e)
        message_response = "This is awkward. There was an error. Please enter your address again."
        resp = twilio.twiml.Response()
{'fraction_non_alphanumeric': 0.11047696697920913, 'fraction_numerical': 0.0024459845087647777, 'mean_word_length': 3.2084048027444254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '1800635', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1400, 'n_words': 413}