import logging

from episodes import find_updates, db, count_all
from logging import error as logi
from flask import Flask, jsonify, request


def create_app(config, debug=False, testing=False, config_overrides=None):
    app = Flask(__name__)
    app.config.from_object(config)
    app.config['JSON_AS_ASCII'] = False

    app.debug = debug
    app.testing = testing

    if config_overrides:
        app.config.update(config_overrides)

    # Configure logging
    if not app.testing:
        logging.basicConfig(level=logging.INFO)

    @app.before_request
    def before_request():
        db.connect()

    @app.after_request
    def after_request(response):
        db.close()
        return response

    @app.route('/get_new_episodes')
    def get_new_episodes():
        appengine_request = request.headers.get('X-Appengine-Cron')
        if appengine_request == 'true':
            from scraper import update_episodes
            update_episodes()
            return '<h1>Success</h1>'
        else:
            return '<h1>This is a crobjob and all the requests should come from appengine.</h1>'

    @app.route('/get_updates')
    def get_update():
        timestamp = request.args.get('timestamp', '')

        if timestamp == '':
            logi('Default timestamp')
            timestamp = 0
        else:
            timestamp = long(timestamp)

        result = find_updates(timestamp)

        return jsonify(result)

    @app.route('/')
    def welcome():
        message = '{}{}{}{}'.format('<h1>Welcome to FardaStationAPI WebService</h1>',
                                    '<p>To get information about the latest episodes of Fardaa Station (by '
                                    'RadioFarda.com) please send a GET request to '
                                    'http://fardastationapi.appspot.com/get_updates URL.</p>',
                                    '<p>A UNIX epoch timestamp can also be passed in as an argument to filter out the '
                                    'episodes before that timestamp. Example: '
                                    'https://fardastationapi.appspot.com/get_updates?timestamp=1512629949</p>',
                                    '<h1>Current number of episodes: {}</h1>'.format(count_all()))
        return message

    # Add an error handler. This is useful for debugging the live application,
    # however, you should disable the output of the exception for production
    # applications.
    @app.errorhandler(500)
    def server_error(e):
        return """
        An internal error occurred: <pre>{}</pre>
        See logs for full stacktrace.
        """.format(e), 500

    return app

[((9, 10, 9, 25), 'flask.Flask', 'Flask', ({(9, 16, 9, 24): '__name__'}, {}), '(__name__)', False, 'from flask import Flask, jsonify, request\n'), ((21, 8, 21, 47), 'logging.basicConfig', 'logging.basicConfig', (), '', False, 'import logging\n'), ((25, 8, 25, 20), 'episodes.db.connect', 'db.connect', ({}, {}), '()', False, 'from episodes import find_updates, db, count_all\n'), ((29, 8, 29, 18), 'episodes.db.close', 'db.close', ({}, {}), '()', False, 'from episodes import find_updates, db, count_all\n'), ((34, 28, 34, 67), 'flask.request.headers.get', 'request.headers.get', ({(34, 48, 34, 66): '"""X-Appengine-Cron"""'}, {}), "('X-Appengine-Cron')", False, 'from flask import Flask, jsonify, request\n'), ((44, 20, 44, 53), 'flask.request.args.get', 'request.args.get', ({(44, 37, 44, 48): '"""timestamp"""', (44, 50, 44, 52): '""""""'}, {}), "('timestamp', '')", False, 'from flask import Flask, jsonify, request\n'), ((52, 17, 52, 40), 'episodes.find_updates', 'find_updates', ({(52, 30, 52, 39): 'timestamp'}, {}), '(timestamp)', False, 'from episodes import find_updates, db, count_all\n'), ((54, 15, 54, 30), 'flask.jsonify', 'jsonify', ({(54, 23, 54, 29): 'result'}, {}), '(result)', False, 'from flask import Flask, jsonify, request\n'), ((37, 12, 37, 29), 'scraper.update_episodes', 'update_episodes', ({}, {}), '()', False, 'from scraper import update_episodes\n'), ((47, 12, 47, 37), 'logging.error', 'logi', ({(47, 17, 47, 36): '"""Default timestamp"""'}, {}), "('Default timestamp')", True, 'from logging import error as logi\n'), ((65, 85, 65, 96), 'episodes.count_all', 'count_all', ({}, {}), '()', False, 'from episodes import find_updates, db, count_all\n')]