# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# Dependencies
import tweepy
import json
import numpy as np
from config2 import consumer_key, consumer_secret, access_token, access_token_secret




# Twitter API Keys
consumer_key = consumer_key
consumer_secret = consumer_secret
access_token = access_token
access_token_secret = access_token_secret

# Setup Tweepy API Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())

target_term = '@facebook'

# +
# Lists to hold sentiments
# Variables for holding sentiments
compound_list = []
positive_list = []
negative_list = []
neutral_list = []

for x in range(1, 20):

    # Get all tweets from home feed
    public_tweets = api.user_timeline(target_term, page=x)

    # Loop through all tweets
    for tweet in public_tweets:

        # Run Vader Analysis on each tweet
        results = analyzer.polarity_scores(tweet["text"])
        compound = results["compound"]
        pos = results["pos"]
        neu = results["neu"]
        neg = results["neg"]

        # Add each value to the appropriate list
        compound_list.append(compound)
        positive_list.append(pos)
        negative_list.append(neg)
        neutral_list.append(neu)
# -

print(
sum(compound_list)/len(compound_list),
sum(positive_list)/len(positive_list),
sum(negative_list)/len(negative_list),
sum(neutral_list)/len(neutral_list))

sentiments = {
    'Average Compounded':  sum(compound_list) / len(compound_list),
    'Average Negative': sum(negative_list) / len(negative_list),
    'Average Positive': sum(positive_list) / len(positive_list),
    'Average Neutral': sum(neutral_list) / len(neutral_list)
}


sentiments

len(compound_list)

# ## mentions of facebook

# +
# Search for all tweets
# public_tweets = api.search(target_term, count=300, result_type="recent")

# +
# Twitter API Keys
consumer_key = consumer_key
consumer_secret = consumer_secret
access_token = access_token
access_token_secret = access_token_secret

# Setup Tweepy API Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())


# -

print(target_term)

# +
# Loop through all public_tweets


fb_tweets = []

date = []
oldest_tweet = None
for x in range(1,20):
    public_tweets = api.search(target_term, count=100, result_type="recent", max_id=oldest_tweet)

    for tweet in public_tweets['statuses']:
        tweet_id = tweet["id"]
        tweet_author = tweet["user"]["screen_name"]
        tweet_text = tweet["text"]

        fb_tweets.append(tweet['text'])
        date.append(tweet['created_at'])
        
        oldest_tweet = tweet_id - 1

print(len(fb_tweets))

# +
compound_list = []
positive_list = []
negative_list = []
neutral_list = []

for tweet in fb_tweets:

    # Run Vader Analysis on each tweet
    results = analyzer.polarity_scores(tweet)
    compound = results["compound"]
    pos = results["pos"]
    neu = results["neu"]
    neg = results["neg"]

    # Add each value to the appropriate list
    compound_list.append(compound)
    positive_list.append(pos)
    negative_list.append(neg)
    neutral_list.append(neu)

# -

sentiments = {
    'Average Compounded':  sum(compound_list) / len(compound_list),
    'Average Negative': sum(negative_list) / len(negative_list),
    'Average Positive': sum(positive_list) / len(positive_list),
    'Average Neutral': sum(neutral_list) / len(neutral_list)
}


sentiments

len(fb_tweets)

date[1891]

june_14 = {
    'Text': fb_tweets,
    'Compounded': compound_list,
    'Negative': negative_list,
    'Positive': positive_list,
    'Neutral': neutral_list,
    'Date': date
}

import pandas as pd

tweets_june_14_df = pd.DataFrame(june_14)

tweets_june_14_df.head()

tweets_june_14_df.to_csv('tweets_june_14.csv')

print(date[0])
print(date[1891])



StarcoderdataJupyterScriptsDedupFiltered