def download_excel(QQ, password, file_type): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish="QQ号与识别码不匹配"), ensure_ascii=False) else: if file_type == 'xlsx': path = RESULT_BASE_DIR if os.path.isfile(os.path.join(path, QQ + '_mood_data.xlsx')): print(os.path.join(path, QQ + '_mood_data.xlsx')) return send_from_directory(path, QQ + '_mood_data.xlsx', as_attachment=True) else: return json.dumps(dict(finish="文件不存在"), ensure_ascii=False) elif file_type == 'csv': path = FRIEND_BASE_DIR if os.path.isfile( os.path.join(path, QQ + '_friend_detail_list.xlsx')): return send_from_directory(path, QQ + '_friend_detail_list.xlsx', as_attachment=True) else: return json.dumps(dict(finish="文件不存在"), ensure_ascii=False)
def clear_cache(QQ, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish="QQ号与识别码不匹配"), ensure_ascii=False) else: try: DATA_DIR_HEAD = BASE_DIR + 'data/' + QQ FRIEND_DIR_HEAD = BASE_DIR + 'friend/' + QQ CONTENT_FILE_NAME = DATA_DIR_HEAD + '_QQ_content.json' LIKE_DETAIL_FILE_NAME = DATA_DIR_HEAD + '_QQ_like_detail' + '.json' LIKE_LIST_NAME_FILE_NAME = DATA_DIR_HEAD + '_QQ_like_list_name' + '.json' MOOD_DETAIL_FILE_NAME = DATA_DIR_HEAD + '_QQ_mood_detail' + '.json' FRIEND_DETAIL_FILE_NAME = FRIEND_DIR_HEAD + '_friend_detail.json' conn.delete(CONTENT_FILE_NAME) conn.delete(LIKE_LIST_NAME_FILE_NAME) conn.delete(MOOD_DETAIL_FILE_NAME) conn.delete(LIKE_DETAIL_FILE_NAME) os.remove(os.path.join(RESULT_BASE_DIR, QQ + '_mood_data.xlsx')) os.remove(os.path.join(RESULT_BASE_DIR, QQ + '_mood_data.csv')) os.remove(os.path.join(BASE_DIR + 'temp', QQ + '.json')) os.remove(os.path.join(BASE_DIR + 'log', QQ + '.log')) finish = 1 return json.dumps(dict(finish=finish), ensure_ascii=False) except BaseException as e: finish = 0 print(e) return json.dumps(dict(info="未知错误:" + str(e), finish=finish), ensure_ascii=False)
def query_spider_info(QQ, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish=-2)) info = conn.lpop(WEB_SPIDER_INFO + QQ) finish = 0 mood_num = -1 friend_num = 0 if info is not None: if info.find(FRIEND_INFO_PRE) != -1: finish = 2 friend_num = int(info.split(':')[1]) elif info.find(MOOD_NUM_PRE) != -1: finish = 1 mood_num = int(info.split(':')[1]) elif info.find("失败") != -1: finish = -1 mood_num = -1 result = dict(info=info, finish=finish, mood_num=mood_num, friend_num=friend_num) return json.dumps(result, ensure_ascii=False)
def query_spider_num(QQ, mood_num, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish=-2)) info = conn.get(MOOD_COUNT_KEY + str(QQ)) finish = 0 if int(info) >= int(mood_num): finish = 1 return json.dumps(dict(num=info, finish=finish))
def query_friend_info_num(QQ, friend_num, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish=-2)) info = conn.get(FRIEND_INFO_COUNT_KEY + str(QQ)) finish = 0 if int(info) >= int(friend_num): finish = 1 return json.dumps(dict(num=info, finish=finish))
def userinfo(QQ, name, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if check_password(conn, QQ, password): user = UserInfo() user.load(QQ) result = dict(finish=1, user=user.to_dict()) return json.dumps(result, ensure_ascii=False) else: result = dict(finish=0) return json.dumps(result, ensure_ascii=False)
def query_clean_data(QQ, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish=-2), ensure_ascii=False) while True: key = conn.get(CLEAN_DATA_KEY + QQ) if key == '1': break else: sleep(0.1) return json.dumps(dict(finish=key), ensure_ascii=False)
def get_history(QQ, name, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) result = {} if not check_password(conn, QQ, password): result['finish'] = 0 return json.dumps(result) history = conn.get(BASE_DIR + 'friend/' + QQ + '_history_like_list.json') if history: history_json = json.loads(history) result['finish'] = 1 result['data'] = history_json else: result['finish'] = -1 return json.dumps(result, ensure_ascii=False)
def stop_spider(QQ, password): pool = get_pool() conn = redis.Redis(connection_pool=pool) if not check_password(conn, QQ, password): return json.dumps(dict(finish=-2)) # 更新标记位,停止爬虫 conn.set(STOP_SPIDER_KEY + QQ, STOP_SPIDER_FLAG) stop = 0 # 等待数据保存 while True: finish_info = conn.get(STOP_SPIDER_KEY + QQ) if finish_info == FINISH_ALL_INFO: stop = 1 break else: sleep(0.1) num = conn.get(MOOD_COUNT_KEY + str(QQ)) return json.dumps(dict(num=num, finish=stop))