Interactive Function to Gather Live Tweets About Road Conditions and Traffic Status

A project to route first respondents during emergencies and natural disasters.

Haya Toumy
3 min readMay 14, 2019

Motivation

You’d say, why do we need this, we have Google, and Waze? That’s generally true, but in case of natural disasters, think floods or wildfires, your neighbor knows if a road is still under high water, or the backroads to evict from the city, and he/she will post in on Twitter in live time, much faster than Google or Waze can catch up.

Idea

Make a function that takes the street name as an input from user, and it immediately returns the most recent 20 tweets about that street conditions.
This function is useable for several cities, the user needs to define the city and street in one line.

The Manual Part

That was in searching online for the official or the most reliable entities/people who posts about traffic and road conditions, and manually saving these names to a list. Then I made a list of lists of these for the most flooded 15 cities in the US, according to this link 15 American Cities With the Most Homes in Danger of Flooding

The Core Code

Get different important parts of each tweet:

def gather_tweets(handle:str, n=300):
"""
Returns the most recent tweets (up to 200), from the selected Twitter username (handle)

Parameters:
-----------
handle: name of the Twitter user page
n: how many tweets you want. can't get more than 200
retrieves only recent tweets
"""
tweets_everything = api.user_timeline(handle, count = n)
df = pd.DataFrame(columns = ['id', 'tweets', 'date', 'location'])

for i in tweets_everything:
tweets = i.text
try:
date = i.formatted_date
except:
date = i.created_at

try:
location = i.geo['coordinates']
except:
try:
location = i.coordinates
except:
location = 'NaN'

tweet_id = i.id # by the way, tweet_id is included in the permalink

df.loc[len(df)] = [tweet_id, tweets, date, location] # inside the loop, building the df row by row

return df
#------------------------------------------------------------------
def traffic(users = ["TotalTrafficHOU", "houstontranstar"]):
"""
This function takes an input a street name, and return the most live recent tweets from the traffic data frame.
Enter street name in all any case you like, upper, lower, mix..

Parameters:
-----------
users: a list of actual Twitter user names (found after the @ in their homepage on Twitter)

"""
print('WARNING: If no users were passed, it will search Houston, TX traffic by default')

if users == []:
users = ["TotalTrafficHOU", "houstontranstar"]
print("Empty users list passed; changed to default: Houston, TX traffic")

s = input('What street you want to find? ')

# getting the df, in case there's more than 2 users:
lst = []
for u in users:
lst.append(gather_tweets(u))

df = pd.concat(lst,
axis = 0, sort = False)

mask = df['tweets'].str.contains(s, case = False) # case = False makes it not case sensitive!
return df.loc[mask, 'tweets'].values[:15]
# the tweets are ordered most recent first by nature; the first 15 tweets are the most recent.
# since the results are not perfect, one will have information about other streets too

Where get_twitter_user_names(city_name, df_handles_cities) is a function returns the Twitter user names that you can plug later in the function traffic(), which in its turn, gets the twitter updates.
Arguments: city_name and the data frame where the Twitter usernames who post traffic updates, of each city are saved.

Usage

All the user have to do is type in:

traffic(get_twitter_user_names('plano, texas'))

Because get_twitter_user_names('plano, texas') returns the Twitter handles (usernames) who tweet about traffic updates.

I set the default to Houston, TX. But this can be fixed by the user (first respondent’s system) later on, by doing the following:

updates = traffic(get_twitter_user_names('rogers city, arkansas'))# then, anytime you want to use it:
traffic(updates)

Now enter the street name when you’re promoted to, all lower case for ease of use. That’s it!

Resources:

I used tweepy library to collect tweets.

A fantastic library for getting old tweets, or more than the 200 Twitter API allows you: GetOldTweets3
It was developed by Jefferson Henrique then Dmitry Mottl, who maintains it.

The repo page

My continually under construction portfolio
My LinkedIn profile, I’d love to keep in touch!

--

--