-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredditmag-server.py
189 lines (166 loc) · 5.1 KB
/
redditmag-server.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
#!/usr/bin/python
from urllib2 import urlopen, Request, unquote
from urlparse import urljoin
import socket
from lxml import etree
from PIL import Image, ImageOps
import cStringIO
import traceback
import redis
import web
import sys
import json
import resource
rsrc = resource.RLIMIT_STACK
soft, hard = resource.getrlimit(rsrc)
print 'Soft limit starts as :', soft
resource.setrlimit(rsrc, (32768, hard))
soft, hard = resource.getrlimit(rsrc)
print 'Soft limit changed to :', soft
web.config.debug = False
socket.setdefaulttimeout(25)
r = redis.StrictRedis(host='localhost', port=6379, db=3)
parser = etree.HTMLParser()
thumb_size = 260, 260
urls = (
'/', 'index',
'/r/(.*)', 'index',
'/i/(.*)', 'go',
'/j/(.*)/(.*)', 'json'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
def get_header(response_info):
_header = dict(response_info)
header = {}
for k in _header:
header[k.lower()] = _header[k]
return header
def gen_thumb(base_url, image_url, size):
request = Request(image_url, headers={'User-Agent' : "Mozilla/5.0"})
try:
f = urlopen(request)
input = cStringIO.StringIO(f.read())
im = Image.open(input)
w, h = im.size
if w < size[0] * 0.65 or h < size[1] * 0.65:
return False
im = ImageOps.fit(im, size, Image.ANTIALIAS)
output = cStringIO.StringIO()
im.convert('RGB').save(output, "JPEG", quality=68)
r.set(base_url, output.getvalue())
r.expire(base_url, 86400)
input.close()
output.close()
return True
except Exception, e:
print image_url
print traceback.format_exc()
return False
def scrape(url):
try:
request = Request(url, headers={'User-Agent' : "Mozilla/5.0"})
response = urlopen(request, timeout=8)
except Exception, e:
print url
print traceback.format_exc()
return False
header = get_header(response.info())
if('content-type' not in header) : return False
if header['content-type'].startswith("image"):
if gen_thumb(url, url, thumb_size): return True
else: return False
elif header['content-type'].startswith("text/html"):
try:
tree = etree.parse(cStringIO.StringIO(response.read()), parser)
except Exception, e:
print url
print traceback.format_exc()
return False
for img in tree.xpath('.//meta[@property="og:image"]'):
uri = img.get("content")
if gen_thumb(url, uri, thumb_size) is True : return True
for img in tree.xpath('.//link[@rel="image_src"]'):
uri = img.get("href")
if gen_thumb(url, uri, thumb_size) is True : return True
if uri.find("imgur") >= 0 and gen_thumb(url, uri + ".jpg", thumb_size) is True : return True
for img in tree.xpath('.//img[@id="img"]'):
uri = urljoin(response.url, img.get("src"))
if gen_thumb(url, uri, thumb_size) is True : return True
images = {}
for img in tree.xpath('.//img'):
if img.get("data-src") is not None:
uri = urljoin(response.url, img.get("data-src"))
else:
uri = urljoin(response.url, img.get("src"))
if uri not in images:
try:
request = Request(uri, headers={'User-Agent' : "Mozilla/5.0"})
response = urlopen(request, timeout=8)
header = get_header(response.info())
if 'content-type' in header and header['content-type'].startswith("image") and 'content-length' in header:
images[uri] = int(header['content-length'])
except Exception, e:
print uri
print traceback.format_exc()
image_urls = images.items()
image_urls.sort( key=lambda images:(-images[1],images[0]) )
for image_url in image_urls:
if gen_thumb(url, image_url[0], thumb_size): return True
return False
else: return False
class go:
def GET(self, url):
web.header('Server', "redditmag.py")
web.header('ETag', url)
web.header('Last-Modified', 'Mon, 14 Nov 2011 00:48:45 GMT')
url = unquote(url)
if r.exists(url):
s = r.get(url)
if s == "none":
raise web.seeother('/static/noimage.png')
else:
r.expire(url, 86400)
web.header('Content-Length', len(s))
web.header('Content-Type', 'image/jpeg')
return s
else:
if(url.startswith("http://www.reddit.com/r/")):
r.set(url, "none")
r.expire(url, 86400)
raise web.seeother('/static/noimage.png')
res = scrape(url)
if res is True:
web.header('Content-Type', 'image/jpeg')
return r.get(url)
else:
r.set(url, "none")
r.expire(url, 3600)
raise web.seeother('/static/noimage.png')
class index:
def GET(self, subreddit=None):
if subreddit == None: raise web.seeother('/r/pics')
return render.index(subreddit)
class json:
def GET(self, subreddit, after):
key = after
if(after == "") : key = subreddit
if r.exists(key):
content = r.get(key)
else:
try:
request = Request("http://www.reddit.com/r/"+subreddit+"/.json?after="+after)
response = urlopen(request)
except Exception, e:
print traceback.format_exc()
r.set(key, "30 second limit hit (from cache)")
r.expire(key, 30)
return "30 second limit hit (fresh request)"
content = response.read()
r.set(key, content)
r.expire(key, 120)
web.header('Content-Length', len(content))
web.header('Content-Type', 'application/json')
return content
if __name__ == '__main__' :
app.run()