Extract pull channel handling from ReqUrl

This commit is contained in:
Mads Marquart
2019-06-29 20:10:55 +02:00
parent f97d36b41f
commit e0754031ad
2 changed files with 17 additions and 17 deletions

View File

@@ -77,6 +77,7 @@ class Client(object):
self._default_thread_id = None self._default_thread_id = None
self._default_thread_type = None self._default_thread_type = None
self._req_url = ReqUrl() self._req_url = ReqUrl()
self._pull_channel = 0
self._markAlive = True self._markAlive = True
self._buddylist = dict() self._buddylist = dict()
@@ -2378,7 +2379,12 @@ class Client(object):
"viewer_uid": self._uid, "viewer_uid": self._uid,
"state": "active", "state": "active",
} }
self._get(self._req_url.PING, data, fix_request=True, as_json=False) self._get(
self._req_url.PING.format(self._pull_channel),
data,
fix_request=True,
as_json=False,
)
def _pullMessage(self): def _pullMessage(self):
"""Call pull api with seq value to get message data.""" """Call pull api with seq value to get message data."""
@@ -2390,7 +2396,12 @@ class Client(object):
"clientid": self._client_id, "clientid": self._client_id,
"state": "active" if self._markAlive else "offline", "state": "active" if self._markAlive else "offline",
} }
return self._get(self._req_url.STICKY, data, fix_request=True, as_json=True) return self._get(
self._req_url.STICKY.format(self._pull_channel),
data,
fix_request=True,
as_json=True,
)
def _parseDelta(self, m): def _parseDelta(self, m):
def getThreadIdAndThreadType(msg_metadata): def getThreadIdAndThreadType(msg_metadata):
@@ -3082,7 +3093,8 @@ class Client(object):
except FBchatFacebookError as e: except FBchatFacebookError as e:
# Fix 502 and 503 pull errors # Fix 502 and 503 pull errors
if e.request_status_code in [502, 503]: if e.request_status_code in [502, 503]:
self._req_url.change_pull_channel() # Bump pull channel, while contraining withing 0-4
self._pull_channel = (self._pull_channel + 1) % 5
self.startListening() self.startListening()
else: else:
raise e raise e

View File

@@ -69,8 +69,8 @@ class ReqUrl(object):
MARK_SEEN = "https://www.facebook.com/ajax/mercury/mark_seen.php" MARK_SEEN = "https://www.facebook.com/ajax/mercury/mark_seen.php"
BASE = "https://www.facebook.com" BASE = "https://www.facebook.com"
MOBILE = "https://m.facebook.com/" MOBILE = "https://m.facebook.com/"
STICKY = "https://0-edge-chat.facebook.com/pull" STICKY = "https://{}-edge-chat.facebook.com/pull"
PING = "https://0-edge-chat.facebook.com/active_ping" PING = "https://{}-edge-chat.facebook.com/active_ping"
UPLOAD = "https://upload.facebook.com/ajax/mercury/upload.php" UPLOAD = "https://upload.facebook.com/ajax/mercury/upload.php"
INFO = "https://www.facebook.com/chat/user_info/" INFO = "https://www.facebook.com/chat/user_info/"
CONNECT = "https://www.facebook.com/ajax/add_friend/action.php?dpr=1" CONNECT = "https://www.facebook.com/ajax/add_friend/action.php?dpr=1"
@@ -116,18 +116,6 @@ class ReqUrl(object):
UNSEND = "https://www.facebook.com/messaging/unsend_message/?dpr=1" UNSEND = "https://www.facebook.com/messaging/unsend_message/?dpr=1"
FORWARD_ATTACHMENT = "https://www.facebook.com/mercury/attachments/forward/" FORWARD_ATTACHMENT = "https://www.facebook.com/mercury/attachments/forward/"
pull_channel = 0
def change_pull_channel(self, channel=None):
if channel is None:
self.pull_channel = (self.pull_channel + 1) % 5 # Pull channel will be 0-4
else:
self.pull_channel = channel
self.STICKY = "https://{}-edge-chat.facebook.com/pull".format(self.pull_channel)
self.PING = "https://{}-edge-chat.facebook.com/active_ping".format(
self.pull_channel
)
facebookEncoding = "UTF-8" facebookEncoding = "UTF-8"