-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
124 lines (105 loc) · 3.72 KB
/
app.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''
Tweet Cloud
Copyright (C) 2012 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os, urllib, simplejson
from flask import Flask, request, render_template, Response
from collections import defaultdict
STRINGS= [',', '.', '-', '"']
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('home.html')
@app.route('/get_tweets/<handle>')
def get_tweets(handle=""):
max_count = request.args.get('count', 1000, type=int)
replies = request.args.get('replies', False, type=int)
include_rts = request.args.get('include_rts', False, type=int)
base_url = "https://api.twitter.com/1/statuses/user_timeline.json"
param = {
'screen_name' : handle,
'exclude_replies' : not replies,
'include_rts' : include_rts,
'count': 200,
'trim_user': 'true'
}
print param
result = []
length = 0
while length < max_count:
if length != 0:
param['max_id'] = result[-1]['id'] - 1
print param['max_id']
url = base_url + '?' + urllib.urlencode(param)
temp = simplejson.load(urllib.urlopen(url))
if 'errors' in temp:
return None
if len(temp) == 0:
break
remaining = max_count - length
result.extend(temp[:remaining])
length = len(result)
print "Length---->",length
type_value = request.args.get('type', 'word_count', type=str)
data = values(type_value, result)
print dict(data)
json = simplejson.dumps({'data': data, 'count': length})
resp = Response(json, status=200, mimetype='application/json')
return resp
def values(option, data):
return {
'word_count': word_count,
'words': words,
'hashtags': hashtags
}.get(option, word_count)(data)
def word_count(data):
tweet_count = defaultdict(lambda:0)
for tweet in data:
text = tweet['text']
# TODO: Improve later
for ch in STRINGS:
text = text.replace(ch, ' ')
length = len(text.split())
tweet_count[length] += 1
return tweet_count
def words(data):
tweets = defaultdict(lambda:0)
for tweet in data:
# TODO: Improve later
text = tweet['text']
for ch in STRINGS:
text = text.replace(ch, ' ')
words = text.split()
for word in words:
if not word.startswith("#"):
tweets[word] += 1
# We dont need words with frequency < 3
tweets = {key: value for key, value in tweets.items() if value > 3}
return tweets
def hashtags(data):
tweets = defaultdict(lambda:0)
for tweet in data:
# TODO: Improve later
text = tweet['text']
for ch in STRINGS:
text = text.replace(ch, ' ')
words = text.split()
for word in words:
if word.startswith("#"):
tweets[word] += 1
# We dont need words with frequency 1
tweets = {key: value for key, value in tweets.items() if value != 1}
return tweets
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)