<commit_before># -*- coding: utf-8 -*-


import os
from itertools import chain
from collections import OrderedDict

import times
from flask import request, render_template, send_from_directory

from . import app, parsers
from .models import Showtime, Film, Cinema
from .image import render_image, Image, PlaceholderImage


@app.context_processor
def inject_config():
    return {
        'ga_code': app.config['GA_CODE'],
        'debug': app.debug,
    }


@app.route('/more/', defaults={'more': True})
@app.route('/', defaults={'more': False})
def index(more):
    less_items = 2
    cinemas = set()

    # prepare data for listing of films
    data = OrderedDict()
    for showtime in Showtime.upcoming():
        day = showtime.starts_at_day
        data.setdefault(day, {}).setdefault(showtime.film, []).append(showtime)

    days = data.keys() if more else data.keys()[:less_items]
    for day, films in data.items():
        if day in days:
            films = OrderedDict(sorted(
                films.items(),
                key=lambda (f, s): f.rating, reverse=True
            ))
            for film, showtimes in films.items():
                films[film] = sorted(showtimes, key=lambda s: s.starts_at)
                cinemas.update(s.cinema for s in showtimes)
            data[day] = films
        else:
            del data[day]

    # cinemas
    cinemas = sorted(
        (c for c in cinemas if not c.is_multiplex),
        key=lambda c: (c.priority, c.name)
    )

    # stats for the closest day
    closest_showtimes = list(
        chain(*[showtimes for (f, showtimes) in data.values()[0].items()])
    )
    closest_cinemas_count = len(frozenset(s.cinema for s in closest_showtimes))
    closest_showtimes_count = len(closest_showtimes)
    today_any_showtimes = bool(data.get(times.now().date(), False))

    # render the template
    return render_template('index.html', data=data, more=more,
                           cinemas=cinemas, less_items=less_items,
                           closest_cinemas_count=closest_cinemas_count,
                           closest_showtimes_count=closest_showtimes_count,
                           today_any_showtimes=today_any_showtimes)


@app.route('/film/<film_slug>')
def film(film_slug):
    raise NotImplementedError


@app.route('/cinema/<cinema_slug>')
def cinema(cinema_slug):
    raise NotImplementedError


@app.route('/favicon.ico')
@app.route('/apple-touch-icon.png')
@app.route('/robots.txt')
@app.route('/humans.txt')
def static_files():
    static_dir = os.path.join(app.root_path, 'static')
    return send_from_directory(static_dir, request.path.lstrip('/'))


@app.route('/images/poster/<film_slug>.jpg')
def poster(film_slug):
    resize = parsers.resize(request.args.get('resize', 'x'))
    crop = request.args.get('crop')

    film = Film.objects.get_or_404(slug=film_slug)
    if film.url_poster:
        img = Image.from_url(film.url_poster)
        return render_image(img, resize=resize, crop=crop)

    img = PlaceholderImage('#EEE', size=resize)
    return render_image(img, crop=crop)


@app.route('/images/photo/<cinema_slug>.jpg')
def photo(cinema_slug):
    resize = parsers.resize(request.args.get('resize', 'x'))
    crop = request.args.get('crop')

    cinema = Cinema.objects.get_or_404(slug=cinema_slug)
    filename = os.path.join(
        app.root_path, 'static/images', cinema.slug + '.jpg'
    )

    if os.path.exists(filename):
        with open(filename) as f:
            img = Image(f)
            return render_image(img, resize=resize, crop=crop)

    img = PlaceholderImage('#EEE', size=resize)
    return render_image(img, crop=crop)
<commit_msg>Fixed #112.<commit_after># -*- coding: utf-8 -*-


import os
from itertools import chain
from collections import OrderedDict

import times
from flask import request, render_template, send_from_directory

from . import app, parsers, log
from .models import Showtime, Film, Cinema
from .image import render_image, Image, PlaceholderImage


@app.context_processor
def inject_config():
    return {
        'ga_code': app.config['GA_CODE'],
        'debug': app.debug,
    }


@app.route('/more/', defaults={'more': True})
@app.route('/', defaults={'more': False})
def index(more):
    less_items = 2
    cinemas = set()

    # prepare data for listing of films
    data = OrderedDict()
    for showtime in Showtime.upcoming():
        day = showtime.starts_at_day
        data.setdefault(day, {}).setdefault(showtime.film, []).append(showtime)

    days = data.keys() if more else data.keys()[:less_items]
    for day, films in data.items():
        if day in days:
            films = OrderedDict(sorted(
                films.items(),
                key=lambda (f, s): f.rating, reverse=True
            ))
            for film, showtimes in films.items():
                films[film] = sorted(showtimes, key=lambda s: s.starts_at)
                cinemas.update(s.cinema for s in showtimes)
            data[day] = films
        else:
            del data[day]

    # cinemas
    cinemas = sorted(
        (c for c in cinemas if not c.is_multiplex),
        key=lambda c: (c.priority, c.name)
    )

    # stats for the closest day
    closest_showtimes = list(
        chain(*[showtimes for (f, showtimes) in data.values()[0].items()])
    )
    closest_cinemas_count = len(frozenset(s.cinema for s in closest_showtimes))
    closest_showtimes_count = len(closest_showtimes)
    today_any_showtimes = bool(data.get(times.now().date(), False))

    # render the template
    return render_template('index.html', data=data, more=more,
                           cinemas=cinemas, less_items=less_items,
                           closest_cinemas_count=closest_cinemas_count,
                           closest_showtimes_count=closest_showtimes_count,
                           today_any_showtimes=today_any_showtimes)


@app.route('/film/<film_slug>')
def film(film_slug):
    raise NotImplementedError


@app.route('/cinema/<cinema_slug>')
def cinema(cinema_slug):
    raise NotImplementedError


@app.route('/favicon.ico')
@app.route('/apple-touch-icon.png')
@app.route('/robots.txt')
@app.route('/humans.txt')
def static_files():
    static_dir = os.path.join(app.root_path, 'static')
    return send_from_directory(static_dir, request.path.lstrip('/'))


@app.route('/images/poster/<film_slug>.jpg')
def poster(film_slug):
    try:
        resize = parsers.resize(request.args.get('resize', 'x'))
        crop = request.args.get('crop')

        film = Film.objects.get_or_404(slug=film_slug)
        if film.url_poster:
            img = Image.from_url(film.url_poster)
            return render_image(img, resize=resize, crop=crop)
    except Exception:
        log.exception()

    img = PlaceholderImage('#EEE', size=resize)
    return render_image(img, crop=crop)


@app.route('/images/photo/<cinema_slug>.jpg')
def photo(cinema_slug):
    try:
        resize = parsers.resize(request.args.get('resize', 'x'))
        crop = request.args.get('crop')

        cinema = Cinema.objects.get_or_404(slug=cinema_slug)
        filename = os.path.join(
            app.root_path, 'static/images', cinema.slug + '.jpg'
        )

        if os.path.exists(filename):
            with open(filename) as f:
                img = Image(f)
                return render_image(img, resize=resize, crop=crop)
    except Exception:
        log.exception()

    img = PlaceholderImage('#EEE', size=resize)
    return render_image(img, crop=crop)

{'fraction_non_alphanumeric': 0.12788371782904387, 'fraction_numerical': 0.002800373383117749, 'mean_word_length': 2.8109756097560976, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5784945', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2390, 'n_words': 506}