<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from math import ceil
import json
import datetime
import os

import requests
from flask import Flask, render_template, request, g, redirect, url_for
from peewee import *


SECRET_KEY = '<KEY>'

app = Flask(__name__)
app.config.from_object(__name__)

db = os.path.dirname(os.path.abspath(__file__)) + '/zhihudaily.db'
database = SqliteDatabase(db)


class BaseModel(Model):
    class Meta:
        database = database


class Zhihudaily(BaseModel):
    date = IntegerField()
    json_news = CharField()
    display_date = CharField()


def create_tables():
    database.connect()
    database.create_tables([Zhihudaily])


@app.before_request
def before_request():
    g.db = database
    g.db.connect()


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


@app.route('/before/<date>')
def before(date):
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get(
        'http://news.at.zhihu.com/api/1.2/news/before/{0}'.format(date))
    display_date = r.json()['display_date']
    today = datetime.date.today().strftime('%Y%m%d')
    strdate = r.json()["date"]
    day_after = (datetime.datetime.strptime(date, '%Y%m%d') + datetime.timedelta(1)).strftime('%Y%m%d')
    if int(today) < int(date):
        if request.args['image']:
            return redirect(url_for('with_image'))
        else:
            return redirect(url_for('index'))
    is_today = r.json().get('is_today', False)
    news_list = [item for item in r.json()['news']]
    if request.args['image']:
        return render_template('with_image.html', lists=news_list,
                               display_date=display_date, date=strdate,
                               is_today=is_today, day_after=day_after)
    else:
        return render_template("index.html", lists=news_list,
                               display_date=display_date, date=strdate,
                               is_today=is_today, day_after=day_after)


@app.route('/')
def index():
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get('http://news.at.zhihu.com/api/1.2/news/latest')
    display_date = r.json()['display_date']
    date = r.json()['date']
    news_list = [item for item in r.json()['news']]
    return render_template("index.html", lists=news_list,
                           display_date=display_date, date=date,
                           is_today=True)


@app.route('/withimage')
def with_image():
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get('http://news.at.zhihu.com/api/1.2/news/latest')
    display_date = r.json()['display_date']
    date = r.json()["date"]
    news_list = [item for item in r.json()['news']]
    request.environ['Referer'] = 'http://daily.zhihu.com/'
    return render_template('with_image.html', lists=news_list,
                           display_date=display_date, date=date)


@app.route('/pages')
@app.route('/pages/<int:page>')
def pages(page=1):
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get('http://news.at.zhihu.com/api/1.2/news/latest')
    display_date = r.json()['display_date']
    date = r.json()["date"]
    news_list = [item for item in r.json()['news']]
    request.environ['Referer'] = 'http://daily.zhihu.com/'
    news = Zhihudaily.select().paginate(page, 7)
    records = []
    for i in news:
        temp = json.loads(i.json_news)
        records.append({"date": i.date, "news": temp, "display_date": i.display_date})
    pages = int(ceil(Zhihudaily.select().count() / 7))
    return render_template('pages.html', lists=news_list,
                           display_date=display_date, date=date,
                           page=page, records=records, pages=pages)


@app.route('/pagination')
def pagination():
    pages = int(ceil(Zhihudaily.select().count() / 7))
    return render_template('pagination.html', pages=pages)
<commit_msg>Pep8 check; bugfix for the before view<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from math import ceil
import json
import datetime
import os

import requests
from flask import Flask, render_template, request, g, redirect, url_for
from peewee import *


SECRET_KEY = '<KEY>'

app = Flask(__name__)
app.config.from_object(__name__)

db = os.path.dirname(os.path.abspath(__file__)) + '/zhihudaily.db'
database = SqliteDatabase(db)


class BaseModel(Model):
    class Meta:
        database = database


class Zhihudaily(BaseModel):
    date = IntegerField()
    json_news = CharField()
    display_date = CharField()


def create_tables():
    database.connect()
    database.create_tables([Zhihudaily])


@app.before_request
def before_request():
    g.db = database
    g.db.connect()


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


@app.route('/before/<date>')
def before(date):
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get(
        'http://news.at.zhihu.com/api/1.2/news/before/{0}'.format(date))
    display_date = r.json()['display_date']
    today = datetime.date.today().strftime('%Y%m%d')
    strdate = r.json()["date"]
    day_after = (
        datetime.datetime.strptime(date, '%Y%m%d') + datetime.timedelta(1)
    ).strftime('%Y%m%d')
    if int(today) < int(date):
        if request.args['image'] == 'True':
            return redirect(url_for('with_image'))
        else:
            return redirect(url_for('index'))
    is_today = r.json().get('is_today', False)
    news_list = [item for item in r.json()['news']]
    if request.args['image'] == 'True':
        return render_template('with_image.html', lists=news_list,
                               display_date=display_date, date=strdate,
                               is_today=is_today, day_after=day_after)
    else:
        return render_template("index.html", lists=news_list,
                               display_date=display_date, date=strdate,
                               is_today=is_today, day_after=day_after)


@app.route('/')
def index():
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get('http://news.at.zhihu.com/api/1.2/news/latest')
    display_date = r.json()['display_date']
    date = r.json()['date']
    news_list = [item for item in r.json()['news']]
    return render_template("index.html", lists=news_list,
                           display_date=display_date, date=date,
                           is_today=True)


@app.route('/withimage')
def with_image():
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get('http://news.at.zhihu.com/api/1.2/news/latest')
    display_date = r.json()['display_date']
    date = r.json()["date"]
    news_list = [item for item in r.json()['news']]
    request.environ['Referer'] = 'http://daily.zhihu.com/'
    return render_template('with_image.html', lists=news_list,
                           display_date=display_date, date=date,
                           is_today=True)


@app.route('/pages')
@app.route('/pages/<int:page>')
def pages(page=1):
    session = requests.Session()
    session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux \
                            x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'})
    r = session.get('http://news.at.zhihu.com/api/1.2/news/latest')
    display_date = r.json()['display_date']
    date = r.json()["date"]
    news_list = [item for item in r.json()['news']]
    request.environ['Referer'] = 'http://daily.zhihu.com/'
    news = Zhihudaily.select().paginate(page, 7)
    records = []
    for i in news:
        temp = json.loads(i.json_news)
        records.append({"date": i.date, "news": temp,
                        "display_date": i.display_date})
    pages = int(ceil(Zhihudaily.select().count() / 7))
    return render_template('pages.html', lists=news_list,
                           display_date=display_date, date=date,
                           page=page, records=records, pages=pages)


@app.route('/pagination')
def pagination():
    pages = int(ceil(Zhihudaily.select().count() / 7))
    return render_template('pagination.html', pages=pages)

{'fraction_non_alphanumeric': 0.15663980571807043, 'fraction_numerical': 0.022850204216800972, 'mean_word_length': 2.6994691710902408, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 9, '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': '541077', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3022, 'n_words': 532}