Last active
April 25, 2018 17:39
-
-
Save davidlenz/4a720adab63727002148a6986a523ca9 to your computer and use it in GitHub Desktop.
Scrape Data from Twitter and extract Sentiment using VaderSentiment. Code is from https://www.pythonprogramming.net/
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
from tweepy import Stream | |
from tweepy import OAuthHandler | |
from tweepy.streaming import StreamListener | |
import json | |
import sqlite3 | |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer | |
from unidecode import unidecode | |
import time | |
analyzer = SentimentIntensityAnalyzer() | |
#consumer key, consumer secret, access token, access secret. | |
ckey= | |
csecret= | |
atoken= | |
asecret= | |
conn = sqlite3.connect('twitter.db') | |
c = conn.cursor() | |
def create_table(): | |
try: | |
c.execute("CREATE TABLE IF NOT EXISTS sentiment(unix REAL, tweet TEXT, sentiment REAL)") | |
c.execute("CREATE INDEX fast_unix ON sentiment(unix)") | |
c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)") | |
c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)") | |
conn.commit() | |
except Exception as e: | |
print(str(e)) | |
create_table() | |
class listener(StreamListener): | |
def on_data(self, data): | |
try: | |
data = json.loads(data) | |
tweet = unidecode(data['text']) | |
time_ms = data['timestamp_ms'] | |
vs = analyzer.polarity_scores(tweet) | |
sentiment = vs['compound'] | |
print(time_ms, tweet, sentiment) | |
c.execute("INSERT INTO sentiment (unix, tweet, sentiment) VALUES (?, ?, ?)", | |
(time_ms, tweet, sentiment)) | |
conn.commit() | |
except KeyError as e: | |
print(str(e)) | |
return(True) | |
def on_error(self, status): | |
print(status) | |
while True: | |
try: | |
auth = OAuthHandler(ckey, csecret) | |
auth.set_access_token(atoken, asecret) | |
twitterStream = Stream(auth, listener()) | |
twitterStream.filter(track=["a","e","i","o","u"]) | |
except Exception as e: | |
print(str(e)) | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment