Robohash/webfront.py

216 lines
7.2 KiB
Python
Raw Normal View History

2011-07-05 15:42:12 -04:00
#!/usr/bin/env python
#
# Copyright 2011 Pluric
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.escape
import socket
import re
2011-07-05 19:31:17 -04:00
import os
2011-07-05 15:42:12 -04:00
import pprint
import Image
2011-07-05 19:31:17 -04:00
import hashlib
import urllib
2011-07-06 01:41:06 -04:00
import random
2011-07-06 06:39:35 +00:00
from tornado.options import define, options
2011-07-05 15:42:12 -04:00
try:
from hashlib import md5 as md5_func
except ImportError:
from md5 import new as md5_func
2011-07-06 06:39:35 +00:00
define("port", default=80, help="run on the given port", type=int)
2011-07-05 15:42:12 -04:00
2011-07-05 19:31:17 -04:00
class Robohash(object):
def __init__(self,string):
hash = hashlib.sha512()
hash.update(string)
self.hexdigest = hash.hexdigest()
self.hasharray = []
2011-07-07 18:49:57 -04:00
#Start this at 3, so earlier is reserved
#0 = Color
#1 = Set
#2 = bgset
#3 = BG
self.iter = 4
2011-07-06 01:41:06 -04:00
2011-07-05 19:31:17 -04:00
def createHashes(self,count):
#Create and store a series of hash-values
#Basically, split up a hash (SHA/md5/etc) into X parts
for i in range(0,count):
#Get 1/numblocks of the hash
blocksize = (len(self.hexdigest) / count)
currentstart = (1 + i) * blocksize - blocksize
currentend = (1 +i) * blocksize
self.hasharray.append(int(self.hexdigest[currentstart:currentend],16))
def dirCount(self,path):
#return the count of just the directories beneath me.
return sum([len(dirs) for (root, dirs, files) in os.walk(path)])
2011-07-05 21:31:42 -04:00
def getHashList(self,path):
2011-07-05 19:31:17 -04:00
#Each iteration, if we hit a directory, recurse
#If not, choose the appropriate file, given the hashes, stored above
2011-07-06 01:41:06 -04:00
2011-07-05 19:31:17 -04:00
completelist = []
locallist = []
for ls in os.listdir(path):
if not ls.startswith("."):
if os.path.isdir(path + "/" + ls):
2011-07-05 21:31:42 -04:00
subfiles = self.getHashList(path + "/" + ls)
2011-07-05 19:31:17 -04:00
if subfiles is not None:
completelist = completelist + subfiles
else:
locallist.append( path + "/" + ls)
if len(locallist) > 0:
2011-07-05 21:31:42 -04:00
elementchoice = self.hasharray[self.iter] % len(locallist)
2011-07-05 19:31:17 -04:00
luckyelement = locallist[elementchoice]
locallist = []
locallist.append(luckyelement)
2011-07-05 21:31:42 -04:00
self.iter += 1
2011-07-05 19:31:17 -04:00
completelist = completelist + locallist
2011-07-05 21:31:42 -04:00
return completelist
2011-07-05 19:31:17 -04:00
2011-07-05 21:09:08 -04:00
class MainHandler(tornado.web.RequestHandler):
def get(self):
ip = self.request.remote_ip
self.write(self.render_string('templates/root.html',ip=ip))
2011-07-05 19:31:17 -04:00
class ImgHandler(tornado.web.RequestHandler):
def get(self,string=None):
2011-07-06 01:41:06 -04:00
colors = ['blue','brown','green','grey','orange','pink','purple','red','white','yellow']
sets = ['set1','set2','set3']
2011-07-07 18:49:57 -04:00
bgsets = ['bg1','bg2']
2011-07-06 01:41:06 -04:00
2011-07-05 19:31:17 -04:00
#Create a hash for the string as given
if string is None:
string = self.request.remote_ip
string = urllib.quote_plus(string)
r = Robohash(string)
2011-07-06 01:41:06 -04:00
#Create 10 hashes. This should be long enough for the current crop of variables.
#This is probably not insecure, sicne we'd be modding anyway. This just spreads it out more.
2011-07-07 18:49:57 -04:00
r.createHashes(11)
2011-07-06 01:41:06 -04:00
2011-07-06 02:31:34 -04:00
2011-07-06 01:41:06 -04:00
#Now, customize the request if possible.
client_color = ""
2011-07-06 02:31:34 -04:00
client_set = ""
2011-07-07 18:49:57 -04:00
client_bgset = ""
sizex = 300
sizey = 300
if "size" in self.request.arguments:
sizelist = self.get_argument("size").split(tornado.escape.xhtml_escape("x"),3)
if ((int(sizelist[0]) > 0) and (int(sizelist[0]) < 4096)):
sizex = int(sizelist[0])
if ((int(sizelist[0]) > 0) and (int(sizelist[0]) < 4096)):
sizey = int(sizelist[1])
2011-07-06 01:41:06 -04:00
if "set" in self.request.arguments:
2011-07-07 18:49:57 -04:00
if tornado.escape.xhtml_escape(self.get_argument("set")) == 'any':
client_set = sets[r.hasharray[1] % len(sets) ]
if self.get_argument("set") in sets:
client_set = tornado.escape.xhtml_escape(self.get_argument("set"))
else:
#If no set specified, you get set 1
client_set = "set1"
if client_set == 'set1':
client_set = colors[r.hasharray[0] % len(colors) ]
2011-07-06 02:31:34 -04:00
if "color" in self.request.arguments:
if self.get_argument("color") in colors:
client_set = tornado.escape.xhtml_escape(self.get_argument("color"))
2011-07-07 18:49:57 -04:00
if "bgset" in self.request.arguments:
if self.get_argument("bgset") in bgsets:
client_bgset = tornado.escape.xhtml_escape(self.get_argument("bgset"))
else:
client_bgset = bgsets[r.hasharray[2] % len(bgsets) ]
2011-07-06 02:31:34 -04:00
#If they don't specify a color, use hashvalue
if ((client_color == "") and (client_set == "")):
client_set = colors[r.hasharray[0] % len(colors) ]
2011-07-06 01:41:06 -04:00
2011-07-05 19:31:17 -04:00
#Change to a usuable format
2011-07-08 02:29:54 -04:00
if string.endswith(('.png','.gif','.jpg','.bmp','.im','.jpeg','.pcx','.ppm','.tiff','.xbm','tif')):
2011-07-05 19:31:17 -04:00
ext = string[string.rfind('.') +1 :len(string)]
2011-07-08 02:17:37 -04:00
if ext.lower() == 'jpg':
ext = 'jpeg'
2011-07-08 02:29:54 -04:00
if ext.lower() == 'tif':
ext = 'tiff'
2011-07-05 19:31:17 -04:00
else:
ext = "png"
self.set_header("Content-Type", "image/" + ext)
2011-07-06 01:41:06 -04:00
hashlist = r.getHashList(client_set)
2011-07-05 15:42:12 -04:00
hashlist.sort()
robohash = Image.open(hashlist[0])
2011-07-07 18:49:57 -04:00
robohash = robohash.resize((1024,1024))
2011-07-05 15:42:12 -04:00
for png in hashlist:
img = Image.open(png)
2011-07-07 18:49:57 -04:00
img = img.resize((1024,1024))
2011-07-05 15:42:12 -04:00
robohash.paste(img,(0,0),img)
2011-07-05 21:09:08 -04:00
if ext == 'bmp':
#Flatten bmps
r, g, b, a = robohash.split()
robohash = Image.merge("RGB", (r, g, b))
2011-07-07 18:49:57 -04:00
if client_bgset is not "":
bglist = []
for ls in os.listdir(client_bgset):
if not ls.startswith("."):
bglist.append(client_bgset + "/" + ls)
bg = Image.open(bglist[r.hasharray[3] % len(bglist)])
bg = bg.resize((1024,1024))
bg.paste(robohash,(0,0),robohash)
robohash = bg
robohash = robohash.resize((sizex,sizey))
2011-07-05 19:31:17 -04:00
robohash.save(self,format=ext)
2011-07-05 15:42:12 -04:00
2011-07-05 19:31:17 -04:00
2011-07-05 15:42:12 -04:00
2011-07-06 06:39:35 +00:00
def main():
tornado.options.parse_command_line()
# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
"cookie_secret": "9b90a85cfe46cad5ec136ee44a3fa332",
"login_url": "/login",
"xsrf_cookies": True,
}
application = tornado.web.Application([
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": os.path.join(os.path.dirname(__file__),
"static/")}),
(r"/", MainHandler),
(r"/(.*)", ImgHandler),
], **settings)
2011-07-06 18:14:09 +00:00
http_server = tornado.httpserver.HTTPServer(application,xheaders=True)
2011-07-06 06:39:35 +00:00
http_server.listen(options.port)
2011-07-05 15:42:12 -04:00
tornado.ioloop.IOLoop.instance().start()
2011-07-06 06:39:35 +00:00
if __name__ == "__main__":
main()