Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/fetch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: UTF-8 -*-

from itertools import islice
from fbchat import Client
from fbchat.models import *

Expand Down Expand Up @@ -62,3 +63,9 @@


# Here should be an example of `getUnread`


# Print image url for 20 last images from thread.
images = client.fetchThreadImages("<thread id>")
for image in islice(image, 20):
print(image.large_preview_url)
36 changes: 36 additions & 0 deletions fbchat/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,42 @@ def getUserActiveStatus(self, user_id):
"""
return self._buddylist.get(str(user_id))

def fetchThreadImages(self, thread_id=None):
"""
Creates generator object for fetching images posted in thread.
Comment thread
mlodybercik marked this conversation as resolved.
:param thread_id: ID of the thread
:return: :class:`ImageAttachment` or :class:`VideoAttachment`.
:rtype: iterable
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"id": thread_id, "first": 48}
thread_id = str(thread_id)
j = self.graphql_request(_graphql.from_query_id("515216185516880", data))
while True:
try:
i = j[thread_id]["message_shared_media"]["edges"][0]
Comment thread
mlodybercik marked this conversation as resolved.
except IndexError:
if j[thread_id]["message_shared_media"]["page_info"].get(
"has_next_page"
):
data["after"] = j[thread_id]["message_shared_media"][
"page_info"
].get("end_cursor")
j = self.graphql_request(
_graphql.from_query_id("515216185516880", data)
)
continue
else:
break

if i["node"].get("__typename") == "MessageImage":
yield ImageAttachment._from_list(i)
elif i["node"].get("__typename") == "MessageVideo":
yield VideoAttachment._from_list(i)
else:
yield Attachment(uid=i["node"].get("legacy_attachment_id"))
del j[thread_id]["message_shared_media"]["edges"][0]

"""
END FETCH METHODS
"""
Expand Down
24 changes: 24 additions & 0 deletions fbchat/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ def _from_graphql(cls, data):
uid=data.get("legacy_attachment_id"),
)

@classmethod
def _from_list(cls, data):
data = data["node"]
return cls(
width=data["original_dimensions"].get("x"),
height=data["original_dimensions"].get("y"),
thumbnail_url=data["image"].get("uri"),
large_preview=data["image2"],
Comment thread
mlodybercik marked this conversation as resolved.
preview=data["image1"],
uid=data["legacy_attachment_id"],
)


@attr.s(cmp=False, init=False)
class VideoAttachment(Attachment):
Expand Down Expand Up @@ -252,6 +264,18 @@ def _from_subattachment(cls, data):
uid=data["target"].get("video_id"),
)

@classmethod
def _from_list(cls, data):
data = data["node"]
return cls(
width=data["original_dimensions"].get("x"),
height=data["original_dimensions"].get("y"),
small_image=data["image"],
medium_image=data["image1"],
large_image=data["image2"],
uid=data["legacy_attachment_id"],
)


def graphql_to_attachment(data):
_type = data["__typename"]
Expand Down