Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
All files
  • Loading branch information
GeekyShiva committed Feb 13, 2017
1 parent 8ea7be0 commit b059969
Show file tree
Hide file tree
Showing 7 changed files with 497 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .idea/Twitter_sentiment_Analysis.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

347 changes: 347 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions graphing.py
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()
43 changes: 43 additions & 0 deletions sentimen.py
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"])
47 changes: 47 additions & 0 deletions twitter_streaming.py
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.

0 comments on commit b059969

Please sign in to comment.