-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
497 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import matplotlib.pyplot as plt | ||
import matplotlib.animation as animation | ||
from matplotlib import style | ||
import time | ||
|
||
style.use("ggplot") | ||
|
||
fig = plt.figure() | ||
ax1 = fig.add_subplot(1, 1, 1) | ||
|
||
|
||
def animate(i): | ||
pullData = open("twitter-out.txt", "r").read() ## data-file name , put name where datafile is stored | ||
lines = pullData.split('\n') | ||
|
||
xar = [] | ||
yar = [] | ||
|
||
x = 0 | ||
y = 0 | ||
|
||
for l in lines[-200:]: ## this saves the last 200 lines of data to put on for graphing. | ||
x += 1 | ||
if "pos" in l: | ||
y += 1 | ||
elif "neg" in l: | ||
y -= 1 ## to remove the negative graphing you can put 0.3 instead 1 | ||
|
||
xar.append(x) | ||
yar.append(y) | ||
|
||
ax1.clear() | ||
ax1.plot(xar, yar) | ||
|
||
|
||
ani = animation.FuncAnimation(fig, animate, interval=1000) | ||
plt.show() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from tweepy import Stream | ||
from tweepy import OAuthHandler | ||
from tweepy.streaming import StreamListener | ||
import json | ||
import sentiment_mod as s | ||
|
||
# consumer key, consumer secret, access token, access secret. | ||
ckey = "asdfsafsafsaf" | ||
csecret = "asdfasdfsadfsa" | ||
atoken = "asdfsadfsafsaf-asdfsaf" | ||
asecret = "asdfsadfsadfsadfsadfsad" | ||
|
||
|
||
class listener(StreamListener): | ||
def on_data(self, data): | ||
try: ## handling reconnecting issue | ||
all_data = json.loads(data) | ||
|
||
tweet = all_data["text"] | ||
sentiment_value, confidence = s.sentiment(tweet) | ||
|
||
print (tweet, sentiment_value, confidence) | ||
time.sleep(0.2) ## Error handling in case of restricted tweets | ||
|
||
if confidence*100>=80: ##Scaling can be reconfigured. | ||
output = open("twitter-out.txt","a") | ||
output.write(sentiment_value) | ||
output.write('\n') | ||
output.close() | ||
|
||
return True | ||
except: | ||
return True ## if this returns false at run time try not reconnecting at same time | ||
|
||
def on_error(self, status): | ||
print (status) | ||
|
||
|
||
auth = OAuthHandler(ckey, csecret) | ||
auth.set_access_token(atoken, asecret) | ||
|
||
twitterStream = Stream(auth, listener()) | ||
twitterStream.filter(track=["car"]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from tweepy import Stream | ||
from tweepy import OAuthHandler | ||
from tweepy.streaming import StreamListener | ||
import MySQLdb | ||
import time | ||
import json | ||
|
||
# replace mysql.server with "localhost" if you are running via your own server! | ||
# server MySQL username MySQL pass Database name. | ||
conn = MySQLdb.connect("mysql.server", "beginneraccount", "cookies", "beginneraccount$tutorial") | ||
|
||
c = conn.cursor() | ||
|
||
# consumer key, consumer secret, access token, access secret. | ||
# get the following access tokens and keys from here https://apps.twitter.com/ | ||
ckey = "asdfsafsafsaf" | ||
csecret = "asdfasdfsadfsa" | ||
atoken = "asdfsadfsafsaf-asdfsaf" | ||
asecret = "asdfsadfsadfsadfsadfsad" | ||
|
||
|
||
class listener(StreamListener): | ||
def on_data(self, data): | ||
all_data = json.loads(data) | ||
|
||
tweet = all_data["text"] | ||
|
||
username = all_data["user"]["screen_name"] | ||
|
||
c.execute("INSERT INTO taula (time, username, tweet) VALUES (%s,%s,%s)", | ||
(time.time(), username, tweet)) | ||
|
||
conn.commit() | ||
|
||
print((username, tweet)) | ||
|
||
return True | ||
|
||
def on_error(self, status): | ||
print (status) | ||
|
||
|
||
auth = OAuthHandler(ckey, csecret) | ||
auth.set_access_token(atoken, asecret) | ||
|
||
twitterStream = Stream(auth, listener()) | ||
twitterStream.filter(track=["value"]) ##replace value with the desired query to stream. |