-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
94 lines (68 loc) · 1.76 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
# @File : main.py
# @Date : 2018-09-05
# @Author : Peng Shiyu
import json
from datetime import datetime
from flask import Flask, jsonify
from check import check_all
from models import ProxyModel
from spider import crawl_all, crawl
from scheduler import start_scheduler
app = Flask(__name__)
@app.route("/")
def index():
return "hello"
@app.route("/get")
def get_proxy():
row = ProxyModel.select().order_by(-ProxyModel.score).first()
item = dict()
for k, v in row.__data__.items():
if isinstance(v, datetime):
v = v.strftime("%Y-%m-%d %H:%M:%S")
item[k] = v
return jsonify(item)
@app.route("/get_list/<int:count>")
def get_proxy_list(count):
rows = ProxyModel.select().order_by(-ProxyModel.score).limit(count)
lst = []
for row in rows:
item = dict()
for k, v in row.__data__.items():
if isinstance(v, datetime):
v = v.strftime("%Y-%m-%d %H:%M:%S")
item[k] = v
lst.append(item)
return jsonify(lst)
@app.route("/crawl")
def crawl_proxy_all():
crawl_all()
return jsonify({
"status": 0,
"msg": "ok"
})
@app.route("/crawl/<int:index>")
def crawl_proxy(index):
crawl(index)
return jsonify({
"status": 0,
"msg": "ok"
})
@app.route("/delete/<uid>")
def delete_proxy(uid):
ret = ProxyModel.delete().where(ProxyModel.id == uid).execute()
return jsonify({
"status": 0,
"msg": "ok",
"rows": ret
})
@app.route("/check")
def check_proxy():
check_all()
return jsonify({
"status": 0,
"msg": "ok",
})
if __name__ == '__main__':
start_scheduler()
app.run(host="0.0.0.0", port=8002, debug=True)