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

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

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

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

H
hjdhnx 已提交
38 39 40 41 42 43 44 45
@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 已提交
46 47 48
@app.route('/vod')
def vod():
    rule = getParmas('rule')
H
hjdhnx 已提交
49
    ext = getParmas('ext')
H
hjdhnx 已提交
50
    if not ext.startswith('http') and not rule:
H
hjdhnx 已提交
51
        return jsonify(error.failed('规则字段必填'))
H
hjdhnx 已提交
52 53
    if not ext.startswith('http') and not rule in rule_list:
        msg = f'服务端本地仅支持以下规则:{",".join(rule_list)}'
H
hjdhnx 已提交
54 55
        return jsonify(error.failed(msg))

H
hjdhnx 已提交
56
    js_path = f'js/{rule}.js' if not ext.startswith('http') else ext
H
hjdhnx 已提交
57
    ctx,js_code = parser.runJs(js_path)
H
hjdhnx 已提交
58 59
    if not js_code:
        return jsonify(error.failed('爬虫规则加载失败'))
H
hjdhnx 已提交
60 61
    rule = ctx.eval('rule')
    cms = CMS(rule)
H
hjdhnx 已提交
62 63 64 65 66 67 68
    wd = getParmas('wd')
    ac = getParmas('ac')
    quick = getParmas('quick')
    play = getParmas('play')
    flag = getParmas('flag')
    filter = getParmas('filter')
    t = getParmas('t')
69 70
    pg = getParmas('pg','1')
    pg = int(pg)
H
hjdhnx 已提交
71 72 73
    ids = getParmas('ids')
    q = getParmas('q')

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

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

H
hjdhnx 已提交
94 95 96 97 98
@app.route('/clear')
def clear():
    rule = getParmas('rule')
    if not rule:
        return jsonify(error.failed('规则字段必填'))
H
hjdhnx 已提交
99
    cache_path = os.path.abspath(f'cache/{rule}.js')
H
hjdhnx 已提交
100
    if not os.path.exists(cache_path):
H
hjdhnx 已提交
101
        return jsonify(error.failed('服务端没有此规则的缓存文件!'+cache_path))
H
hjdhnx 已提交
102 103 104
    os.remove(cache_path)
    return jsonify(error.success('成功删除文件:'+cache_path))

H
hjdhnx 已提交
105
def getRules():
H
hjdhnx 已提交
106 107 108 109 110 111
    base_path = 'cache/'  # 当前文件所在目录
    print(base_path)
    file_name = os.listdir(base_path)
    file_name = list(filter(lambda x: str(x).endswith('.js'), file_name))
    # print(file_name)
    rule_list = [file.replace('.js', '') for file in file_name]
H
hjdhnx 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    rules = {'list': rule_list, 'count': len(rule_list)}
    return rules

@app.route('/rules')
def rules():
    return render_template('rules.html',rules=getRules())

@app.route('/raw')
def rules_raw():
    return render_template('raw.html',rules=getRules())

@app.route("/plugin/<name>",methods=['GET'])
def plugin(name):
    # name=道长影视模板.js
    if not name or not name.split('.')[-1] in ['js','txt','py','json']:
        return jsonify(error.failed(f'非法威胁,未指定文件名。必须包含js|txt|json|py'))
    try:
        return parser.toJs(name)
    except Exception as e:
        return jsonify(error.failed(f'非法猥亵\n{e}'))
H
hjdhnx 已提交
132 133


H
hjdhnx 已提交
134
if __name__ == '__main__':
H
hjdhnx 已提交
135 136
    app.run(host="0.0.0.0", port=5705)
    # app.run(debug=True, host='0.0.0.0', port=5705)