Created
June 6, 2018 20:44
-
-
Save oneroyalace/c4a6bdf9df930378760a07be997b99be to your computer and use it in GitHub Desktop.
Sentiment of Kanye's last tweet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy | |
from dotenv import load_dotenv | |
from nltk.tokenize import sent_tokenize | |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer | |
import os | |
from os.path import join | |
load_dotenv(join(os.getcwd(), '.env')) | |
CONSUMER_TOKEN = os.environ['CONSUMER_TOKEN'] | |
CONSUMER_SECRET = os.environ['CONSUMER_SECRET'] | |
ACCESS_TOKEN_KEY = os.environ['ACCESS_TOKEN_KEY'] | |
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET'] | |
print(CONSUMER_TOKEN) | |
print(CONSUMER_SECRET) | |
print(ACCESS_TOKEN_KEY) | |
print(ACCESS_TOKEN_SECRET) | |
def scrape_twitter(): | |
""" Returns JSON of Kanye's last text tweet""" | |
auth = tweepy.OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET) | |
api = tweepy.API(auth) | |
new_ye_tweets = api.user_timeline(screen_name='kanyewest', tweet_mode= "extended", count=25) | |
for tw in new_ye_tweets: | |
if len(tw._json['full_text'].split()) > 1: | |
return tw._json | |
def sent_analyze(tweet): | |
""" Returns sentiment score of tweet [-1,1]""" | |
analyzer = SentimentIntensityAnalyzer() | |
sent_score = analyzer.polarity_scores(tweet['full_text'])['compound'] | |
return sent_score | |
def process_tweet(): | |
""" return object for publishing""" | |
new_ye_tweet = scrape_twitter() | |
score = sent_analyze(new_ye_tweet) | |
return { | |
'tweet_body': new_ye_tweet['full_text'], | |
'id': new_ye_tweet['id'], | |
'sent_score': score | |
} | |
def main (): | |
print(process_tweet()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment