-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweetscrape.py
53 lines (43 loc) · 1.98 KB
/
tweetscrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from sentiment import TSentiment
import datetime
from datetime import timedelta
topic = "Kendrick Lamar"
s = TSentiment(topic)
numOfTweets = 200
tweetMap = {}
daysAgo = 10
# Count from 10 days ago to include every single day time interval
while daysAgo >= 0:
since = (datetime.datetime.now() - timedelta(days=daysAgo)).strftime("%Y-%m-%d")
until = (datetime.datetime.now() - timedelta(days=daysAgo-1)).strftime("%Y-%m-%d")
tweets = s.get_tweets(numOfTweets, since, until)
# Tally up positive, negative, and neutral sentiment among the tweets per day
tweetsObtained = len(tweets)
positiveTweets = 0
negativeTweets = 0
neutralTweets = 0
for tweet in tweets:
if tweet[2] == 'positive':
positiveTweets += 1
elif tweet[2] == 'negative':
negativeTweets += 1
else:
neutralTweets += 1
# Calculate the percentage distribution
# positivePercentage = round(positiveTweets/float(tweetsObtained), 2)
# negativePercentage = round(negativeTweets/float(tweetsObtained), 2)
# neutralPercentage = round(neutralTweets/float(tweetsObtained), 2)
# print "Volume for " + since + ": " + str(tweetsObtained)
# Store each day's unique values in our tweet map
tweetMap[since] = str(positiveTweets) + ', ' + str(neutralTweets) + ', ' + str(negativeTweets) + ', ' + '0'
# for tweet in tweets:
# print tweet[0].encode('ascii', 'replace') + '\n' + tweet[1] + '\n' + tweet[2]
# print '----------------------------------------------------------------------'
daysAgo -= 1
write_csvfile = "./templates/sentiment_data.csv"
csv = open(write_csvfile, "w")
csv.write('Sentiment,Positive,Neutral,Negative,' + topic + '\n')
for timeStamp in tweetMap:
csv.write(timeStamp + ', ' + tweetMap[timeStamp] + '\n')
# for time in tweetMap:
# print time + " " + str(tweetMap[time][0]) + " percent positive, " + str(tweetMap[time][1]) + " percent negative, " + str(tweetMap[time][2]) + " percent neutral"