diff --git a/fbchat/client.py b/fbchat/client.py index 5f5268e..aa382de 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -63,6 +63,10 @@ class Client(object): if not self.login(): raise Exception("id or password is wrong") + self.threads = [] + self.threads = [] + self.data = data + def _console(self, msg): if self.debug: print(msg) @@ -192,7 +196,10 @@ class Client(object): r = self._post("https://www.facebook.com/ajax/mercury/send_messages.php", form) return r.ok - def getThreadList(self, start, end): + def getThreadList(self, start, end=None): + if not end: + end = start + 20 + timestamp = now() date = datetime.now() data = { @@ -206,11 +213,18 @@ class Client(object): 'inbox[offset]' : start, 'inbox[limit]' : end, } - print data r = self._post("https://www.facebook.com/ajax/mercury/threadlist_info.php", data) - self.r = r - return r.ok + if not r.ok: + return None + + j = get_json(r.text) + + for thread in j['payload']['threads']: + t = Thread(thread) + self.threads.append(t) + + return self.threads def sendSticker(self): pass diff --git a/fbchat/models.py b/fbchat/models.py index 3ff6ff5..938282f 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -21,3 +21,6 @@ class User(Base): self.data = data +class Thread(): + def __init__(self, **entries): + self.__dict__.update(entries) diff --git a/fbchat/utils.py b/fbchat/utils.py index c93c3d5..82ab364 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -15,7 +15,7 @@ def now(): return int(time()*1000) def get_json(text): - return json.loads(re.sub(r"for.*(.*;.*;.*).*;", '', text.decode("unicode-escape").encode('utf-8'))) + return json.loads(re.sub(r"for.*(;;).*;", '', text.decode("unicode-escape").encode('utf-8'), 1)) def digit_to_char(digit): if digit < 10: