From 48894f35c49c3cfaa0afd510bde591e1e2f6d509 Mon Sep 17 00:00:00 2001 From: lemon <961222258@qq.com> Date: Thu, 20 Feb 2020 22:14:08 +0800 Subject: [PATCH] 修复好友列表的无好友获取异常问题 --- core/core.py | 2 ++ core/monitor.py | 10 ++++++++-- lib/facebook.py | 13 ++++++++++++- utils/parse_html.py | 2 ++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/core/core.py b/core/core.py index 5906322..a481714 100644 --- a/core/core.py +++ b/core/core.py @@ -4,6 +4,7 @@ # @Author : Lemon # @File : core.py # @Software: PyCharm +import datetime import functools import json @@ -89,6 +90,7 @@ def run(): sched = TornadoScheduler() sched.add_job(monitor._socket.sendheartbeat, 'interval', seconds=25) sched.add_job(monitor._socket.auto_move_server, 'interval', seconds=31) + sched.add_job(monitor.go_login, next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=5)) sched.start() # init schedule end diff --git a/core/monitor.py b/core/monitor.py index e2f5fbc..fa98b64 100644 --- a/core/monitor.py +++ b/core/monitor.py @@ -6,9 +6,11 @@ # @Software: PyCharm import functools import logging +import threading import time import uuid from concurrent.futures import ThreadPoolExecutor +from threading import Timer from munch import Munch @@ -43,13 +45,17 @@ class Monitor(callback.CallBack): self._socket = socket Config.set('host', self._socket.ws_url) + def go_login(self): + threading.Thread(target=self._auto_login,args=()).start() + def _auto_login(self): user_list = UserList.query(status=Status.ONLINE) for user in user_list: - print("执行登录", user) - self.login(user.email, user.password, user.format_cookie()) + print("自动登录->", user) + self.login(user.email, user.password, user.format_cookie(), user.user_agent) time.sleep(0.2) + def _replace_call(self, client): funcs = [x for x in dir(self) if x.startswith("on")] for fname in funcs: diff --git a/lib/facebook.py b/lib/facebook.py index 69f7cf1..8567bbb 100644 --- a/lib/facebook.py +++ b/lib/facebook.py @@ -795,7 +795,18 @@ class FacebookClient(Client): res = res['data']['node'] else: res = self._state._session.get(self._state.page_url + '/friends') - res = parse_html.get_frient_div(res.text) + if res.status_code == 200 and '/friends' in res.url: + res = parse_html.get_frient_div(res.text) + if not res: + respone = { + 'items': [], + 'count': 0, + 'ext_data': {}, + 'has_next_page': False + } + return respone + else: + raise BaseException("网络访问异常,utl=", res.url) edges = res['items']['edges'] count = res['items']['count'] friends = [] diff --git a/utils/parse_html.py b/utils/parse_html.py index 75f555b..07deb63 100644 --- a/utils/parse_html.py +++ b/utils/parse_html.py @@ -183,6 +183,8 @@ def get_div_text(html): def get_frient_div(text): + if not 'ProfileCometAppSectionFriendsList' in text: + return None b = bs4.BeautifulSoup(text, 'html.parser') script = b.find('script', string=re.compile(r'ProfileCometAppSectionFriendsList')) a = re.findall(r'TimelineAppCollectionListRenderer",collection:(\{.*?\}\}\})', script.string)[0] -- libgit2 0.24.0