from flask import Flask, Response, redirect, render_template, request
from sunlightapi import sunlight, SunlightApiError
import csv
import json
import logging
import os

logger = logging.getLogger('districtoffices')

PWD = os.path.abspath(os.path.dirname(__file__))
OFFICES = {}

sunlight.apikey = 'a25cc8b1f8bc16b4fdbfc23995516049'

# Load district offices from CSV and store in memory

infile = open(os.path.join(PWD, 'data', 'directory-clean.csv'))

for record in csv.reader(infile):
    if record[0] not in OFFICES:
        OFFICES[record[0]] = []
    OFFICES[record[0]].append({
        'address': record[1],
        'latitude': record[2],
        'longitude': record[3],
        'phone': record[4],
    })

infile.close()

def legislator_to_dict(legislator):
    return {
        'title': legislator.title,
        'firstname': legislator.firstname,
        'lastname': legislator.lastname,
        'bioguide_id': legislator.bioguide_id,
        'district': legislator.district,
        'party': legislator.party,
        'offices': OFFICES.get(legislator.bioguide_id, []),
    }

#
# the app
#

app = Flask(__name__)

@app.before_request
def request_logger():
    print request.path
    logger.debug(request.path)

@app.route("/zipcode/<zipcode>")
def by_zipcode(zipcode):

    members = []

    for legislator in sunlight.legislators.allForZip(zipcode):

        if legislator.in_office:
            members.append(legislator_to_dict(legislator))

    return Response(json.dumps(members), mimetype='application/json')

@app.route("/member/<bioguide_id>")
def by_member(bioguide_id):

    legislator = sunlight.legislators.get(bioguide_id=bioguide_id, in_office=True)
    member = legislator_to_dict(legislator)

    return Response(json.dumps(member), mimetype='application/json')

@app.route("/embed")
def embed():
    return render_template('index.html')

@app.route("/")
def index():
    return redirect('/embed')


if __name__ == '__main__':
    app.run(debug=True, port=8000)
@@ -1118,16 +1118,18 @@
 ame__)%0A%0A
+# 
 @app.bef
@@ -1140,16 +1140,18 @@
 request%0A
+# 
 def requ
@@ -1160,24 +1160,26 @@
 t_logger():%0A
+# 
     print re
@@ -1189,16 +1189,18 @@
 st.path%0A
+# 
     logg

remove logging