-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSENT.py
More file actions
70 lines (53 loc) · 2.05 KB
/
SENT.py
File metadata and controls
70 lines (53 loc) · 2.05 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 12 10:14:12 2016
@author: Satya
"""
from tweepy import OAuthHandler
import tweepy
import urllib.request
import json
from unidecode import unidecode
CKEY = "QdjZPGYPwd99r72qQfyZyZEcO"
CSECRET = "yUgsvYUSFtNQIEgLW1aY9DMJRRxYajxfYC2pg3RzFhR3rkcl5L"
ATOKEN = "174336590-kvtw1cqrwuH75LIMKqXZkKSeoU9BfEAB9QBnMIoI"
ATOKENSECRET = "KdnYcVNI0h6ny7i9ACzNw3I0h0hLSlqdjXTSDoYpgDXc5"
URL_SENTIMENT140 = "http://www.sentiment140.com/api/bulkClassifyJson"
d = dict(parameter1="value1", parameter2="value2")
COMPANYNAME = "AAPL"
LIMIT = 2500
LANGUAGE = 'es' # Sentiment140 API only support English or Spanish.
def parse_response(json_response):
negative_tweets, positive_tweets = 0, 0
for j in json_response["data"]:
if int(j["polarity"]) == 0:
negative_tweets += 1
elif int(j["polarity"]) == 4:
positive_tweets += 1
return negative_tweets, positive_tweets
def main():
auth = OAuthHandler(CKEY, CSECRET)
auth.set_access_token(ATOKEN, ATOKENSECRET)
api = tweepy.API(auth)
tweets = []
for tweet in tweepy.Cursor(api.search,
q=COMPANYNAME,
result_type='recent',
include_entities=True,
lang=LANGUAGE).items(LIMIT):
aux = {"text": unidecode(tweet.text.replace('"', '')), "language": LANGUAGE, "query": COMPANYNAME,
"id": tweet.id}
tweets.append(aux)
result = {"data": tweets}
data = urllib.parse.urlencode(d).encode("utf-8")
req = urllib.request.Request(URL_SENTIMENT140)
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req, str(result))
json_response = json.loads(response.read())
negative_tweets, positive_tweets = parse_response(json_response)
print
"Positive Tweets: " + str(positive_tweets)
print
"Negative Tweets: " + str(negative_tweets)
if __name__ == '__main__':
main()