Skip to content

fetchThreadMessages returns graphQL 500 error limit too high #456

@KGALLET

Description

@KGALLET

Description of the problem

I'm currently writing a script which fetch every messages of a specific thread_id using your API.
This script is made in order to download every pictures that I sent in the past to the specific thread_id conversation.
My conversation reach almost 100k messages.
When I use my script with a limit parameter for the fetchThreadMessages which is not too high, it's ok. But as soon as I used a limit really high, I got a graphQL 500 error. (example: 50000)

Code to reproduce

#!python3
from os import mkdir
from os.path import isdir, join
from datetime import datetime

from fbchat import Client
from fbchat.models import ImageAttachment
import urllib3

http = urllib3.PoolManager()

def download_pictures(user, password, thread_id, output_folder, before_tstp, msg_limit=100):
    if not isdir(output_folder):
        mkdir(output_folder)

    client = Client(user, password)

    msg_list = client.fetchThreadMessages(thread_id=thread_id, limit=msg_limit, before=before_tstp)
    for msg in msg_list:
        for att in msg.attachments:
            if not type(att) == ImageAttachment:
                continue
            if (att.original_extension) == 'gif':
                continue     
            url = client.fetchImageUrl(att.uid)
            data = http.request("GET", url=url, preload_content=False)
            data = data.read()
            dt_object = datetime.fromtimestamp(int(msg.timestamp) / 1000).strftime('%Y-%m-%d')
            output_filename = join(output_folder, dt_object + " | " + att.uid + "." + att.original_extension)
            print("Downloading %s" % output_filename)
#            last_timestamp = msg.timestamp

            with open(output_filename, "wb") as f:
                f.write(data)

if __name__ == '__main__':
    from argparse import ArgumentParser

    parser = ArgumentParser()
    parser.add_argument("email", help="Email of your facebook account")
    parser.add_argument("password", help="Password of your facebook account")
    parser.add_argument("thread_id", help="ID of your conversation")
    parser.add_argument("--output", '-o', help="Output folder where pictures will be stored", default=".")
    parser.add_argument("--before", "-b", help="Before which timestamp should we download")
    parser.add_argument('--limit', "-l", help="Limit on the number of messages to search for pictures", default=100)

    args = parser.parse_args()
    download_pictures(args.email, args.password, args.thread_id, args.output, args.before, args.limit)

Traceback

Traceback (most recent call last):
  File "download_messenger_pictures.py", line 60, in <module>
    download_pictures(args.email, args.password, args.thread_id, args.output, args.before, args.limit)
  File "download_messenger_pictures.py", line 28, in download_pictures
    msg_list = client.fetchThreadMessages(thread_id=thread_id, limit=msg_limit, before=before_tstp)
  File "/usr/local/lib/python3.7/site-packages/fbchat/_client.py", line 783, in fetchThreadMessages
    j = self.graphql_request(_graphql.from_doc_id("1860982147341344", params))
  File "/usr/local/lib/python3.7/site-packages/fbchat/_client.py", line 185, in graphql_request
    return self.graphql_requests(query)[0]
  File "/usr/local/lib/python3.7/site-packages/fbchat/_client.py", line 177, in graphql_requests
    return tuple(self._post("/api/graphqlbatch/", data, as_graphql=True))
  File "/usr/local/lib/python3.7/site-packages/fbchat/_client.py", line 134, in _post
    content = check_request(r)
  File "/usr/local/lib/python3.7/site-packages/fbchat/_util.py", line 156, in check_request
    check_http_code(r.status_code)
  File "/usr/local/lib/python3.7/site-packages/fbchat/_util.py", line 171, in check_http_code
    raise FBchatFacebookError(msg, request_status_code=code)
fbchat._exception.FBchatFacebookError: Error when sending request: Got 500 response.

Environment information

  • Python3
  • fbchat 1.7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions