<jupyter_start><jupyter_text>Streaming Data on Twitter Find the currently active and popular singer on Twitter.<jupyter_code># Import the necessary methods from tweepy library
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream<jupyter_output><empty_output><jupyter_text>Authorization Code to access Twitter API<jupyter_code>access_token = 
access_token_secret = 
consumer_key = 
consumer_secret = 
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)<jupyter_output><empty_output><jupyter_text>Stream Tweets<jupyter_code>import json
import pandas as pd

class MyStreamListener(tweepy.StreamListener):
    
    def __init__(self, api=None):
        # inherit class attributes
        super(MyStreamListener, self).__init__()
        self.num_tweets = 0
        self.file = open("tweets.txt", "w+")

    def on_status(self, status):
        tweet = status._json
        
        self.file.write( json.dumps(tweet) + '\n' )
        
        self.num_tweets += 1
        if self.num_tweets < 500000:
            return True
        else:
            return False
        self.file.close()

    def on_error(self, status):
        print(status)

# Initialize the Stream Listen
l = MyStreamListener()

# Authorization 
stream = tweepy.Stream(auth, l)

# Capture streaming data using my favoriate singers:
stream.filter(track=['<NAME>', '<NAME>', '<NAME>', '<NAME>', 'Shakira', '<NAME>','<NAME>','Beyonce', 'Adele', 'Rihanna', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>'], async=True)<jupyter_output><empty_output><jupyter_text>Load and explore my Twitter data<jupyter_code># Import package
import json

# String of path to file: tweets_data_path
tweets_data_path = 'tweets.txt'

# Initialize empty list to store tweets: tweets_data
tweets_data = []

# Open connection to file
tweets_file = open(tweets_data_path, "r")

# Read in tweets and store in list: tweets_data
for line in tweets_file:
    tweet = json.loads(line)
    tweets_data.append(tweet)

import pandas as pd
pd.DataFrame(tweets_data).head(1)

# Build DataFrame of tweet texts and languages
df = pd.DataFrame(tweets_data, columns=['text', 'lang'])
print(df.shape)

# Print the first few from DataFrame
print(df.head())<jupyter_output>(2404, 2)
                                                text lang
0  RT @RapSpotlightss: Rihanna 😍 https://t.co/UNk...   is
1  RT @neoliberals: hello kevin spacey, "i was dr...   en
2  RT @fentyy: Rihanna as <NAME>ebbles 😭😭 http...   en
3  <NAME> Anda tidak akan berhasil menjadi ...   in
4  hits pop rock #disco listen 'n enjoy Demi Lova...   en<jupyter_text>Visualize the Tweets<jupyter_code>import re

def word_in_text(word, tweet):
    word = word.lower()
    text = tweet.lower()
    match = re.search(word, tweet)

    if match:
        return True
    return False

# Initialize list to store tweet counts
[Bieber, Spears, Grande, Gomez, Shakira, Lopez, Puth, Beyonce, Adele, Rihanna, Gaga, Perry, Mendes, Lovato, Swift, Larsson] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# Iterate through df, counting the number of tweets in which
# each candidate is mentioned
for index, row in df.iterrows():
    Bieber += word_in_text('<NAME>', row['text'].lower())
    Spears += word_in_text('<NAME>', row['text'].lower())
    Grande += word_in_text('<NAME>', row['text'].lower())
    Gomez += word_in_text('<NAME>', row['text'].lower())
    Shakira += word_in_text('Shakira', row['text'].lower())
    Lopez += word_in_text('<NAME>', row['text'].lower())
    Puth += word_in_text('<NAME>', row['text'].lower())
    Beyonce += word_in_text('Beyonce', row['text'].lower())
    Adele += word_in_text('Adele', row['text'].lower())
    Rihanna += word_in_text('Rihanna', row['text'].lower())
    Gaga += word_in_text('<NAME>', row['text'].lower())
    Perry += word_in_text('<NAME>', row['text'].lower())
    Mendes += word_in_text('<NAME>', row['text'].lower())
    Lovato += word_in_text('<NAME>', row['text'].lower())
    Swift+= word_in_text('<NAME>', row['text'].lower())
    Larsson += word_in_text('<NAME>', row['text'].lower())
print (Bieber, Spears, Grande, Gomez, Shakira, Lopez, Puth, Beyonce, Adele, Rihanna, Gaga, Perry, Mendes, Lovato, Swift, Larsson)

# Import packages
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline
# Set seaborn style
sns.set(color_codes=True)

# Create a list of labels:cd
cd = ['Bieber', 'Spears', 'Grande', 'Gomez', 'Shakira', 'Lopez','Puth', 'Beyonce', 'Adele', 'Rihanna', 'Gaga', 'Perry', 'Mendes', 'Lovato', 'Swift', 'Larsson']

# Plot histogram
plt.figure(figsize=(20, 20))
ax = sns.barplot(cd, [Bieber, Spears, Grande, Gomez, Shakira, Lopez, Puth, Beyonce, Adele, Rihanna, Gaga, Perry, Mendes, Lovato, Swift, Larsson],alpha=.6)
ax.set(ylabel="Count")
ax.set(xlabel="Lusi's Favorite Singers")
ax.set_title('Popularity Count on Twitter')
plt.show()<jupyter_output>242 57 102 377 13 11 15 70 44 709 112 26 39 121 104 61<jupyter_text>Above is the data that I streamed on Monday Oct 31, 2017. RiRi seems to be the most popular/active singer on Twitter on this day. She is followed by Gomez and then Bieber. My current favoriate singer is <NAME> because he is my age and we can totally relate.  Analyze My Twitter Account Analyze My Followers<jupyter_code>twitter_handle = 'lucieari'

auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

import csv 

f = csv.writer(open('myfollowers.csv', 'w'))

f.writerow(["screenName", "name", "location", "description"])

users = tweepy.Cursor(api.followers, screen_name=twitter_handle, count = 200).items()

for u in users:
    screenName = u.screen_name
    name = u.name
    location = u.location
    description = u.description
    f.writerow([screenName, name, location, description, ])
import pandas as pd
df = pd.read_csv('myfollowers.csv')
df.head(5)
len(df)<jupyter_output><empty_output><jupyter_text>The length of the dataset is 149, which means I have only the info of 149 of my followers. Since I have the database of my followers, I can tell who unfollowed me by comparing the new csv file with the old csv file.  Look for CEO and Founder Followers<jupyter_code>def word(text):
    if 'CEO' in str(text):
        return True
    else:
        return False
sum(df['description'].apply(lambda text: word(text)))
def word(text):
    if 'Founder' in str(text):
        return True
    else:
        return False
sum(df['description'].apply(lambda text: word(text)))<jupyter_output><empty_output><jupyter_text>Adding them up, I have 11 CEO & Founders followers on my Twitter account.  Look for users who put their location as "United States".<jupyter_code>def word(text):
    if 'United States' in str(text):
        return True
    else:
        return False
sum(df['location'].apply(lambda text: word(text)))<jupyter_output><empty_output><jupyter_text>Find the Twitter Trend  Find the current top hashtags on October 30, 2017 at around 6:35PM EST.<jupyter_code>trends = api.trends_place(1)
data = trends[0] 
# Call up the key "trends" of the dictionary
trends = data['trends']
# Extract the name of the hashtag
names = [trend['name'] for trend in trends]
# Extract the tweet_volume from trends
tweet_volume = [trend['tweet_volume'] for trend in trends]
# Extract url from trends
url = [trend['url'] for trend in trends]
# Create a dataframe from the above live data
data = [names, tweet_volume, url]
df = pd.DataFrame(data, index = ['Hashtag', 'Total Tweets', 'url'])
df = df.T.head(5)
df<jupyter_output><empty_output><jupyter_text>The above dataset changes all the time because the data is live from Twitter.  Average Number of Tweets of the top Hashtag of the moment<jupyter_code>df['Total Tweets'].mean()
%matplotlib inline
# Seaborn package
import seaborn as sns
df['Total Tweets'].mean()
newTT = df['Total Tweets'].fillna(df['Total Tweets'].mean())
sns.distplot(newTT)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1318108477100658, 'fraction_numerical': 0.013528608663274171, 'mean_word_length': 4.545767377838954, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 66, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '594352', 'license': 'MIT', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2724, 'n_words': 805, 'path': 'Twitter.ipynb', 'repo_name': 'yanglusi/DataAnalytics', 'size': '8216'}