<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Graphing the number of favorites to @infinite_scream over time
By R. Stuart Geiger (@staeiou), Released CC-BY 4.0 & MIT License
Setup
Installing dependencies
Step1:  Importing libraries
Step2:  Authenticate with Twitter
Step3:  Getting the tweets
This is limited to the last ~3200 tweets from the API. 
Step4:  Data processing
First, go through all the tweets and pull out a few key variables, putting it into a two-dimensional array.
Step5:  Convert to a pandas DataFrame for easy processing
Step6:  Peek into this dataframe, pulling out rows 1000 to 1005
Step7:  Filter out tweets that were replies to specific users.
Step8:  Time is in UTC / GMT-0 from the API, convert to US/Pacific
Step9:  Visualization
Step10:  Output to file
<ASSISTANT_TASK:>
Python Code:
!pip install tweepy pandas seaborn 
Explanation: Graphing the number of favorites to @infinite_scream over time
By R. Stuart Geiger (@staeiou), Released CC-BY 4.0 & MIT License
Setup
Installing dependencies
End of explanation
import random
import twitter_login  # a file containing my API keys
import tweepy
import pandas as pd
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import pytz
Explanation: Importing libraries
End of explanation
CONSUMER_KEY = twitter_login.CONSUMER_KEY
CONSUMER_SECRET = twitter_login.CONSUMER_SECRET
ACCESS_TOKEN = twitter_login.ACCESS_TOKEN
ACCESS_TOKEN_SECRET = twitter_login.ACCESS_TOKEN_SECRET
# Authenticate
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
Explanation: Authenticate with Twitter
End of explanation
# by yanofsky, https://gist.github.com/yanofsky/5436496
screen_name="infinite_scream"
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
    print("getting tweets before %s" % (oldest))
    #all subsiquent requests use the max_id param to prevent duplicates
    new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
    #save most recent tweets
    alltweets.extend(new_tweets)
    #update the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1
    print("...%s tweets downloaded so far" % (len(alltweets)))
Explanation: Getting the tweets
This is limited to the last ~3200 tweets from the API. :(
End of explanation
tweets = [[tweet.id_str, tweet.created_at, tweet.favorite_count, tweet.retweet_count,\
           tweet.favorite_count + tweet.retweet_count, tweet.in_reply_to_screen_name is not None,\
           tweet.text, len(tweet.text)] for tweet in alltweets]
Explanation: Data processing
First, go through all the tweets and pull out a few key variables, putting it into a two-dimensional array.
End of explanation
tweets_df = pd.DataFrame.from_dict(tweets)
tweets_df.columns = ["id_str", "created_at", "favorite_count", "retweet_count", "fav_rt_count", \
                     "is_reply", "tweet_text", "tweet_length"]
Explanation: Convert to a pandas DataFrame for easy processing
End of explanation
tweets_df[1000:1005]
Explanation: Peek into this dataframe, pulling out rows 1000 to 1005
End of explanation
tweets_noreplies_df = tweets_df[tweets_df['is_reply'] == False]
Explanation: Filter out tweets that were replies to specific users.
End of explanation
fav_count = tweets_noreplies_df.set_index("created_at")["fav_rt_count"]
fav_count = fav_count.tz_localize(pytz.utc).tz_convert(pytz.timezone('US/Pacific'))
fav_count[0:10]
Explanation: Time is in UTC / GMT-0 from the API, convert to US/Pacific
End of explanation
ax = fav_count.plot(figsize=[14,8], fontsize=16)
ax.set_title("Engagement to @infinite_scream's tweets over time", {"fontsize":20})
ax.set_ylabel("Number of favorites + retweets", {"fontsize":20})
ax.set_xlabel("Time (Pacific Standard Time / GMT-8)", {"fontsize":20})
xlim_start, xlim_end = plt.xlim()
xlim_end = xlim_end + .25
plt.xlim(xlim_start, xlim_end)
Explanation: Visualization
End of explanation
tweets_df.index.name = "tweet_num"
tweets_df.to_csv("infinite_scream.csv")
tweets_df.to_json("infinite_scream.json")
!head infinite_scream.csv
Explanation: Output to file
End of explanation
<END_TASK>