Be more explicit in UserData/PageData parsing
Allows us to make some fields required (aka. not None)
This commit is contained in:
@@ -501,7 +501,7 @@ class Client:
|
|||||||
if pages_and_users.get(_id) is None:
|
if pages_and_users.get(_id) is None:
|
||||||
raise FBchatException("Could not fetch thread {}".format(_id))
|
raise FBchatException("Could not fetch thread {}".format(_id))
|
||||||
entry.update(pages_and_users[_id])
|
entry.update(pages_and_users[_id])
|
||||||
if "first_name" in entry["type"]:
|
if "first_name" in entry:
|
||||||
rtn[_id] = UserData._from_graphql(self.session, entry)
|
rtn[_id] = UserData._from_graphql(self.session, entry)
|
||||||
else:
|
else:
|
||||||
rtn[_id] = PageData._from_graphql(self.session, entry)
|
rtn[_id] = PageData._from_graphql(self.session, entry)
|
||||||
@@ -551,7 +551,9 @@ class Client:
|
|||||||
if _type == "GROUP":
|
if _type == "GROUP":
|
||||||
rtn.append(GroupData._from_graphql(self.session, node))
|
rtn.append(GroupData._from_graphql(self.session, node))
|
||||||
elif _type == "ONE_TO_ONE":
|
elif _type == "ONE_TO_ONE":
|
||||||
rtn.append(UserData._from_thread_fetch(self.session, node))
|
user = UserData._from_thread_fetch(self.session, node)
|
||||||
|
if user:
|
||||||
|
rtn.append(user)
|
||||||
else:
|
else:
|
||||||
raise FBchatException(
|
raise FBchatException(
|
||||||
"Unknown thread type: {}, with data: {}".format(_type, node)
|
"Unknown thread type: {}, with data: {}".format(_type, node)
|
||||||
|
@@ -24,9 +24,9 @@ class PageData(Page):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The page's picture
|
#: The page's picture
|
||||||
photo = attr.ib(None)
|
photo = attr.ib()
|
||||||
#: The name of the page
|
#: The name of the page
|
||||||
name = attr.ib(None)
|
name = attr.ib()
|
||||||
#: Datetime when the thread was last active / when the last message was sent
|
#: Datetime when the thread was last active / when the last message was sent
|
||||||
last_active = attr.ib(None)
|
last_active = attr.ib(None)
|
||||||
#: Number of messages in the thread
|
#: Number of messages in the thread
|
||||||
@@ -61,7 +61,7 @@ class PageData(Page):
|
|||||||
city=data.get("city").get("name"),
|
city=data.get("city").get("name"),
|
||||||
category=data.get("category_type"),
|
category=data.get("category_type"),
|
||||||
photo=Image._from_uri(data["profile_picture"]),
|
photo=Image._from_uri(data["profile_picture"]),
|
||||||
name=data.get("name"),
|
name=data["name"],
|
||||||
message_count=data.get("messages_count"),
|
message_count=data.get("messages_count"),
|
||||||
plan=plan,
|
plan=plan,
|
||||||
)
|
)
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import attr
|
import attr
|
||||||
from ._core import attrs_default, Enum, Image
|
from ._core import log, attrs_default, Enum, Image
|
||||||
from . import _util, _session, _plan, _thread
|
from . import _util, _session, _plan, _thread
|
||||||
|
|
||||||
|
|
||||||
@@ -81,23 +81,23 @@ class UserData(User):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The user's picture
|
#: The user's picture
|
||||||
photo = attr.ib(None)
|
photo = attr.ib()
|
||||||
#: The name of the user
|
#: The name of the user
|
||||||
name = attr.ib(None)
|
name = attr.ib()
|
||||||
|
#: Whether the user and the client are friends
|
||||||
|
is_friend = attr.ib()
|
||||||
|
#: The users first name
|
||||||
|
first_name = attr.ib()
|
||||||
|
#: The users last name
|
||||||
|
last_name = attr.ib(None)
|
||||||
#: Datetime when the thread was last active / when the last message was sent
|
#: Datetime when the thread was last active / when the last message was sent
|
||||||
last_active = attr.ib(None)
|
last_active = attr.ib(None)
|
||||||
#: Number of messages in the thread
|
#: Number of messages in the thread
|
||||||
message_count = attr.ib(None)
|
message_count = attr.ib(None)
|
||||||
#: Set `Plan`
|
#: Set `Plan`
|
||||||
plan = attr.ib(None)
|
plan = attr.ib(None)
|
||||||
#: The profile URL
|
#: The profile URL. ``None`` for Messenger-only users
|
||||||
url = attr.ib(None)
|
url = attr.ib(None)
|
||||||
#: The users first name
|
|
||||||
first_name = attr.ib(None)
|
|
||||||
#: The users last name
|
|
||||||
last_name = attr.ib(None)
|
|
||||||
#: Whether the user and the client are friends
|
|
||||||
is_friend = attr.ib(None)
|
|
||||||
#: The user's gender
|
#: The user's gender
|
||||||
gender = attr.ib(None)
|
gender = attr.ib(None)
|
||||||
#: From 0 to 1. How close the client is to the user
|
#: From 0 to 1. How close the client is to the user
|
||||||
@@ -123,18 +123,18 @@ class UserData(User):
|
|||||||
return cls(
|
return cls(
|
||||||
session=session,
|
session=session,
|
||||||
id=data["id"],
|
id=data["id"],
|
||||||
url=data.get("url"),
|
url=data["url"],
|
||||||
first_name=data.get("first_name"),
|
first_name=data["first_name"],
|
||||||
last_name=data.get("last_name"),
|
last_name=data.get("last_name"),
|
||||||
is_friend=data.get("is_viewer_friend"),
|
is_friend=data["is_viewer_friend"],
|
||||||
gender=GENDERS.get(data.get("gender")),
|
gender=GENDERS.get(data["gender"]),
|
||||||
affinity=data.get("viewer_affinity"),
|
affinity=data.get("viewer_affinity"),
|
||||||
nickname=c_info.get("nickname"),
|
nickname=c_info.get("nickname"),
|
||||||
color=c_info.get("color"),
|
color=c_info.get("color"),
|
||||||
emoji=c_info.get("emoji"),
|
emoji=c_info.get("emoji"),
|
||||||
own_nickname=c_info.get("own_nickname"),
|
own_nickname=c_info.get("own_nickname"),
|
||||||
photo=Image._from_uri(data["profile_picture"]),
|
photo=Image._from_uri(data["profile_picture"]),
|
||||||
name=data.get("name"),
|
name=data["name"],
|
||||||
message_count=data.get("messages_count"),
|
message_count=data.get("messages_count"),
|
||||||
plan=plan,
|
plan=plan,
|
||||||
)
|
)
|
||||||
@@ -150,16 +150,18 @@ class UserData(User):
|
|||||||
user = next(
|
user = next(
|
||||||
p for p in participants if p["id"] == data["thread_key"]["other_user_id"]
|
p for p in participants if p["id"] == data["thread_key"]["other_user_id"]
|
||||||
)
|
)
|
||||||
|
if user["__typename"] != "User":
|
||||||
|
# TODO: Add Page._from_thread_fetch, and parse it there
|
||||||
|
log.warning("Tried to parse %s as a user.", user["__typename"])
|
||||||
|
return None
|
||||||
|
|
||||||
last_active = None
|
last_active = None
|
||||||
if "last_message" in data:
|
if "last_message" in data:
|
||||||
last_active = _util.millis_to_datetime(
|
last_active = _util.millis_to_datetime(
|
||||||
int(data["last_message"]["nodes"][0]["timestamp_precise"])
|
int(data["last_message"]["nodes"][0]["timestamp_precise"])
|
||||||
)
|
)
|
||||||
|
|
||||||
first_name = user.get("short_name")
|
first_name = user["short_name"]
|
||||||
if first_name is None:
|
|
||||||
last_name = None
|
|
||||||
else:
|
|
||||||
last_name = user.get("name").split(first_name, 1).pop().strip()
|
last_name = user.get("name").split(first_name, 1).pop().strip()
|
||||||
|
|
||||||
plan = None
|
plan = None
|
||||||
@@ -169,19 +171,18 @@ class UserData(User):
|
|||||||
return cls(
|
return cls(
|
||||||
session=session,
|
session=session,
|
||||||
id=user["id"],
|
id=user["id"],
|
||||||
url=user.get("url"),
|
url=user["url"],
|
||||||
name=user.get("name"),
|
name=user["name"],
|
||||||
first_name=first_name,
|
first_name=first_name,
|
||||||
last_name=last_name,
|
last_name=last_name,
|
||||||
is_friend=user.get("is_viewer_friend"),
|
is_friend=user["is_viewer_friend"],
|
||||||
gender=GENDERS.get(user.get("gender")),
|
gender=GENDERS.get(user["gender"]),
|
||||||
affinity=user.get("affinity"),
|
|
||||||
nickname=c_info.get("nickname"),
|
nickname=c_info.get("nickname"),
|
||||||
color=c_info.get("color"),
|
color=c_info.get("color"),
|
||||||
emoji=c_info.get("emoji"),
|
emoji=c_info.get("emoji"),
|
||||||
own_nickname=c_info.get("own_nickname"),
|
own_nickname=c_info.get("own_nickname"),
|
||||||
photo=Image._from_uri(user["big_image_src"]),
|
photo=Image._from_uri(user["big_image_src"]),
|
||||||
message_count=data.get("messages_count"),
|
message_count=data["messages_count"],
|
||||||
last_active=last_active,
|
last_active=last_active,
|
||||||
plan=plan,
|
plan=plan,
|
||||||
)
|
)
|
||||||
@@ -191,12 +192,12 @@ class UserData(User):
|
|||||||
return cls(
|
return cls(
|
||||||
session=session,
|
session=session,
|
||||||
id=data["id"],
|
id=data["id"],
|
||||||
first_name=data.get("firstName"),
|
first_name=data["firstName"],
|
||||||
url=data.get("uri"),
|
url=data["uri"],
|
||||||
photo=Image(url=data.get("thumbSrc")),
|
photo=Image(url=data["thumbSrc"]),
|
||||||
name=data.get("name"),
|
name=data["name"],
|
||||||
is_friend=data.get("is_friend"),
|
is_friend=data["is_friend"],
|
||||||
gender=GENDERS.get(data.get("gender")),
|
gender=GENDERS.get(data["gender"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user