diff --git a/fbchat/_client.py b/fbchat/_client.py index 746ce68..a8ca034 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -746,7 +746,7 @@ class Client(object): GraphQL(query=GraphQL.SEARCH_PAGE, params={"search": name, "limit": limit}) ) - return [graphql_to_page(node) for node in j[name]["pages"]["nodes"]] + return [Page._from_graphql(node) for node in j[name]["pages"]["nodes"]] def searchForGroups(self, name, limit=10): """ @@ -790,7 +790,7 @@ class Client(object): # MessageThread => Group thread rtn.append(Group._from_graphql(node)) elif node["__typename"] == "Page": - rtn.append(graphql_to_page(node)) + rtn.append(Page._from_graphql(node)) elif node["__typename"] == "Group": # We don't handle Facebook "Groups" pass @@ -1058,7 +1058,7 @@ class Client(object): if entry["type"] == ThreadType.USER: rtn[_id] = User._from_graphql(entry) else: - rtn[_id] = graphql_to_page(entry) + rtn[_id] = Page._from_graphql(entry) else: raise FBchatException( "{} had an unknown thread type: {}".format(thread_ids[i], entry) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 0df4032..81accbb 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -159,27 +159,6 @@ def graphql_to_thread(thread): ) -def graphql_to_page(page): - if page.get("profile_picture") is None: - page["profile_picture"] = {} - if page.get("city") is None: - page["city"] = {} - plan = None - if page.get("event_reminders") and page["event_reminders"].get("nodes"): - plan = Plan._from_graphql(page["event_reminders"]["nodes"][0]) - - return Page( - page["id"], - url=page.get("url"), - city=page.get("city").get("name"), - category=page.get("category_type"), - photo=page["profile_picture"].get("uri"), - name=page.get("name"), - message_count=page.get("messages_count"), - plan=plan, - ) - - def graphql_queries_to_json(*queries): """ Queries should be a list of GraphQL objects diff --git a/fbchat/_page.py b/fbchat/_page.py index 9c696e0..b5846c0 100644 --- a/fbchat/_page.py +++ b/fbchat/_page.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import attr +from . import _plan from ._thread import ThreadType, Thread @@ -36,3 +37,24 @@ class Page(Thread): self.likes = likes self.sub_title = sub_title self.category = category + + @classmethod + def _from_graphql(cls, data): + if data.get("profile_picture") is None: + data["profile_picture"] = {} + if data.get("city") is None: + data["city"] = {} + plan = None + if data.get("event_reminders") and data["event_reminders"].get("nodes"): + plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0]) + + return cls( + data["id"], + url=data.get("url"), + city=data.get("city").get("name"), + category=data.get("category_type"), + photo=data["profile_picture"].get("uri"), + name=data.get("name"), + message_count=data.get("messages_count"), + plan=plan, + ) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index b7f0253..e35f623 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -14,7 +14,6 @@ from ._graphql import ( graphql_to_quick_reply, graphql_to_message, graphql_to_thread, - graphql_to_page, graphql_queries_to_json, graphql_response_to_json, GraphQL,