-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
poll.py
76 lines (59 loc) · 2.16 KB
/
poll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Tweepy
# Copyright 2009-2023 Joshua Roesslein
# See LICENSE for details.
from tweepy.mixins import DataMapping, HashableID
from tweepy.utils import parse_datetime
#: All the potential fields for :class:`Poll` objects
POLL_FIELDS = [
"duration_minutes",
"end_datetime",
"id",
"options",
"voting_status",
]
class Poll(HashableID, DataMapping):
"""A poll included in a Tweet is not a primary object on any endpoint, but
can be found and expanded in the Tweet object.
The object is available for expansion with
``?expansions=attachments.poll_ids`` to get the condensed object with only
default fields. Use the expansion with the field parameter: ``poll.fields``
when requesting additional fields to complete the object.
.. versionadded:: 4.0
Attributes
----------
data : dict
The JSON data representing the poll.
id : str
Unique identifier of the expanded poll.
options : list
Contains objects describing each choice in the referenced poll.
duration_minutes : int | None
Specifies the total duration of this poll.
end_datetime : datetime.datetime | None
Specifies the end date and time for this poll.
voting_status : str | None
Indicates if this poll is still active and can receive votes, or if the
voting is now closed.
References
----------
https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/poll
"""
__slots__ = (
"data", "id", "options", "duration_minutes", "end_datetime",
"voting_status"
)
def __init__(self, data):
self.data = data
self.id = data["id"]
self.options = data["options"]
self.duration_minutes = data.get("duration_minutes")
self.end_datetime = data.get("end_datetime")
if self.end_datetime is not None:
self.end_datetime = parse_datetime(self.end_datetime)
self.voting_status = data.get("voting_status")
def __iter__(self):
return iter(self.options)
def __len__(self):
return len(self.options)
def __repr__(self):
return f"<Poll id={self.id} options={self.options}>"