1
1
from __future__ import annotations
2
2
3
+ import re
3
4
from datetime import datetime
4
5
from typing import TYPE_CHECKING
5
6
@@ -159,10 +160,22 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
159
160
i ['text' ] for i in hashtags
160
161
]
161
162
163
+ if (
164
+ 'card' in data and
165
+ data ['card' ]['legacy' ]['name' ].startswith ('poll' )
166
+ ):
167
+ self ._poll_data = data ['card' ]
168
+ else :
169
+ self ._poll_data = None
170
+
162
171
@property
163
172
def created_at_datetime (self ) -> datetime :
164
173
return timestamp_to_datetime (self .created_at )
165
174
175
+ @property
176
+ def poll (self ) -> Poll :
177
+ return self ._poll_data and Poll (self ._client , self ._poll_data , self )
178
+
166
179
async def delete (self ) -> Response :
167
180
"""Deletes the tweet.
168
181
@@ -405,4 +418,108 @@ async def delete(self) -> Response:
405
418
return await self ._client .delete_scheduled_tweet (self .id )
406
419
407
420
def __repr__ (self ) -> str :
408
- return f'<ScheduledTweet id="{ self .id } ">'
421
+ return f'<ScheduledTweet id="{ self .id } ">'
422
+
423
+
424
+ class Poll :
425
+ """Represents a poll associated with a tweet.
426
+ Attributes
427
+ ----------
428
+ tweet : Tweet
429
+ The tweet associated with the poll.
430
+ id : str
431
+ The unique identifier of the poll.
432
+ name : str
433
+ The name of the poll.
434
+ choices : list of dict
435
+ A list containing dictionaries representing poll choices.
436
+ Each dictionary contains 'label' and 'count' keys
437
+ for choice label and count.
438
+ duration_minutes : int
439
+ The duration of the poll in minutes.
440
+ end_datetime_utc : str
441
+ The end date and time of the poll in UTC format.
442
+ last_updated_datetime_utc : str
443
+ The last updated date and time of the poll in UTC format.
444
+ selected_choice : str | None
445
+ Number of the selected choice.
446
+ """
447
+
448
+ def __init__ (
449
+ self , client : Client , data : dict , tweet : Tweet | None = None
450
+ ) -> None :
451
+ self ._client = client
452
+ self .tweet = tweet
453
+
454
+ legacy = data ['legacy' ]
455
+ binding_values = legacy ['binding_values' ]
456
+
457
+ if isinstance (legacy ['binding_values' ], list ):
458
+ binding_values = {
459
+ i .get ('key' ): i .get ('value' )
460
+ for i in legacy ['binding_values' ]
461
+ }
462
+
463
+ self .id : str = data ['rest_id' ]
464
+ self .name : str = legacy ['name' ]
465
+
466
+ choices_number = int (re .findall (
467
+ r'poll(\d)choice_text_only' , self .name
468
+ )[0 ])
469
+ choices = []
470
+
471
+ for i in range (1 , choices_number + 1 ):
472
+ choice_label = binding_values [f'choice{ i } _label' ]
473
+ choice_count = binding_values [f'choice{ i } _count' ]
474
+ choices .append ({
475
+ 'number' : str (i ),
476
+ 'label' : choice_label ['string_value' ],
477
+ 'count' : choice_count .get ('string_value' , '0' )
478
+ })
479
+
480
+ self .choices = choices
481
+
482
+ duration_minutes = binding_values ['duration_minutes' ]['string_value' ]
483
+ self .duration_minutes = int (duration_minutes )
484
+
485
+ end = binding_values ['end_datetime_utc' ]['string_value' ]
486
+ updated = binding_values ['last_updated_datetime_utc' ]['string_value' ]
487
+ self .end_datetime_utc : str = end
488
+ self .last_updated_datetime_utc : str = updated
489
+
490
+ counts_are_final = binding_values ['counts_are_final' ]['boolean_value' ]
491
+ self .counts_are_final : bool = counts_are_final
492
+
493
+ if 'selected_choice' in binding_values :
494
+ selected_choice = binding_values ['selected_choice' ]['string_value' ]
495
+ self .selected_choice : str = selected_choice
496
+ else :
497
+ self .selected_choice = None
498
+
499
+ async def vote (self , selected_choice : str ) -> Poll :
500
+ """
501
+ Vote on the poll with the specified selected choice.
502
+ Parameters
503
+ ----------
504
+ selected_choice : str
505
+ The label of the selected choice for the vote.
506
+ Returns
507
+ -------
508
+ Poll
509
+ The Poll object representing the updated poll after voting.
510
+ """
511
+ return await self ._client .vote (
512
+ selected_choice ,
513
+ self .id ,
514
+ self .tweet .id ,
515
+ self .name
516
+ )
517
+
518
+ def __repr__ (self ) -> str :
519
+ return f'<Poll id="{ self .id } ">'
520
+
521
+ def __eq__ (self , __value : object ) -> bool :
522
+ return isinstance (__value , Poll ) and self .id == __value .id
523
+
524
+ def __ne__ (self , __value : object ) -> bool :
525
+ return not self == __value
0 commit comments