summaryrefslogtreecommitdiff
path: root/app.py
blob: 2f6c4b8dda9464951321b2e79ba7315fa2797875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
import traceback
import acmespb

from flask import Flask, render_template, jsonify, request


app = Flask(__name__)
app.config.from_mapping(SECRET_KEY='dev', JSON_AS_ASCII=False)

logger = logging.getLogger('app')


@app.after_request
def add_header(r):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    r.headers["Pragma"] = "no-cache"
    r.headers["Expires"] = "0"
    r.headers['Cache-Control'] = 'public, max-age=0'
    return r


@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')


@app.route('/hints.ajax', methods=['GET'])
def ajax_hints():
    query = request.args.get('q') or ''
    if len(query) < 3:
        return jsonify(error="query is too short")

    results = acmespb.search(query)
    return jsonify(response=results)


@app.route('/offers.ajax', methods=['GET'])
def ajax_offers():
    query = request.args.get('q') or ''
    page = request.args.get('page') or 1
    target_url = request.args.get('target_url') or ''

    try:
        if page == 1 or not target_url:
            target_url, trade_names = acmespb.trade_names(query)
            if trade_names:
                return jsonify(tradeNames=trade_names)

        target_url, pages, offers = acmespb.offers(query, page=page, target_url=target_url)
    except Exception as e:
        traceback.print_exc()
        return jsonify(error=str(e))

    return jsonify(
        offers=[offer.as_dict() for offer in offers],
        pages=pages
    )

    # TODO support empty results


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)