/
ServerHandler.py
86 lines (68 loc) · 2.96 KB
/
ServerHandler.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from flask import Flask, Response, json
from TwitterAPI import TwitterAPI
from alchemy import AlchemyAPI
from secretKey import getKeys
import sys, traceback
app = Flask(__name__)
@app.route("/<bias>/<path:url>")
def twitterScraper(bias, url):
try:
##Load keys from other file and initialize the APIS
consumer_key, consumer_secret, access_token_key, access_token_secret = getKeys()
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
alchemyAPI = AlchemyAPI()
##Get the list of keywords for the page
response = alchemyAPI.keywords('url', url, {'sentiment': 1})
##Put relevant keywords in a list
keys = []
for x in response['keywords']:
if float(x['relevance']) > 0.6:
keys.append(x['text'])
if len(keys) >= 5:
break
alreadyAdded = set()
htmlString = "<body>\n"
##Get some tweets for each of the relevant keywords
for key in keys:
key += ' ' + bias;
tweetList = api.request('search/tweets', {'q':str(key), 'lang':'en'})
y = [] ## redundant tweets
for x in tweetList:
## redundant tweets
if x['text'][:20] not in y:
y.append(x['text'][:20])
else:
continue
##Skip non english
if x['lang'] != 'en':
continue
if x['text'][:2] == "RT":
continue
htmlString += "<blockquote class=\"twitter-tweet\" lang=\"en\"><p>"
htmlString += x['text']
htmlString += "</p>—"
htmlString += x['user']['name']
htmlString += " (@"
htmlString += x['user']['screen_name']
htmlString += ") <a href=\"https://twitter.com/"
htmlString += x['user']['screen_name']
htmlString += "/status/"
htmlString += str(x['id'])
htmlString += "\">"
htmlString += "</a></blockquote> <script async src=\"http://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>\n"
htmlString += "</body>"
return htmlString
except Exception as e:
return "There was some kind of error, Go home"
def convertToDate(rawDate):
year = rawDate[-4:]
rawDate = rawDate[4:]
monthDict = {'Jan' : 'January', 'Feb' : 'February', 'Mar' : 'March', 'Apr' : 'April',
'May' : 'May', 'Jun' : 'June', 'Jul' : 'July', 'Aug' : 'August',
'Sep' : 'September', 'Oct' : 'October', 'Nov' : 'November', 'Dec' : 'December'}
month = monthDict[rawDate[:3]]
rawDate = rawDate[4:]
day = str(int(rawDate[:2]))
finalForm = month + " " + day + ", " + year
if __name__ == "__main__":
app.run(host='localhost', port=5001, debug=None)