Example usage
Installation
To use tweepypoll in a project -
Begin by installing and importing tweepypoll, and optionally checking the version
import tweepypoll
print(tweepypoll.__version__)
0.2.0
Imports
Next, import the functions:
from tweepypoll.tweepypoll import get_poll_by_id
from tweepypoll.tweepypoll import get_polls_from_user
from tweepypoll.tweepypoll import visualize_poll
Main functions
Now you are ready to use the main functionality!
You may start by fetching tweet IDs for tweets with polls from a certain user, like so:
1. get_polls_from_user(username, tweet_num=5)
This function returns a list of tweet IDs containing polls for a provided twitter username.
get_polls_from_user('PollzOnTwitta', 7)
[1239677495487737856,
1239677278193438722,
1239676949238292488,
1233970022109892608,
1228442768902688769]
As you can probably see, the first argument is the Twitter username, and the second argument is the number of polls you would like to fetch. The second argument is optional and defaults to 5.
2. get_poll_by_id(id)
This function takes in the tweet id (for instance, fetched by get_polls_from_user() function documented above) and returns a dictionary with the following fields:
(1) text of the tweet
(2) poll_options
(3) total_responses
(4) poll duration
(5) end date of the poll
(6) author’s username
get_poll_by_id(1239677278193438722)
{'text': 'do you find it weird when parents make a social media account for babies/young children? #poll',
'duration': 10080,
'date': datetime.datetime(2020, 3, 23, 22, 17, 49, tzinfo=datetime.timezone.utc),
'poll options': [{'position': 1, 'label': 'YES', 'votes': 135},
{'position': 2, 'label': 'No, idc.', 'votes': 42}],
'user': 'PollzOnTwitta',
'total': 177}
3. visualize_poll(poll_obj, show_user=False, show_duration=False, show_date=False)
This function takes the poll object generated by get_poll_by_id() and returns a simple bar chart of poll responses. show_user, show_duration and show_date are optional arguments for displaying that information.
poll = get_poll_by_id(1239677278193438722)
visualize_poll(poll, show_duration=True)
The duration of the poll in hours: 168.0h
Enjoy and have fun!
Thank you for checking out our little twitter poll package!!