diff --git a/fbchat/client.py b/fbchat/client.py index 9513725..77c409a 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -40,6 +40,7 @@ PingURL ="https://0-channel-proxy-06-ash2.facebook.com/active_ping" UploadURL ="https://upload.facebook.com/ajax/mercury/upload.php" UserInfoURL ="https://www.facebook.com/chat/user_info/" RemoveUserURL="https://www.facebook.com/chat/remove_participants/" +LogoutURL ="https://www.facebook.com/logout.php" class Client(object): """A client for the Facebook Chat (Messenger). @@ -160,6 +161,7 @@ class Client(object): r = self._get(BaseURL) soup = bs(r.text, "lxml") self.fb_dtsg = soup.find("input", {'name':'fb_dtsg'})['value'] + self.fb_h = soup.find("input", {'name':'h'})['value'] self._setttstamp() # Set default payload self.payloadDefault['__rev'] = int(r.text.split('"revision":',1)[1].split(",",1)[0]) @@ -188,6 +190,19 @@ class Client(object): else: return False + def logout(self, timeout=30): + data = {} + data['ref'] = "mb" + data['h'] = self.fb_h + payload=self._generatePayload(data) + r = self._session.get(LogoutURL, headers=self._header, params=payload, timeout=timeout) + # reset value + self.payloadDefault={} + self._session = requests.session() + self.req_counter = 1 + self.seq = "0" + return r + def listen(self): pass @@ -327,21 +342,27 @@ class Client(object): # Strip the start and parse out the returned image_id return json.loads(r._content[9:])['payload']['metadata'][0]['image_id'] - def getThreadInfo(self, userID, start, end=None): + def getThreadInfo(self, userID, start, end=None, thread_type='user'): """Get the info of one Thread :param userID: ID of the user you want the messages from :param start: the start index of a thread :param end: (optional) the last index of a thread + :param thread_type: (optional) change from 'user' for group threads """ if not end: end = start + 20 if end <= start: end = start + end data = {} - data['messages[user_ids][%s][offset]'%userID] = start - data['messages[user_ids][%s][limit]'%userID] = end - data['messages[user_ids][%s][timestamp]'%userID] = now() + if thread_type == 'user': + key = 'user_ids' + else: + key = 'thread_fbids' + + data['messages[{}][{}][offset]'.format(key, userID)] = start + data['messages[{}][{}][limit]'.format(key, userID)] = end + data['messages[{}][{}][timestamp]'.format(key, userID)] = now() r = self._post(MessagesURL, query=data) if not r.ok or len(r.text) == 0: