forked from latentflip/Mowars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·305 lines (255 loc) · 10.5 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import cgi, os, sys, re
sys.path.insert(0, "lib")
from datetime import date
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import images
import twitter as twapi
from twitpicapi import get_twitpic_image
from twitter_oauth_handler import *
#import PIL
from models import Moustache, get_random_taches, Vote, get_top_taches, get_bottom_taches, get_spider, Spider, get_taches_by_username, get_taches_by_twitpic, check_vote_spam
class BasicPage(webapp.RequestHandler):
def head(self):
self.response.set_status(200)
template_file = ''
def render_template(self, template_values, template_file=''):
if not template_file:
template_file = self.template_file
path = os.path.join(os.path.dirname(__file__), 'templates/'+template_file)
self.response.out.write(template.render(path, template_values))
# class TMP(webapp.RequestHandler):
# def get(self):
# offset = int(self.request.get('off'))
# tashes = Moustache.all().fetch(50,offset)
#
# for tash in tashes:
# the_image = images.resize(tash.image, width=340, height=340)
# tash.image = the_image
# tash.put()
# self.response.out.write(tash.name)
class MainPage(BasicPage):
template_file = 'index.html'
def get(self):
extra_values = {}
try:
loser = Moustache.get_by_id(int(self.request.get('l')))
winner = Moustache.get_by_id(int(self.request.get('w')))
extra_values['last_winner'] = winner
extra_values['last_loser'] = loser
except:
pass
template_values = self.auth_and_taches()
template_values.update(extra_values)
self.render_template(template_values)
def auth_and_taches(self):
client = OAuthClient("twitter", self)
logged_in = client.get_cookie()
extra = {}
if logged_in:
info = client.get("/account/verify_credentials")
extra.update(info)
rate_info = client.get("/account/rate_limit_status")
extra["rate_info"] = rate_info
tache1, tache2 = get_random_taches()
template_values = {
"pagetitle": 'Welcome to Moustache Wars',
"logged_in": logged_in,
"tache1": tache1,
"tache2": tache2,
}
template_values.update(extra)
return template_values
def post(self):
loser = db.get(str(self.request.get('loser')))
winner = db.get(str(self.request.get('winner')))
vote = Vote(winner=winner, loser=loser)
#To store username if the user is logged in
client = OAuthClient("twitter", self)
logged_in = client.get_cookie()
if logged_in:
info = client.get("/account/verify_credentials")
vote.name = info["screen_name"]
else:
vote.name = 'Guest'
#TODO: Stop spam-voting for same key from same ip repeatedly
vote.ip = self.request.remote_addr
if check_vote_spam(vote.ip, winner):
self.redirect('/msg=You+appear+to+be+votespamming,+please+stop+or+try+again+later')
else:
vote.put()
#Wins and losses
loser.losses = loser.losses+1
loser.win_percentage = loser.calc_win_percentage()
loser.put()
winner.wins = winner.wins+1
winner.win_percentage = winner.calc_win_percentage()
winner.put()
#Redirect so that people cannot repost twice
self.redirect('/?w=%s&l=%s' % (winner.key().id(), loser.key().id()))
class CountVote(BasicPage):
def get(self):
win_key = str(self.request.get('k'))
winner = db.get(win_key)
ip = str(self.request.remote_addr)
count = check_vote_spam(ip, winner)
self.response.out.write(str(win_key)+' '+str(count)+' '+ip)
class Top10(BasicPage):
template_file = 'topbottom.html'
def get(self):
"""Returns the top 10 taches template"""
taches = get_top_taches()
template_values = {
'page-title': 'Top 10 Taches',
'taches': taches,
'ranking_type': 'Top',
}
self.render_template(template_values)
class Bottom10(BasicPage):
template_file = 'topbottom.html'
def get(self):
"""Returns the bottom 10 taches template"""
taches = get_bottom_taches()
template_values = {
'pagetitle': 'Bottom 10 Taches',
'taches': taches,
'ranking_type': 'Bottom',
}
self.render_template(template_values)
class Profile(BasicPage):
template_file = 'profile.html'
def get(self, username):
taches = get_taches_by_username(username)
template_values = {
'page-title': 'Profile For '+username,
'twitter_username': username,
'taches': taches,
'ranking_type': 'Top',
}
self.render_template(template_values)
class GetByTwitpic(BasicPage):
template_file = 'profile.html'
def get(self, twitpic):
taches = get_taches_by_twitpic(twitpic)
template_values = {
'page-title': 'Twitpics For '+twitpic,
'taches': taches,
'ranking_type': 'Top',
}
self.render_template(template_values)
class GrabTwitter(webapp.RequestHandler):
def get(self):
today = date.today()
#Get the spider, or create it if we lost it
spider = get_spider()
if not spider:
spider = Spider()
spider.put()
#Find the list of pictures the spider has already found
twitpic_spider_list = spider.twitpics
#Api to call twitter
twitter_search = twapi.Twitter(domain="search.twitter.com")
twitter_search.encoded_args = 'q=&ands=&phrase=twitpic&ors=%23movember+%23mowars&since=' + str(spider.last_since) + '&rpp=100&until=' + str(spider.last_until)
tw_search_results = twitter_search.search()
#Find twitpic links
reg = re.compile(r'http://(www)?twitpic.com/([^\s]*)\s*', re.I)
results = []
for twt in tw_search_results['results']:
#Crudely try to find original tweeter
message = twt['text']
if 'RT' not in message[0:8]:
#Find all twitpics
res = reg.findall(twt['text'])
for url_groups in res:
#This is just the twitpic link slug
twitpic_url = url_groups[1]
#Make a tache
tache = Moustache(name=twt['from_user'], tweet=twt['text'], twitpic = twitpic_url)
#Don't regrab if an older one has
if twitpic_url not in twitpic_spider_list:
try:
tache_image = images.resize(get_twitpic_image(twitpic_url), 340, 340)
tache.image = db.Blob(tache_image)
tache.put()
twitpic_spider_list.append(twitpic_url)
results.append(dict)
except:
pass
#Increase limits by a day a time, or just keep it as today and yesterday
one_day = timedelta(days=1)
spider.last_since = spider.last_until
new_until = spider.last_until + one_day
if new_until<=today:
spider.last_until = new_until
else:
spider.last_until = today
spider.last_since = today - one_day
spider.twitpics = twitpic_spider_list
spider.put()
self.response.out.write(spider.last_until)
self.response.out.write(spider.last_since)
self.response.out.write('\n\n<br><br>')
self.response.out.write(results)
self.response.out.write('\n\n<br><br>')
self.response.out.write(tw_search_results)
class Upload(BasicPage):
def post(self):
client = OAuthClient("twitter", self)
logged_in = client.get_cookie()
if not logged_in:
self.redirect("/")
twitter_info = client.get("/account/verify_credentials")
tache = Moustache()
if self.request.get('add-tache'):
tache.name = twitter_info["screen_name"]
tache_image = images.resize(self.request.get('image'), 340, 340)
tache.image = db.Blob(tache_image)
tache.put()
self.redirect('/profile/'+tache.name)
else:
self.render_form()
def get(self):
client = OAuthClient("twitter", self)
logged_in = client.get_cookie()
if not logged_in:
# TODO: Tell them they need to login + link to Twitter login.
self.redirect("/")
self.render_form()
def render_form(self):
template_values = {
'pagetitle': 'Upload a new tache',
}
self.render(template_values)
class Image (webapp.RequestHandler):
"""Returns an image file based on a tache key (not id)"""
def get(self):
tache_id = self.request.get("img_id")
tache = db.get(tache_id)
the_image = tache.image
if the_image:
self.response.headers['Content-Type'] = "image/jpg"
self.response.out.write(the_image)
else:
self.error(404)
application = webapp.WSGIApplication(
[('/', MainPage),
('/upload', Upload),
('/img', Image),
('/tashwin', Top10),
('/tashfail', Bottom10),
('/grabtwitter', GrabTwitter),
('/profile/(.*)', Profile),
('/tp/(.*)', GetByTwitpic),
('/cnt', CountVote),
#('/tmp', TMP),
# Logins
('/oauth/(.*)/(.*)', OAuthHandler)
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()