-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunfetchy.py
More file actions
223 lines (176 loc) · 6.83 KB
/
funfetchy.py
File metadata and controls
223 lines (176 loc) · 6.83 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
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
import os
import json
import random
import urlparse
import unicodedata
import webapp2 as webapp
from google.appengine.api import images
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch
#RedditSubmissions data model
##############################################
class RedditSubmissions(db.Model):
created_date = db.DateTimeProperty(auto_now_add=True)
json = db.TextProperty()
data = db.BlobProperty()
width = db.IntegerProperty()
height = db.IntegerProperty()
title = db.StringProperty()
url = db.StringProperty()
author = db.StringProperty()
score = db.IntegerProperty()
rand = db.FloatProperty()
star = db.BooleanProperty()
#Request handler
##############################################
class FfBaseHandler(webapp.RequestHandler):
def template_path(self, filename):
return os.path.join(os.path.dirname(__file__), filename)
def render_to_response(self, filename, template_args):
template_args.setdefault('current_uri', self.request.uri)
self.response.out.write(str(
template.render(self.template_path(filename), template_args)))
#Show eveything starred
##############################################
class FfSlideshow(FfBaseHandler):
def get(self):
submissions = RedditSubmissions.all().filter('star =',True).order('-created_date').fetch(99)
self.render_to_response('templatehtml/index.html', {
'subs': submissions,
})
#Show eveything in webgl
##############################################
class FfPass(FfBaseHandler):
def get(self):
submissions = RedditSubmissions.all().order('-created_date').fetch(99)
self.render_to_response('templatehtml/webgl.html', {
'subs': submissions,
'size': len(submissions),
'one': submissions[0],
})
#Show eveything in html5
##############################################
class FfNew(FfBaseHandler):
def get(self):
submissions = RedditSubmissions.all().order('-created_date').fetch(99)
self.render_to_response('templatehtml/new.html', {
'subs': submissions,
})
#Show random in html5
##############################################
class FfRandom(FfBaseHandler):
def get(self):
submissions = RedditSubmissions.all().filter('rand > ', random.random()).order('rand').fetch(99)
self.render_to_response('templatehtml/index.html', {
'subs': submissions,
})
#Delete cron job
##############################################
class FfDelete(webapp.RequestHandler):
def get(self):
#DELETE ALL PREVIOUS POSTS
s = RedditSubmissions.all().order('-created_date').fetch(99);
for j in s:
if not j.star:
print j
j.delete()
#Set starred in html5
##############################################
class FfUpVote2(FfBaseHandler):
def post(self,pic_key):
sub = db.get(pic_key)
if not sub.star:
sub.star = True
else:
sub.star = False
sub.put()
self.redirect('/new')
#Set starred in webgl
##############################################
class FfUpVote(FfBaseHandler):
def post(self,pic_key):
sub = db.get(pic_key)
if not sub.star:
sub.star = True
else:
sub.star = False
sub.put()
self.redirect('/webgl')
#Utility serve image
##############################################
class FfServeImage(webapp.RequestHandler):
def get(self,pic_key):
image = db.get(pic_key)
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(str(image.data))
#Grab images from the passed reddit page
# i.e. <web>/update/(funny/wtf/etc)
##############################################
class FfUpdate(webapp.RequestHandler):
def get(self,page):
page_json = urlfetch.Fetch('http://www.reddit.com/r/'+page+'.json' )
#print page,page_json.content
obj = json.loads( page_json.content )
#print(obj.get('data').get('children'))
for subs in obj.get('data').get('children'):
if not subs['data']['url']:
continue
path = urlparse.urlparse(subs['data']['url']).path
ext = os.path.splitext(path)[1]
if not ext or ext == ".gif":
continue
title = subs['data']['title']
if title.find("NSFW") > 0:
print "<p>", title.encode('utf-8'), "discarded because NSFW.. </p>"
continue
tt = subs['data']['url'];
s = RedditSubmissions.all();
r = s.filter('url =', tt).fetch(limit=1)
if len(r) > 0:
print "<p>", title.encode('utf-8'), tt.encode('utf-8'), "Already inserted. </p>"
continue
try:
image = urlfetch.Fetch(subs['data']['url']).content
img = images.Image(image)
img.im_feeling_lucky()
if img.width > 2048 or img.height > 1600:
continue
if img.width > 1024 or img.height > 768:
img.resize(img.width/2,img.height/2)
png_data = img
png_data = img.execute_transforms(images.PNG)
temp = subs['data']['title']
temp = temp.replace("\"", "\'")
temp = unicodedata.normalize('NFKD', temp).encode('ascii','ignore')
RedditSubmissions(
data= png_data,
width = img.width,
height = img.height,
json = "",
title = temp,
url = unicodedata.normalize('NFKD', subs['data']['url']).encode('ascii','ignore'),
author = unicodedata.normalize('NFKD', subs['data']['author']).encode('ascii','ignore'),
score = int(subs['data']['score']),
rand = random.random(),
star = False,
).put()
print "<p>", title.encode('utf-8'), tt.encode('utf-8'), len(r), ext.encode('utf-8'), "inserted! </p>"
except Exception,e:
print e
self.redirect('/')
#self.render_to_response('templatehtml/upload.html', {'subs': sub })
##############################################
# URL MAP DEFINITION
##############################################
url_map = [
('/delete', FfDelete),
('/new', FfNew),
('/random', FfRandom),
('/webgl', FfPass),
('/image/([-\w]+)', FfServeImage),
('/upvote/([-\w]+)', FfUpVote),
('/upvote2/([-\w]+)', FfUpVote2),
('/update/([-\w]+)', FfUpdate),
('/', FfSlideshow)]
application = webapp.WSGIApplication(url_map,debug=True)