<jupyter_start><jupyter_text>1. Let's open a connection to Twitter we use twarc library<jupyter_code>## Agranda academic token
BearerToken='<KEY> <PASSWORD>4gl<PASSWORD>Ceb<PASSWORD>EH<PASSWORD>Xe<PASSWORD>'
# This is where you initialize the client with your own bearer token (replace the XXXXX with your own bearer token)
client = Twarc2(bearer_token=BearerToken)<jupyter_output><empty_output><jupyter_text>2. Getting a user's Tweet timeline In the previous lab we observe that @manuelmunizv was a top influencer. Let's investigate a bit further who he is.  Alternative 1: Fetch all the tweets he has posted in the last 7 days<jupyter_code># This is where we specify our query 
query = "from:manuelmunizv "
search_results = client.search_recent(query=query, max_results=100)
ListofTweets=[]


limit = 50
counter=0
for page in search_results:
    
        
            
        # The Twitter API v2 returns the Tweet information and the user, media etc.  separately
        # so we use expansions.flatten to get all the information in a single JSON
        
        result = expansions.flatten(page)
        for tweet in result:    
            ListofTweets.append(tweet)
            
        
        
        
        # we set a counter to limit the number of API calls
        counter+=1
        if counter == limit:
               break
            
            
            
ListofTweetsDataFrame=pd.json_normalize(ListofTweets)
ListofTweetsDataFrame[['created_at','author.name','text','public_metrics.retweet_count']]<jupyter_output><empty_output><jupyter_text>Alternative 2: Fetch all the tweets he has ever posted<jupyter_code>usertimeline = client.timeline(user="manuelmunizv",max_results=20)
ListofTweets=[]


limit = 2
counter=0

 # Twarc returns all Tweets for the criteria set above, so we page through the results
for page in usertimeline:
        
        
        # The Twitter API v2 returns the Tweet information and the user, media etc.  separately
        # so we use expansions.flatten to get all the information in a single JSON
        result = expansions.flatten(page)
        for tweet in result:
          
            
            ListofTweets.append(tweet)
            
       # we set a counter to limit the number of API calls
        counter=counter+1
        if counter == limit:
               break
UserTimeLineDataFrame=pd.json_normalize(ListofTweets)
UserTimeLineDataFrame.sample(1)<jupyter_output><empty_output><jupyter_text>2. Get followers for a user<jupyter_code>followers = client.followers(user="manuelmunizv",max_results=20)
ListofFollowers=[]


limit = 2
counter=0


 # Twarc returns all Tweets for the criteria set above, so we page through the results

for page in followers:
                
        
        
        # The Twitter API v2 returns the Tweet information and the user, media etc.  separately
        # so we use expansions.flatten to get all the information in a single JSON
        result = expansions.flatten(page)
        for user in result:
          
            
            ListofFollowers.append(user)
            
            
            
        # we set a counter to limit the number of API calls
        counter=counter+1
        if counter == limit:
               break

ListofFollowersDataFrame=pd.json_normalize(ListofFollowers)
ListofFollowersDataFrame.sample(3)<jupyter_output><empty_output><jupyter_text>3 . Get users that a user is following<jupyter_code>following = client.following(user="manuelmunizv",max_results=10)
ListofFollowees=[]



limit = 2
counter=0

 # Twarc returns all Tweets for the criteria set above, so we page through the results
for page in following:
        # The Twitter API v2 returns the Tweet information and the user, media etc.  separately
        # so we use expansions.flatten to get all the information in a single JSON
        result = expansions.flatten(page)
        for user in result:
          
            
            ListofFollowees.append(user)
            
            
            # we set a counter to limit the number of API calls
        counter=counter+1
        if counter == limit:
               break
ListofFolloweesDataFrame=pd.json_normalize(ListofFollowees)
ListofFolloweesDataFrame.sample(3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.062206572769953054, 'fraction_numerical': 0.00892018779342723, 'mean_word_length': 2.705217391304348, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '544726', 'license': 'Apache-2.0', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1198, 'n_words': 398, 'path': 'sessions/Session2-Lab2.ipynb', 'repo_name': 'thousandoaks/TopicsInDataAnalysis', 'size': '4306'}