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.3.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)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [4], in <module>
----> 1 get_poll_by_id(1239677278193438722)
File ~/checkouts/readthedocs.org/user_builds/tweepypoll/envs/latest/lib/python3.9/site-packages/tweepypoll/tweepypoll.py:100, in get_poll_by_id(tweet_ids)
98 # Check argument validity
99 if not (isinstance(tweet_ids, list)):
--> 100 raise TypeError(
101 "Invalid argument type: input tweet_id must be a list of numeric IDs."
102 )
104 # Twitter API credentials
105 # from dotenv import load_dotenv, find_dotenv
106 # load_dotenv(find_dotenv())
(...)
111 # In practice, we would use commented out code to get the token from environmental variable
112 ############################################################################
114 bearer_token = "AAAAAAAAAAAAAAAAAAAAAAyIYQEAAAAAjvBdCMMh1dT8clkpXhHxzld7Dhs%3DLPl5zMXXOZqznZGe9JP7zHj3Wzx0N4unogLcWl8wfIkwikjQKm"
TypeError: Invalid argument type: input tweet_id must be a list of numeric IDs.
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!!