app.py 2.2 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5 6 7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : app.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2022/8/25

from flask import Flask, jsonify, abort,request,redirect,make_response,render_template
H
hjdhnx 已提交
8
from js.rules import getRules
H
hjdhnx 已提交
9
from utils import error,parser
H
hjdhnx 已提交
10 11
import sys
import codecs
H
hjdhnx 已提交
12
from models.cms import CMS
H
hjdhnx 已提交
13 14 15 16
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False  # jsonify返回的中文正常显示
H
hjdhnx 已提交
17
from utils.web import *
H
hjdhnx 已提交
18 19
rule_list = getRules()
print(rule_list)
H
hjdhnx 已提交
20

H
hjdhnx 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
def getParmas(key=None):
    """
    获取链接参数
    :param key:
    :return:
    """
    args = {}
    if request.method == 'POST':
        args = request.json
    elif request.method == 'GET':
        args = request.args
    if key:
        return args.get(key,'')
    else:
        return args

H
hjdhnx 已提交
37 38 39 40 41 42 43 44
@app.route('/')
def forbidden():  # put application's code here
    abort(403)

@app.route('/index')
def index():  # put application's code here
    return render_template('index.html')

H
hjdhnx 已提交
45 46 47 48 49 50 51 52 53 54 55
@app.route('/vod')
def vod():
    rule = getParmas('rule')
    if not rule:
        return jsonify(error.failed('规则字段必填'))
    if not rule in rule_list:
        msg = f'仅支持以下规则:{",".join(rule_list)}'
        return jsonify(error.failed(msg))

    js_path = f'js/{rule}.js'
    ctx,js_code = parser.runJs(js_path)
H
hjdhnx 已提交
56 57
    rule = ctx.eval('rule')
    cms = CMS(rule)
H
hjdhnx 已提交
58 59 60 61 62 63 64 65 66 67 68 69
    wd = getParmas('wd')
    ac = getParmas('ac')
    quick = getParmas('quick')
    play = getParmas('play')
    flag = getParmas('flag')
    filter = getParmas('filter')
    t = getParmas('t')
    pg = getParmas('pg')
    ext = getParmas('ext')
    ids = getParmas('ids')
    q = getParmas('q')

H
hjdhnx 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    if ac and t: # 一级
        data = cms.categoryContent(t,pg)
        # print(data)
        return jsonify(data)
    if ac and ids: # 二级
        data = cms.detailContent(ids.split(','))
        # print(data)
        return jsonify(data)
    if wd: # 搜索
        data = cms.searchContent(wd)
        # print(data)
        return jsonify(data)

    # return jsonify({'rule':rule,'js_code':js_code})
    home_data = cms.homeContent()
    return jsonify(home_data)
H
hjdhnx 已提交
86

H
hjdhnx 已提交
87 88
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=9000)