def query_spider_info(QQ, password): pool_flag = session.get(POOL_FLAG) conn = get_redis_conn(pool_flag) info = conn.lpop(WEB_SPIDER_INFO + QQ) if not check_password(conn, QQ, password): if info is not None and info.find("登陆失败") != -1: return json.dumps(dict(finish=FAILED_STATE, info=info)) else: return json.dumps(dict(finish=INVALID_LOGIN, info=0)) finish = 0 mood_num = -1 friend_num = 0 if info is not None: if info.find(".jpg") != -1: finish = LOGGING_STATE elif info.find(FRIEND_INFO_PRE) != -1: finish = FINISH_FRIEND friend_num = int(info.split(':')[1]) elif info.find(MOOD_NUM_PRE) != -1: finish = SUCCESS_STATE mood_num = int(info.split(':')[1]) elif info.find("失败") != -1: finish = FAILED_STATE mood_num = FAILED_STATE result = dict(info=info, finish=finish, mood_num=mood_num, friend_num=friend_num) return json.dumps(result, ensure_ascii=False) else: info = '' result = dict(info=info, finish=finish, mood_num=mood_num, friend_num=friend_num) return json.dumps(result, ensure_ascii=False)
def start_spider(): if request.method == 'POST': nick_name = request.form['nick_name'] qq = request.form['qq'] stop_time = str(request.form['stop_time']) mood_num = int(request.form['mood_num']) cookie = request.form['cookie'] if cookie == None or len(cookie) < 10: return json.dumps(dict(result=0), ensure_ascii=False) no_delete = False if request.form['no_delete'] == 'false' else True password = request.form['password'] password = md5_password(password) print("begin spider:", qq) pool_flag = session.get(POOL_FLAG) conn = get_redis_conn(pool_flag) res = init_redis_key(conn, qq) if not res: try: session[POOL_FLAG] = judge_pool() init_redis_key(conn, qq) except BaseException: result = dict(result="连接数据库失败,请刷新页面再尝试") return json.dumps(result, ensure_ascii=False) try: t = threading.Thread(target=web_interface, args=(qq, nick_name, stop_time, mood_num, cookie, no_delete, password, pool_flag)) t.start() result = dict(result=1) return json.dumps(result, ensure_ascii=False) except BaseException as e: result = dict(result=e) return json.dumps(result, ensure_ascii=False) else: return "老哥你干嘛?"
def download_excel(QQ, password, file_type): pool_flag = session.get(POOL_FLAG) conn = get_redis_conn(pool_flag) if not check_password(conn, QQ, password): return json.dumps(dict(finish="QQ号与校验码不匹配"), ensure_ascii=False) else: if file_type == 'mood': path = BASE_DIR + QQ + "/data/result" if os.path.isfile(os.path.join(path, 'mood_data.xlsx')): print(os.path.join(path, 'mood_data.xlsx')) return send_from_directory(path, 'mood_data.xlsx', as_attachment=True) else: return json.dumps(dict(finish="文件不存在"), ensure_ascii=False) elif file_type == 'friend': path = BASE_DIR + QQ + "/friend" if os.path.isfile(os.path.join(path, 'friend_detail_list.xlsx')): return send_from_directory(path, 'friend_detail_list.xlsx', as_attachment=True) else: return json.dumps(dict(finish="文件不存在"), ensure_ascii=False)
from datetime import timedelta import threading curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(os.path.split(rootPath)[0]) from src.web.web_util.web_util import judge_pool, get_redis_conn, WAITING_USER_LIST, begin_check_redis, USER_MAP_KEY import redis from flask import Flask, render_template, session from src.web.controller.dataController import data, POOL_FLAG from src.web.controller.spiderController import spider app = Flask(__name__) app.config['SECRET_KEY'] = os.urandom(24) app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7) host = judge_pool() conn = get_redis_conn(host) conn.delete(WAITING_USER_LIST) @app.route('/') def config(): session[POOL_FLAG] = judge_pool() print("pool flag:", session.get(POOL_FLAG)) return render_template("config.html") @app.route('/error') def error(): return render_template("error.html")
def get_redis_conn(self): host = judge_pool() conn = get_redis_conn(host) return conn