<commit_before>import re
import requests
import json
import praw
from datetime import datetime

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

from twilio import twiml
from twilio.util import TwilioCapability

# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')

API_KEY = 'fc51e71739c072154f4f8d58ed4f9ec0770aee76'

# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = twiml.Response()
    response.say("Supported services: Everyblock, News, Maps. Version 1.1")
    return str(response)

# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    response = twiml.Response()
    body = request.form['Body'].lower()

    def news():
        '''Returns top 3 hot news stories fetched from news subreddit.'''
        r = praw.Reddit(user_agent='news_reader_textfeed')
        submissions = r.get_subreddit('news').get_hot(limit=3)
        return '. '.join(x.title for x in submissions)

    def everyblock(body_split):
        '''Returns everyblock crime news feed from Everyblock API.'''
        # print(body_split)
        def find_metro(s):
            # print(s)
            if s.isdigit():
                for met in ('philly', 'denver', 'houston', 'boston', 'chicago'):
                    everyblock_url = 'https://api.everyblock.com/content/{}/zipcodes'.format(met)
                    r = requests.get(everyblock_url, headers={'Authorization': 'Token {}'.format(API_KEY)}) # want to keep key private!!!
                    data = json.loads(r.text)
                    print(data)
                    for i in data:
                        if s == i['name']:
                            return met, s
            else:
                for met in ('philly', 'denver', 'houston', 'boston', 'chicago'):
                    everyblock_url = 'https://api.everyblock.com/content/{}/neighborhoods'.format(met)
                    r = requests.get(everyblock_url, headers={'Authorization': 'Token {}'.format(API_KEY)}) # want to keep key private!!!
                    data = json.loads(r.text)
                    print(s)
                    for i in data:
                        if s.title() == i['name']:
                            return met, s.lower().replace(' ', '-')
            print('not found error')
            return 'not found', 'error'
        
        metro, location = find_metro(' '.join(body_split[1:]))
        everyblock_url = 'https://api.everyblock.com/content/{}/locations/{}/timeline/?schema=crime'.format(metro, location)
        r = requests.get(everyblock_url, headers = {'Authorization': 'Token {}'.format(API_KEY)})
        data = json.loads(r.text)
        if not r or data['count'] == 0:
            everyblock_url = 'https://api.everyblock.com/content/{}/locations/{}/timeline/?schema=crime-reports'.format(metro, location)
            r = requests.get(everyblock_url, headers = {'Authorization': 'Token {}'.format(API_KEY)})
            data = json.loads(r.text)
        
        count = 0
        if 'count' in data or data['count'] != 0:
            output = ''
        #print data
        for event in data['results']:
            date = event['item_date']
            date = date[5:7]+'/'+date[8:10]+'/'+date[2:4]
            timetype = ''
            for i in ('dispatch_time', 'crime_time', 'occurrence_time', 'offense_time'):
                if i in event['attributes']:
                    timetype = i
                    break
            time = datetime.strptime(event['attributes'][timetype][:5], '%H:%M').strftime('%I:%M %p')
            output += '{event} on {location} at {date}, {time}\n'.format(event=event['title'], location=event['location_name'], date=date, time=time)
            count += 1
            if count == data['count'] or count == 2:
                break
        return output

    body_split = body.lower().split()
    if body_split[0] == 'directions':
        # TO-DO: Maps
        pass
    elif body_split[0] == 'news':
        output = news()
        response.sms(output)
    elif body_split[0] == 'everyblock':
        output = everyblock(body_split)
        response.sms(output)
    else:
        response.sms('Text options:\n"everyblock [zipcode or town]": crime news feed for your area\n"news": hottest news stories')
    return str(response)

<commit_msg>error handling<commit_after>import re
import requests
import json
import os
import praw
from datetime import datetime

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

from twilio import twiml
from twilio.util import TwilioCapability

# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')

API_KEY = os.environ.get('EVERYBLOCK_KEY', None)

# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = twiml.Response()
    response.say("Supported services: Everyblock, News, Maps. Version 1.1")
    return str(response)

# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    response = twiml.Response()
    body = request.form['Body'].lower()

    def news():
        '''Returns top 3 hot news stories fetched from news subreddit.'''
        r = praw.Reddit(user_agent='news_reader_textfeed')
        submissions = r.get_subreddit('news').get_hot(limit=3)
        return '. '.join(x.title for x in submissions)

    def everyblock(body_split):
        '''Returns everyblock crime news feed from Everyblock API.'''
        # print(body_split)
        def find_metro(s):
            # print(s)
            if s.isdigit():
                for met in ('philly', 'denver', 'houston', 'boston', 'chicago'):
                    everyblock_url = 'https://api.everyblock.com/content/{}/zipcodes'.format(met)
                    r = requests.get(everyblock_url, headers={'Authorization': 'Token {}'.format(API_KEY)}) # want to keep key private!!!
                    data = json.loads(r.text)
                    for i in data:
                        if s == i['name']:
                            return met, s
            else:
                for met in ('philly', 'denver', 'houston', 'boston', 'chicago'):
                    everyblock_url = 'https://api.everyblock.com/content/{}/neighborhoods'.format(met)
                    r = requests.get(everyblock_url, headers={'Authorization': 'Token {}'.format(API_KEY)}) # want to keep key private!!!
                    data = json.loads(r.text)
                    print(s)
                    for i in data:
                        if s.title() == i['name']:
                            return met, s.lower().replace(' ', '-')
            return 'ERROR', 'not found'
        
        metro, location = find_metro(' '.join(body_split[1:]))
        if metro == 'ERROR':
            return 'EveryBlock couldn\'t determine your location. Try "everyblock [zipcode or town]" to get a crime news feed for your area.'
        everyblock_url = 'https://api.everyblock.com/content/{}/locations/{}/timeline/?schema=crime'.format(metro, location)
        r = requests.get(everyblock_url, headers = {'Authorization': 'Token {}'.format(API_KEY)})
        data = json.loads(r.text)
        if not r or data['count'] == 0:
            everyblock_url = 'https://api.everyblock.com/content/{}/locations/{}/timeline/?schema=crime-reports'.format(metro, location)
            r = requests.get(everyblock_url, headers = {'Authorization': 'Token {}'.format(API_KEY)})
            data = json.loads(r.text)
        
        count = 0
        if 'count' in data or data['count'] != 0:
            output = ''
        #print data
        for event in data['results']:
            date = event['item_date']
            date = date[5:7]+'/'+date[8:10]+'/'+date[2:4]
            timetype = ''
            for i in ('dispatch_time', 'crime_time', 'occurrence_time', 'offense_time'):
                if i in event['attributes']:
                    timetype = i
                    break
            time = datetime.strptime(event['attributes'][timetype][:5], '%H:%M').strftime('%I:%M %p')
            output += '{event} on {location} at {date}, {time}\n'.format(event=event['title'], location=event['location_name'], date=date, time=time)
            count += 1
            if count == data['count'] or count == 2:
                break
        return output

    body_split = body.lower().split()
    if body_split[0] == 'directions':
        # TO-DO: Maps
        pass
    elif body_split[0] == 'news':
        output = news()
        response.sms(output)
    elif body_split[0] == 'everyblock':
        output = everyblock(body_split)
        response.sms(output)
    else:
        response.sms('Text options:\n"everyblock [zipcode or town/city]": crime news feed for your area\n"news": hottest news stories')
    return str(response)


{'fraction_non_alphanumeric': 0.14198218262806236, 'fraction_numerical': 0.007461024498886414, 'mean_word_length': 2.286132455177461, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '4690165', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2577, 'n_words': 659}