"""
A Flask server that presents a minimal browsable interface for the Olin course catalog.

author: Oliver Steele <oliver.steele@olin.edu>
date  : 2017-01-18
license: MIT
"""

import os
import json
import re
from datetime import datetime
from bson import json_util

from bs4 import BeautifulSoup
from flask import Flask, redirect, render_template, request, url_for
from jinja2 import evalcontextfilter, Markup, escape
import requests as r
from pymongo import MongoClient
import parsedatetime as pdt

from factory import create_app
from models import get_date_format

_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')

cal = pdt.Calendar()

CLIENT = MongoClient('localhost', 27017)
DB = CLIENT["carpe"]
EMAIL_COLLECTION = DB["emails"]

GOOGLE_BASE = "https://www.google.com/search?tbm=isch&q=%s"


def gmail_to_mongo(email_data):
    """
    This will be updated to actually parse data
    coming from Google once we have GMail API set up.
    """
    data = {
        "message_id": email_data["id"],
        "text": email_data["text"],
        "subject": email_data["subject"],
        "date": get_date_format(re.split('\s\-|\s\+', email_data["date"])[0]),
        "author_email": email_data["author_email"],
        "author_name": email_data["author_name"],
        "replying_to": email_data["replying_to"]
    }
    return data


app = create_app()


@app.route('/health')
def health():
    return 'ok'


@app.template_filter()
@evalcontextfilter
def nl2br(eval_ctx, value):
    result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br>\n')) for p in _paragraph_re.split(escape(value)))
    if eval_ctx.autoescape:
        result = Markup(result)
    return result


@app.route('/')
def home_page():
    # dates = sorted(set(map(lambda fname: re.findall('\d+', fname)[0], os.listdir('parsed_data'))))
    emails = EMAIL_COLLECTION.find().limit(10)
    dates = [(email, cal.parseDT(email["text"], email["date"])) for email in emails]
    html_doc = r.get(GOOGLE_BASE % "cats").content
    soup = BeautifulSoup(html_doc, 'html.parser')
    print soup.find(id="res")
    return render_template('list.html')


@app.route('/email/<id>')
def single_email_view(id):
    email = EMAIL_COLLECTION.find_one({"message_id": id})
    return render_template('single.html', email=email)


@app.route('/emails', methods=["POST"])
def filter_emails():
    dates = request.form.get("date_range", "01/01/2005 - 01/31/2005").split(" - ")  # sets default to Jan 2005
    start_date, end_date = datetime.strptime(dates[0], '%m/%d/%Y'), datetime.strptime(dates[1], '%m/%d/%Y')
    emails = EMAIL_COLLECTION.find({"date": {"$gt": start_date, "$lt": end_date}})
    return json.dumps([email for email in emails], default=json_util.default)


if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    host = "127.0.0.1" if port == 5000 else "0.0.0.0"
    app.run(host=host, debug=True, port=port)

@@ -2047,17 +2047,17 @@
    print
- 
+(
 soup.fin
@@ -2067,16 +2067,17 @@
 d=%22res%22)
+)
 %0A    ret

Changed print to python 3