-
Notifications
You must be signed in to change notification settings - Fork 3
Method: Utility functions
We also include a few utility methods which will (hopefully) make working with the new Twitter API structure a bit easier.
First, you can import the utility methods into your environment with the following code...
import osometweet.utils as o_utils
... which will allow you to work with the o_utils
object to access the methods.
Managing time is an important aspect of gathering data from Twitter and often you'd just like to wait some specified time. This is relatively easy with the time
module, however, it is even easier with the pause_until()
method. Simply input the time that you would like to pause your code until, and the method handles the rest. This method can take in a datetime
object or a Unix epoch time-stamp. For example, if you'd like to wait ten seconds, you can simple do...
import osometweet.utils as o_utils
import datetime as datetime
# The below line of code takes the time at the current moment, converts it to an epoch time-stamp
# and then adds ten seconds to it.
now_plus_10_with_epoch_timestamp = datetime.datetime.now().timestamp() + 10
# Then we input that into the pause_until() method and your machine will
# sleep until that specific time, ten seconds later
o_utils.pause_until(now_plus_10_with_epoch_timestamp)
If you'd like to do this with a datetime
object, it looks like this...
import osometweet.utils as o_utils
import datetime as datetime
# The timedelta method takes input in the following way...
# timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
now_plus_10_with_datetime_object = datetime.datetime.now() + datetime.timedelta(seconds=10)
o_utils.pause_until(now_plus_10_with_datetime_object)
Another reality of working with Twitter data is that you are only allowed to query Twitter with a maximum number of users/tweets/whatever per endpoint. To deal with this, we created the o_utils.chunker
method which turns a list into a list of smaller lists where the length of those smaller lists are no longer than the user indicated size. For example...
from osometweet import utils as o_util
my_list = ["user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9"]
chunked_list = o_util.chunker(seq=my_list, size=2)
print(chunked_list)
which returns...
[['user1', 'user2'], ['user3', 'user4'], ['user5', 'user6'], ['user7', 'user8'], ['user9']]