# coding=utf-8
import dateutil.parser
import flask
import json
import os
import time
import urllib
import yaml


EPISODES = yaml.load(open("episodes.yaml").read())


app = flask.Flask(__name__,
                  static_path="/assets",
                  static_folder="assets")

app.jinja_env.filters["strftime"] = \
    lambda str, fmt: dateutil.parser.parse(str).strftime(fmt)
app.jinja_env.filters["quote_plus"] = lambda u: urllib.quote_plus(u)


ASSETS = os.path.join(app.root_path, "assets")
@app.route("/favicon.ico")
def favicon():
    return flask.send_from_directory(
        ASSETS,
        "favicon.ico",
        mimetype="image/icon")


@app.route("/")
def home():
    return flask.render_template("pages/home.html",
        playlist=os.environ["PLAYLIST"],
        episodes=EPISODES,
        autoplay=not app.debug)


@app.route("/episod/<int:number>")
def episode(number):
    if number < 1:
        return "not found"
    elif number > len(EPISODES):
        return "coming soon"
    else:
        episode = EPISODES[len(EPISODES) - number]
        template = "pages/episode/%s.html" % (
            "youtube" if "yt" in episode else "facebook"
        )
        return flask.render_template(template,
                                     number=number,
                                     episode=episode,
                                     episodes=EPISODES)

[((172, 240), 'flask.Flask', 'flask.Flask', (['__name__'], {'static_path': '"""/assets"""', 'static_folder': '"""assets"""'}), "(__name__, static_path='/assets', static_folder='assets')\n", (183, 240), False, 'import flask\n'), ((458, 495), 'os.path.join', 'os.path.join', (['app.root_path', '"""assets"""'], {}), "(app.root_path, 'assets')\n", (470, 495), False, 'import os\n'), ((426, 446), 'urllib.quote_plus', 'urllib.quote_plus', (['u'], {}), '(u)\n', (443, 446), False, 'import urllib\n'), ((549, 620), 'flask.send_from_directory', 'flask.send_from_directory', (['ASSETS', '"""favicon.ico"""'], {'mimetype': '"""image/icon"""'}), "(ASSETS, 'favicon.ico', mimetype='image/icon')\n", (574, 620), False, 'import flask\n'), ((687, 807), 'flask.render_template', 'flask.render_template', (['"""pages/home.html"""'], {'playlist': "os.environ['PLAYLIST']", 'episodes': 'EPISODES', 'autoplay': '(not app.debug)'}), "('pages/home.html', playlist=os.environ['PLAYLIST'],\n    episodes=EPISODES, autoplay=not app.debug)\n", (708, 807), False, 'import flask\n'), ((1184, 1271), 'flask.render_template', 'flask.render_template', (['template'], {'number': 'number', 'episode': 'episode', 'episodes': 'EPISODES'}), '(template, number=number, episode=episode, episodes=\n    EPISODES)\n', (1205, 1271), False, 'import flask\n')]