可扩展的Cookies池,用无头浏览器登录并生成cookie供给爬虫使用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.5 KiB

7 years ago
import json
8 years ago
from flask import Flask, g
from cookiespool.config import *
8 years ago
from cookiespool.db import *
8 years ago
__all__ = ['app']
app = Flask(__name__)
@app.route('/')
def index():
return '<h2>Welcome to Cookie Pool System</h2>'
7 years ago
8 years ago
def get_conn():
"""
获取
:return:
"""
7 years ago
for website in GENERATOR_MAP:
print(website)
if not hasattr(g, website):
setattr(g, website + '_cookies', eval('RedisClient' + '("cookies", "' + website + '")'))
setattr(g, website + '_accounts', eval('RedisClient' + '("accounts", "' + website + '")'))
8 years ago
return g
7 years ago
@app.route('/<website>/random')
def random(website):
8 years ago
"""
获取随机的Cookie, 访问地址如 /weibo/random
:return: 随机Cookie
"""
g = get_conn()
7 years ago
cookies = getattr(g, website + '_cookies').random()
8 years ago
return cookies
7 years ago
@app.route('/<website>/add/<username>/<password>')
def add(website, username, password):
8 years ago
"""
添加用户, 访问地址如 /weibo/add/user/password
7 years ago
:param website: 站点
:param username: 用户名
:param password: 密码
:return:
8 years ago
"""
g = get_conn()
7 years ago
print(username, password)
getattr(g, website + '_accounts').set(username, password)
return json.dumps({'status': '1'})
8 years ago
7 years ago
@app.route('/<website>/count')
def count(website):
8 years ago
"""
获取Cookies总数
"""
g = get_conn()
7 years ago
count = getattr(g, website + '_cookies').count()
return json.dumps({'status': '1', 'count': count})
8 years ago
if __name__ == '__main__':
app.run(host='0.0.0.0')