Merge pull request #399 from carpedm20/attrs

Use attrs to declare our models
This commit is contained in:
Mads Marquart
2019-02-24 20:24:21 +01:00
committed by GitHub
14 changed files with 266 additions and 366 deletions

View File

@@ -174,5 +174,5 @@ html_sidebars = {"**": ["sidebar.html", "searchbox.html"]}
html_show_sphinx = False html_show_sphinx = False
html_show_sourcelink = False html_show_sourcelink = False
autoclass_content = "init" autoclass_content = "both"
html_short_title = description html_short_title = description

View File

@@ -1,73 +1,48 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
@attr.s(cmp=False)
class Attachment(object): class Attachment(object):
#: The attachment ID
uid = None
def __init__(self, uid=None):
"""Represents a Facebook attachment""" """Represents a Facebook attachment"""
self.uid = uid
#: The attachment ID
uid = attr.ib(None)
@attr.s(cmp=False)
class UnsentMessage(Attachment): class UnsentMessage(Attachment):
def __init__(self, *args, **kwargs):
"""Represents an unsent message attachment""" """Represents an unsent message attachment"""
super(UnsentMessage, self).__init__(*args, **kwargs)
@attr.s(cmp=False)
class ShareAttachment(Attachment): class ShareAttachment(Attachment):
#: ID of the author of the shared post
author = None
#: Target URL
url = None
#: Original URL if Facebook redirects the URL
original_url = None
#: Title of the attachment
title = None
#: Description of the attachment
description = None
#: Name of the source
source = None
#: URL of the attachment image
image_url = None
#: URL of the original image if Facebook uses `safe_image`
original_image_url = None
#: Width of the image
image_width = None
#: Height of the image
image_height = None
#: List of additional attachments
attachments = None
def __init__(
self,
author=None,
url=None,
original_url=None,
title=None,
description=None,
source=None,
image_url=None,
original_image_url=None,
image_width=None,
image_height=None,
attachments=None,
**kwargs
):
"""Represents a shared item (eg. URL) that has been sent as a Facebook attachment""" """Represents a shared item (eg. URL) that has been sent as a Facebook attachment"""
super(ShareAttachment, self).__init__(**kwargs)
self.author = author #: ID of the author of the shared post
self.url = url author = attr.ib(None)
self.original_url = original_url #: Target URL
self.title = title url = attr.ib(None)
self.description = description #: Original URL if Facebook redirects the URL
self.source = source original_url = attr.ib(None)
self.image_url = image_url #: Title of the attachment
self.original_image_url = original_image_url title = attr.ib(None)
self.image_width = image_width #: Description of the attachment
self.image_height = image_height description = attr.ib(None)
if attachments is None: #: Name of the source
attachments = [] source = attr.ib(None)
self.attachments = attachments #: URL of the attachment image
image_url = attr.ib(None)
#: URL of the original image if Facebook uses `safe_image`
original_image_url = attr.ib(None)
#: Width of the image
image_width = attr.ib(None)
#: Height of the image
image_height = attr.ib(None)
#: List of additional attachments
attachments = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
# Put here for backwards compatibility, so that the init argument order is preserved
uid = attr.ib(None)

View File

@@ -1,83 +1,85 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._attachment import Attachment from ._attachment import Attachment
@attr.s(cmp=False)
class FileAttachment(Attachment): class FileAttachment(Attachment):
#: Url where you can download the file
url = None
#: Size of the file in bytes
size = None
#: Name of the file
name = None
#: Whether Facebook determines that this file may be harmful
is_malicious = None
def __init__(self, url=None, size=None, name=None, is_malicious=None, **kwargs):
"""Represents a file that has been sent as a Facebook attachment""" """Represents a file that has been sent as a Facebook attachment"""
super(FileAttachment, self).__init__(**kwargs)
self.url = url
self.size = size
self.name = name
self.is_malicious = is_malicious
#: Url where you can download the file
class AudioAttachment(Attachment): url = attr.ib(None)
#: Size of the file in bytes
size = attr.ib(None)
#: Name of the file #: Name of the file
filename = None name = attr.ib(None)
#: Url of the audio file #: Whether Facebook determines that this file may be harmful
url = None is_malicious = attr.ib(None)
#: Duration of the audioclip in milliseconds
duration = None
#: Audio type
audio_type = None
def __init__( # Put here for backwards compatibility, so that the init argument order is preserved
self, filename=None, url=None, duration=None, audio_type=None, **kwargs uid = attr.ib(None)
):
@attr.s(cmp=False)
class AudioAttachment(Attachment):
"""Represents an audio file that has been sent as a Facebook attachment""" """Represents an audio file that has been sent as a Facebook attachment"""
super(AudioAttachment, self).__init__(**kwargs)
self.filename = filename #: Name of the file
self.url = url filename = attr.ib(None)
self.duration = duration #: Url of the audio file
self.audio_type = audio_type url = attr.ib(None)
#: Duration of the audioclip in milliseconds
duration = attr.ib(None)
#: Audio type
audio_type = attr.ib(None)
# Put here for backwards compatibility, so that the init argument order is preserved
uid = attr.ib(None)
@attr.s(cmp=False, init=False)
class ImageAttachment(Attachment): class ImageAttachment(Attachment):
"""Represents an image that has been sent as a Facebook attachment
To retrieve the full image url, use: :func:`fbchat.Client.fetchImageUrl`, and pass
it the uid of the image attachment
"""
#: The extension of the original image (eg. 'png') #: The extension of the original image (eg. 'png')
original_extension = None original_extension = attr.ib(None)
#: Width of original image #: Width of original image
width = None width = attr.ib(None, converter=lambda x: None if x is None else int(x))
#: Height of original image #: Height of original image
height = None height = attr.ib(None, converter=lambda x: None if x is None else int(x))
#: Whether the image is animated #: Whether the image is animated
is_animated = None is_animated = attr.ib(None)
#: URL to a thumbnail of the image #: URL to a thumbnail of the image
thumbnail_url = None thumbnail_url = attr.ib(None)
#: URL to a medium preview of the image #: URL to a medium preview of the image
preview_url = None preview_url = attr.ib(None)
#: Width of the medium preview image #: Width of the medium preview image
preview_width = None preview_width = attr.ib(None)
#: Height of the medium preview image #: Height of the medium preview image
preview_height = None preview_height = attr.ib(None)
#: URL to a large preview of the image #: URL to a large preview of the image
large_preview_url = None large_preview_url = attr.ib(None)
#: Width of the large preview image #: Width of the large preview image
large_preview_width = None large_preview_width = attr.ib(None)
#: Height of the large preview image #: Height of the large preview image
large_preview_height = None large_preview_height = attr.ib(None)
#: URL to an animated preview of the image (eg. for gifs) #: URL to an animated preview of the image (eg. for gifs)
animated_preview_url = None animated_preview_url = attr.ib(None)
#: Width of the animated preview image #: Width of the animated preview image
animated_preview_width = None animated_preview_width = attr.ib(None)
#: Height of the animated preview image #: Height of the animated preview image
animated_preview_height = None animated_preview_height = attr.ib(None)
def __init__( def __init__(
self, self,
@@ -91,11 +93,6 @@ class ImageAttachment(Attachment):
animated_preview=None, animated_preview=None,
**kwargs **kwargs
): ):
"""
Represents an image that has been sent as a Facebook attachment
To retrieve the full image url, use: :func:`fbchat.Client.fetchImageUrl`,
and pass it the uid of the image attachment
"""
super(ImageAttachment, self).__init__(**kwargs) super(ImageAttachment, self).__init__(**kwargs)
self.original_extension = original_extension self.original_extension = original_extension
if width is not None: if width is not None:
@@ -126,38 +123,41 @@ class ImageAttachment(Attachment):
self.animated_preview_height = animated_preview.get("height") self.animated_preview_height = animated_preview.get("height")
@attr.s(cmp=False, init=False)
class VideoAttachment(Attachment): class VideoAttachment(Attachment):
"""Represents a video that has been sent as a Facebook attachment"""
#: Size of the original video in bytes #: Size of the original video in bytes
size = None size = attr.ib(None)
#: Width of original video #: Width of original video
width = None width = attr.ib(None)
#: Height of original video #: Height of original video
height = None height = attr.ib(None)
#: Length of video in milliseconds #: Length of video in milliseconds
duration = None duration = attr.ib(None)
#: URL to very compressed preview video #: URL to very compressed preview video
preview_url = None preview_url = attr.ib(None)
#: URL to a small preview image of the video #: URL to a small preview image of the video
small_image_url = None small_image_url = attr.ib(None)
#: Width of the small preview image #: Width of the small preview image
small_image_width = None small_image_width = attr.ib(None)
#: Height of the small preview image #: Height of the small preview image
small_image_height = None small_image_height = attr.ib(None)
#: URL to a medium preview image of the video #: URL to a medium preview image of the video
medium_image_url = None medium_image_url = attr.ib(None)
#: Width of the medium preview image #: Width of the medium preview image
medium_image_width = None medium_image_width = attr.ib(None)
#: Height of the medium preview image #: Height of the medium preview image
medium_image_height = None medium_image_height = attr.ib(None)
#: URL to a large preview image of the video #: URL to a large preview image of the video
large_image_url = None large_image_url = attr.ib(None)
#: Width of the large preview image #: Width of the large preview image
large_image_width = None large_image_width = attr.ib(None)
#: Height of the large preview image #: Height of the large preview image
large_image_height = None large_image_height = attr.ib(None)
def __init__( def __init__(
self, self,
@@ -171,7 +171,6 @@ class VideoAttachment(Attachment):
large_image=None, large_image=None,
**kwargs **kwargs
): ):
"""Represents a video that has been sent as a Facebook attachment"""
super(VideoAttachment, self).__init__(**kwargs) super(VideoAttachment, self).__init__(**kwargs)
self.size = size self.size = size
self.width = width self.width = width

View File

@@ -1,26 +1,32 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._thread import ThreadType, Thread from ._thread import ThreadType, Thread
@attr.s(cmp=False, init=False)
class Group(Thread): class Group(Thread):
"""Represents a Facebook group. Inherits `Thread`"""
#: Unique list (set) of the group thread's participant user IDs #: Unique list (set) of the group thread's participant user IDs
participants = None participants = attr.ib(factory=set, converter=lambda x: set() if x is None else x)
#: A dict, containing user nicknames mapped to their IDs #: A dict, containing user nicknames mapped to their IDs
nicknames = None nicknames = attr.ib(factory=dict, converter=lambda x: {} if x is None else x)
#: A :class:`ThreadColor`. The groups's message color #: A :class:`ThreadColor`. The groups's message color
color = None color = attr.ib(None)
#: The groups's default emoji #: The groups's default emoji
emoji = None emoji = attr.ib(None)
# Set containing user IDs of thread admins # Set containing user IDs of thread admins
admins = None admins = attr.ib(factory=set, converter=lambda x: set() if x is None else x)
# True if users need approval to join # True if users need approval to join
approval_mode = None approval_mode = attr.ib(None)
# Set containing user IDs requesting to join # Set containing user IDs requesting to join
approval_requests = None approval_requests = attr.ib(
factory=set, converter=lambda x: set() if x is None else x
)
# Link for joining group # Link for joining group
join_link = None join_link = attr.ib(None)
def __init__( def __init__(
self, self,
@@ -36,7 +42,6 @@ class Group(Thread):
privacy_mode=None, privacy_mode=None,
**kwargs **kwargs
): ):
"""Represents a Facebook group. Inherits `Thread`"""
super(Group, self).__init__(ThreadType.GROUP, uid, **kwargs) super(Group, self).__init__(ThreadType.GROUP, uid, **kwargs)
if participants is None: if participants is None:
participants = set() participants = set()
@@ -56,12 +61,14 @@ class Group(Thread):
self.join_link = join_link self.join_link = join_link
@attr.s(cmp=False, init=False)
class Room(Group): class Room(Group):
"""Deprecated. Use :class:`Group` instead"""
# True is room is not discoverable # True is room is not discoverable
privacy_mode = None privacy_mode = attr.ib(None)
def __init__(self, uid, privacy_mode=None, **kwargs): def __init__(self, uid, privacy_mode=None, **kwargs):
"""Deprecated. Use :class:`Group` instead"""
super(Room, self).__init__(uid, **kwargs) super(Room, self).__init__(uid, **kwargs)
self.type = ThreadType.ROOM self.type = ThreadType.ROOM
self.privacy_mode = privacy_mode self.privacy_mode = privacy_mode

View File

@@ -1,45 +1,48 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._attachment import Attachment from ._attachment import Attachment
@attr.s(cmp=False)
class LocationAttachment(Attachment): class LocationAttachment(Attachment):
"""Latitude and longitude OR address is provided by Facebook""" """Represents a user location
Latitude and longitude OR address is provided by Facebook
"""
#: Latitude of the location #: Latitude of the location
latitude = None latitude = attr.ib(None)
#: Longitude of the location #: Longitude of the location
longitude = None longitude = attr.ib(None)
#: URL of image showing the map of the location #: URL of image showing the map of the location
image_url = None image_url = attr.ib(None, init=False)
#: Width of the image #: Width of the image
image_width = None image_width = attr.ib(None, init=False)
#: Height of the image #: Height of the image
image_height = None image_height = attr.ib(None, init=False)
#: URL to Bing maps with the location #: URL to Bing maps with the location
url = None url = attr.ib(None, init=False)
# Address of the location # Address of the location
address = None address = attr.ib(None)
def __init__(self, latitude=None, longitude=None, address=None, **kwargs): # Put here for backwards compatibility, so that the init argument order is preserved
"""Represents a user location""" uid = attr.ib(None)
super(LocationAttachment, self).__init__(**kwargs)
self.latitude = latitude
self.longitude = longitude
self.address = address
@attr.s(cmp=False, init=False)
class LiveLocationAttachment(LocationAttachment): class LiveLocationAttachment(LocationAttachment):
"""Represents a live user location"""
#: Name of the location #: Name of the location
name = None name = attr.ib(None)
#: Timestamp when live location expires #: Timestamp when live location expires
expiration_time = None expiration_time = attr.ib(None)
#: True if live location is expired #: True if live location is expired
is_expired = None is_expired = attr.ib(None)
def __init__(self, name=None, expiration_time=None, is_expired=None, **kwargs): def __init__(self, name=None, expiration_time=None, is_expired=None, **kwargs):
"""Represents a live user location"""
super(LiveLocationAttachment, self).__init__(**kwargs) super(LiveLocationAttachment, self).__init__(**kwargs)
self.expiration_time = expiration_time self.expiration_time = expiration_time
self.is_expired = is_expired self.is_expired = is_expired

View File

@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from string import Formatter from string import Formatter
from ._core import Enum from ._core import Enum
@@ -25,92 +26,48 @@ class MessageReaction(Enum):
NO = "👎" NO = "👎"
@attr.s(cmp=False)
class Mention(object): class Mention(object):
#: The thread ID the mention is pointing at
thread_id = None
#: The character where the mention starts
offset = None
#: The length of the mention
length = None
def __init__(self, thread_id, offset=0, length=10):
"""Represents a @mention""" """Represents a @mention"""
self.thread_id = thread_id
self.offset = offset
self.length = length
def __repr__(self): #: The thread ID the mention is pointing at
return self.__unicode__() thread_id = attr.ib()
#: The character where the mention starts
def __unicode__(self): offset = attr.ib(0)
return "<Mention {}: offset={} length={}>".format( #: The length of the mention
self.thread_id, self.offset, self.length length = attr.ib(10)
)
@attr.s(cmp=False)
class Message(object): class Message(object):
#: The actual message
text = None
#: A list of :class:`Mention` objects
mentions = None
#: A :class:`EmojiSize`. Size of a sent emoji
emoji_size = None
#: The message ID
uid = None
#: ID of the sender
author = None
#: Timestamp of when the message was sent
timestamp = None
#: Whether the message is read
is_read = None
#: A list of pepole IDs who read the message, works only with :func:`fbchat.Client.fetchThreadMessages`
read_by = None
#: A dict with user's IDs as keys, and their :class:`MessageReaction` as values
reactions = None
#: The actual message
text = None
#: A :class:`Sticker`
sticker = None
#: A list of attachments
attachments = None
#: A list of :class:`QuickReply`
quick_replies = None
#: Whether the message is unsent (deleted for everyone)
unsent = None
def __init__(
self,
text=None,
mentions=None,
emoji_size=None,
sticker=None,
attachments=None,
quick_replies=None,
):
"""Represents a Facebook message""" """Represents a Facebook message"""
self.text = text
if mentions is None:
mentions = []
self.mentions = mentions
self.emoji_size = emoji_size
self.sticker = sticker
if attachments is None:
attachments = []
self.attachments = attachments
if quick_replies is None:
quick_replies = []
self.quick_replies = quick_replies
self.reactions = {}
self.read_by = []
self.deleted = False
def __repr__(self): #: The actual message
return self.__unicode__() text = attr.ib(None)
#: A list of :class:`Mention` objects
def __unicode__(self): mentions = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
return "<Message ({}): {}, mentions={} emoji_size={} attachments={}>".format( #: A :class:`EmojiSize`. Size of a sent emoji
self.uid, repr(self.text), self.mentions, self.emoji_size, self.attachments emoji_size = attr.ib(None)
) #: The message ID
uid = attr.ib(None, init=False)
#: ID of the sender
author = attr.ib(None, init=False)
#: Timestamp of when the message was sent
timestamp = attr.ib(None, init=False)
#: Whether the message is read
is_read = attr.ib(None, init=False)
#: A list of pepole IDs who read the message, works only with :func:`fbchat.Client.fetchThreadMessages`
read_by = attr.ib(factory=list, init=False)
#: A dict with user's IDs as keys, and their :class:`MessageReaction` as values
reactions = attr.ib(factory=dict, init=False)
#: A :class:`Sticker`
sticker = attr.ib(None)
#: A list of attachments
attachments = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
#: A list of :class:`QuickReply`
quick_replies = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
#: Whether the message is unsent (deleted for everyone)
unsent = attr.ib(False, init=False)
@classmethod @classmethod
def formatMentions(cls, text, *args, **kwargs): def formatMentions(cls, text, *args, **kwargs):

View File

@@ -1,20 +1,24 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._thread import ThreadType, Thread from ._thread import ThreadType, Thread
@attr.s(cmp=False, init=False)
class Page(Thread): class Page(Thread):
"""Represents a Facebook page. Inherits `Thread`"""
#: The page's custom url #: The page's custom url
url = None url = attr.ib(None)
#: The name of the page's location city #: The name of the page's location city
city = None city = attr.ib(None)
#: Amount of likes the page has #: Amount of likes the page has
likes = None likes = attr.ib(None)
#: Some extra information about the page #: Some extra information about the page
sub_title = None sub_title = attr.ib(None)
#: The page's category #: The page's category
category = None category = attr.ib(None)
def __init__( def __init__(
self, self,
@@ -26,7 +30,6 @@ class Page(Thread):
category=None, category=None,
**kwargs **kwargs
): ):
"""Represents a Facebook page. Inherits `Thread`"""
super(Page, self).__init__(ThreadType.PAGE, uid, **kwargs) super(Page, self).__init__(ThreadType.PAGE, uid, **kwargs)
self.url = url self.url = url
self.city = city self.city = city

View File

@@ -1,46 +1,28 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
@attr.s(cmp=False)
class Plan(object): class Plan(object):
#: ID of the plan
uid = None
#: Plan time (unix time stamp), only precise down to the minute
time = None
#: Plan title
title = None
#: Plan location name
location = None
#: Plan location ID
location_id = None
#: ID of the plan creator
author_id = None
#: List of the people IDs who will take part in the plan
going = None
#: List of the people IDs who won't take part in the plan
declined = None
#: List of the people IDs who are invited to the plan
invited = None
def __init__(self, time, title, location=None, location_id=None):
"""Represents a plan""" """Represents a plan"""
self.time = int(time)
self.title = title
self.location = location or ""
self.location_id = location_id or ""
self.author_id = None
self.going = []
self.declined = []
self.invited = []
def __repr__(self): #: ID of the plan
return self.__unicode__() uid = attr.ib(None, init=False)
#: Plan time (unix time stamp), only precise down to the minute
def __unicode__(self): time = attr.ib(converter=int)
return "<Plan ({}): {} time={}, location={}, location_id={}>".format( #: Plan title
self.uid, title = attr.ib()
repr(self.title), #: Plan location name
self.time, location = attr.ib(None, converter=lambda x: x or "")
repr(self.location), #: Plan location ID
repr(self.location_id), location_id = attr.ib(None, converter=lambda x: x or "")
) #: ID of the plan creator
author_id = attr.ib(None, init=False)
#: List of the people IDs who will take part in the plan
going = attr.ib(factory=list, init=False)
#: List of the people IDs who won't take part in the plan
declined = attr.ib(factory=list, init=False)
#: List of the people IDs who are invited to the plan
invited = attr.ib(factory=list, init=False)

View File

@@ -1,52 +1,34 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
@attr.s(cmp=False)
class Poll(object): class Poll(object):
#: ID of the poll
uid = None
#: Title of the poll
title = None
#: List of :class:`PollOption`, can be fetched with :func:`fbchat.Client.fetchPollOptions`
options = None
#: Options count
options_count = None
def __init__(self, title, options):
"""Represents a poll""" """Represents a poll"""
self.title = title
self.options = options
def __repr__(self): #: ID of the poll
return self.__unicode__() uid = attr.ib(None, init=False)
#: Title of the poll
def __unicode__(self): title = attr.ib()
return "<Poll ({}): {} options={}>".format( #: List of :class:`PollOption`, can be fetched with :func:`fbchat.Client.fetchPollOptions`
self.uid, repr(self.title), self.options options = attr.ib()
) #: Options count
options_count = attr.ib(None, init=False)
@attr.s(cmp=False)
class PollOption(object): class PollOption(object):
#: ID of the poll option
uid = None
#: Text of the poll option
text = None
#: Whether vote when creating or client voted
vote = None
#: ID of the users who voted for this poll option
voters = None
#: Votes count
votes_count = None
def __init__(self, text, vote=False):
"""Represents a poll option""" """Represents a poll option"""
self.text = text
self.vote = vote
def __repr__(self): #: ID of the poll option
return self.__unicode__() uid = attr.ib(None, init=False)
#: Text of the poll option
def __unicode__(self): text = attr.ib()
return "<PollOption ({}): {} voters={}>".format( #: Whether vote when creating or client voted
self.uid, repr(self.text), self.voters vote = attr.ib(False)
) #: ID of the users who voted for this poll option
voters = attr.ib(None, init=False)
#: Votes count
votes_count = attr.ib(None, init=False)

View File

@@ -1,76 +1,76 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._attachment import Attachment from ._attachment import Attachment
@attr.s(cmp=False)
class QuickReply(object): class QuickReply(object):
#: Payload of the quick reply
payload = None
#: External payload for responses
external_payload = None
#: Additional data
data = None
#: Whether it's a response for a quick reply
is_response = None
def __init__(self, payload=None, data=None, is_response=False):
"""Represents a quick reply""" """Represents a quick reply"""
self.payload = payload
self.data = data
self.is_response = is_response
def __repr__(self): #: Payload of the quick reply
return self.__unicode__() payload = attr.ib(None)
#: External payload for responses
def __unicode__(self): external_payload = attr.ib(None, init=False)
return "<{}: payload={!r}>".format(self.__class__.__name__, self.payload) #: Additional data
data = attr.ib(None)
#: Whether it's a response for a quick reply
is_response = attr.ib(False)
@attr.s(cmp=False, init=False)
class QuickReplyText(QuickReply): class QuickReplyText(QuickReply):
"""Represents a text quick reply"""
#: Title of the quick reply #: Title of the quick reply
title = None title = attr.ib(None)
#: URL of the quick reply image (optional) #: URL of the quick reply image (optional)
image_url = None image_url = attr.ib(None)
#: Type of the quick reply #: Type of the quick reply
_type = "text" _type = "text"
def __init__(self, title=None, image_url=None, **kwargs): def __init__(self, title=None, image_url=None, **kwargs):
"""Represents a text quick reply"""
super(QuickReplyText, self).__init__(**kwargs) super(QuickReplyText, self).__init__(**kwargs)
self.title = title self.title = title
self.image_url = image_url self.image_url = image_url
@attr.s(cmp=False, init=False)
class QuickReplyLocation(QuickReply): class QuickReplyLocation(QuickReply):
"""Represents a location quick reply (Doesn't work on mobile)"""
#: Type of the quick reply #: Type of the quick reply
_type = "location" _type = "location"
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""Represents a location quick reply (Doesn't work on mobile)"""
super(QuickReplyLocation, self).__init__(**kwargs) super(QuickReplyLocation, self).__init__(**kwargs)
self.is_response = False self.is_response = False
@attr.s(cmp=False, init=False)
class QuickReplyPhoneNumber(QuickReply): class QuickReplyPhoneNumber(QuickReply):
"""Represents a phone number quick reply (Doesn't work on mobile)"""
#: URL of the quick reply image (optional) #: URL of the quick reply image (optional)
image_url = None image_url = attr.ib(None)
#: Type of the quick reply #: Type of the quick reply
_type = "user_phone_number" _type = "user_phone_number"
def __init__(self, image_url=None, **kwargs): def __init__(self, image_url=None, **kwargs):
"""Represents a phone number quick reply (Doesn't work on mobile)"""
super(QuickReplyPhoneNumber, self).__init__(**kwargs) super(QuickReplyPhoneNumber, self).__init__(**kwargs)
self.image_url = image_url self.image_url = image_url
@attr.s(cmp=False, init=False)
class QuickReplyEmail(QuickReply): class QuickReplyEmail(QuickReply):
"""Represents an email quick reply (Doesn't work on mobile)"""
#: URL of the quick reply image (optional) #: URL of the quick reply image (optional)
image_url = None image_url = attr.ib(None)
#: Type of the quick reply #: Type of the quick reply
_type = "user_email" _type = "user_email"
def __init__(self, image_url=None, **kwargs): def __init__(self, image_url=None, **kwargs):
"""Represents an email quick reply (Doesn't work on mobile)"""
super(QuickReplyEmail, self).__init__(**kwargs) super(QuickReplyEmail, self).__init__(**kwargs)
self.image_url = image_url self.image_url = image_url

View File

@@ -1,36 +1,39 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._attachment import Attachment from ._attachment import Attachment
@attr.s(cmp=False, init=False)
class Sticker(Attachment): class Sticker(Attachment):
"""Represents a Facebook sticker that has been sent to a thread as an attachment"""
#: The sticker-pack's ID #: The sticker-pack's ID
pack = None pack = attr.ib(None)
#: Whether the sticker is animated #: Whether the sticker is animated
is_animated = False is_animated = attr.ib(False)
# If the sticker is animated, the following should be present # If the sticker is animated, the following should be present
#: URL to a medium spritemap #: URL to a medium spritemap
medium_sprite_image = None medium_sprite_image = attr.ib(None)
#: URL to a large spritemap #: URL to a large spritemap
large_sprite_image = None large_sprite_image = attr.ib(None)
#: The amount of frames present in the spritemap pr. row #: The amount of frames present in the spritemap pr. row
frames_per_row = None frames_per_row = attr.ib(None)
#: The amount of frames present in the spritemap pr. coloumn #: The amount of frames present in the spritemap pr. coloumn
frames_per_col = None frames_per_col = attr.ib(None)
#: The frame rate the spritemap is intended to be played in #: The frame rate the spritemap is intended to be played in
frame_rate = None frame_rate = attr.ib(None)
#: URL to the sticker's image #: URL to the sticker's image
url = None url = attr.ib(None)
#: Width of the sticker #: Width of the sticker
width = None width = attr.ib(None)
#: Height of the sticker #: Height of the sticker
height = None height = attr.ib(None)
#: The sticker's label/name #: The sticker's label/name
label = None label = attr.ib(None)
def __init__(self, *args, **kwargs): def __init__(self, uid=None):
"""Represents a Facebook sticker that has been sent to a Facebook thread as an attachment""" super(Sticker, self).__init__(uid=uid)
super(Sticker, self).__init__(*args, **kwargs)

View File

@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._core import Enum from ._core import Enum
@@ -42,21 +43,24 @@ class ThreadColor(Enum):
BILOBA_FLOWER = "#a695c7" BILOBA_FLOWER = "#a695c7"
@attr.s(cmp=False, init=False)
class Thread(object): class Thread(object):
"""Represents a Facebook thread"""
#: The unique identifier of the thread. Can be used a `thread_id`. See :ref:`intro_threads` for more info #: The unique identifier of the thread. Can be used a `thread_id`. See :ref:`intro_threads` for more info
uid = None uid = attr.ib(converter=str)
#: Specifies the type of thread. Can be used a `thread_type`. See :ref:`intro_threads` for more info #: Specifies the type of thread. Can be used a `thread_type`. See :ref:`intro_threads` for more info
type = None type = attr.ib()
#: A url to the thread's picture #: A url to the thread's picture
photo = None photo = attr.ib(None)
#: The name of the thread #: The name of the thread
name = None name = attr.ib(None)
#: Timestamp of last message #: Timestamp of last message
last_message_timestamp = None last_message_timestamp = attr.ib(None)
#: Number of messages in the thread #: Number of messages in the thread
message_count = None message_count = attr.ib(None)
#: Set :class:`Plan` #: Set :class:`Plan`
plan = None plan = attr.ib(None)
def __init__( def __init__(
self, self,
@@ -68,7 +72,6 @@ class Thread(object):
message_count=None, message_count=None,
plan=None, plan=None,
): ):
"""Represents a Facebook thread"""
self.uid = str(uid) self.uid = str(uid)
self.type = _type self.type = _type
self.photo = photo self.photo = photo
@@ -76,9 +79,3 @@ class Thread(object):
self.last_message_timestamp = last_message_timestamp self.last_message_timestamp = last_message_timestamp
self.message_count = message_count self.message_count = message_count
self.plan = plan self.plan = plan
def __repr__(self):
return self.__unicode__()
def __unicode__(self):
return "<{} {} ({})>".format(self.type.name, self.name, self.uid)

View File

@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import attr
from ._core import Enum from ._core import Enum
from ._thread import ThreadType, Thread from ._thread import ThreadType, Thread
@@ -12,27 +13,30 @@ class TypingStatus(Enum):
TYPING = 1 TYPING = 1
@attr.s(cmp=False, init=False)
class User(Thread): class User(Thread):
"""Represents a Facebook user. Inherits `Thread`"""
#: The profile url #: The profile url
url = None url = attr.ib(None)
#: The users first name #: The users first name
first_name = None first_name = attr.ib(None)
#: The users last name #: The users last name
last_name = None last_name = attr.ib(None)
#: Whether the user and the client are friends #: Whether the user and the client are friends
is_friend = None is_friend = attr.ib(None)
#: The user's gender #: The user's gender
gender = 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
affinity = None affinity = attr.ib(None)
#: The user's nickname #: The user's nickname
nickname = None nickname = attr.ib(None)
#: The clients nickname, as seen by the user #: The clients nickname, as seen by the user
own_nickname = None own_nickname = attr.ib(None)
#: A :class:`ThreadColor`. The message color #: A :class:`ThreadColor`. The message color
color = None color = attr.ib(None)
#: The default emoji #: The default emoji
emoji = None emoji = attr.ib(None)
def __init__( def __init__(
self, self,
@@ -49,7 +53,6 @@ class User(Thread):
emoji=None, emoji=None,
**kwargs **kwargs
): ):
"""Represents a Facebook user. Inherits `Thread`"""
super(User, self).__init__(ThreadType.USER, uid, **kwargs) super(User, self).__init__(ThreadType.USER, uid, **kwargs)
self.url = url self.url = url
self.first_name = first_name self.first_name = first_name
@@ -63,23 +66,11 @@ class User(Thread):
self.emoji = emoji self.emoji = emoji
@attr.s(cmp=False)
class ActiveStatus(object): class ActiveStatus(object):
#: Whether the user is active now #: Whether the user is active now
active = None active = attr.ib(None)
#: Timestamp when the user was last active #: Timestamp when the user was last active
last_active = None last_active = attr.ib(None)
#: Whether the user is playing Messenger game now #: Whether the user is playing Messenger game now
in_game = None in_game = attr.ib(None)
def __init__(self, active=None, last_active=None, in_game=None):
self.active = active
self.last_active = last_active
self.in_game = in_game
def __repr__(self):
return self.__unicode__()
def __unicode__(self):
return "<ActiveStatus: active={} last_active={} in_game={}>".format(
self.active, self.last_active, self.in_game
)

View File

@@ -23,6 +23,7 @@ maintainer-email = "madsmtm@gmail.com"
home-page = "https://github.com/carpedm20/fbchat/" home-page = "https://github.com/carpedm20/fbchat/"
requires = [ requires = [
"aenum", "aenum",
"attrs~=18.2.0",
"requests", "requests",
"beautifulsoup4", "beautifulsoup4",
] ]