User:Eleanorg/1.3/Dissolute Image/Code2

From XPUB & Lens-Based wiki
< User:Eleanorg
Revision as of 15:11, 3 August 2012 by Eleanorg (talk | contribs) (Created page with "===create an image file=== This script grabs an unadopted pixel randomly from the DB, creates an image file on the fly and gives it to the user. It also creates a hash for that f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

create an image file

This script grabs an unadopted pixel randomly from the DB, creates an image file on the fly and gives it to the user. It also creates a hash for that file to ensure that users can't tamper with it.

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
import cgi
import cgitb; cgitb.enable()
import hashlib
import os
import pymongo, random  
from pymongo import Connection
 
## grab a random unadopted pixel from the database and create an image out of it

connection = Connection()
myDB = connection['pixelDbQuery1']
collection = myDB.collection

##--------- choose pixel randomly from DB ------------#
# 
# find all entries where url is empty:
unadopted = []
for entry in collection.find({"url": ""}):
    unadopted.append(entry)			# add this pixel's hash as an item to the 'unadopted' dictionary
#print unadopted
 
howMany = len(unadopted)			# find out how many items in 'unadopted' list
#print howMany
random = random.randint(0,howMany)		# pick a random number in this range
#print random
pixelHash = unadopted[random]			# ...and choose the pixel at this index
#print pixelHash

#color = "#333"
#xpos = str(10)
#ypos = str(20)
pixelId = pixelHash['ID']
color = pixelHash['color']
xpos = pixelHash['xpos']
ypos = pixelHash['ypos']
image = "../../forbiddenPixels/images/" + xpos + "_" + ypos + ".jpg"
#print image

#--------- create an image with it ------------#

# creates new image and saves to images folder
command = "convert -size 1x1 xc:" + color + " " + image   
#print command	           	
os.popen(command, 'r').read()


#--------- create hash for img and put in DB ------------#
 
# md5sum -- create an hex digest of a file's md5 hash
# usage: print md5sum(open('tmp.html','rb'))

#def md5sum(f, block_size=8192):
#    md5 = hashlib.md5()
#    while True:
#        data = f.read(block_size)
#        if not data:
#            break
#        md5.update(data)
#    return md5.hexdigest()

## test:
## print md5sum(open('tmp.html','rb'))

# define hashing function
def md5sum(f, block_size=8192):
    md5 = hashlib.md5()
    while True:
        data = f.read(block_size)
        if not data:
            break
        md5.update(data)
    return md5.hexdigest()

# run hashing function, assigning to var 'hash'
pixelHash = md5sum(open(image,'rb'))
# insert hash for this pixel into its array in DB
collection.update( {"ID": pixelId}, {"$set":{"hash": pixelHash}})

#--------- give user link to the image ------------#
 
htmlHeader = """<!DOCTYPE html>
<html>
  <head>
    <title>Adopt a Pixel</title>
        <link <link rel="stylesheet" href="">
  </head>
  <body>"""
 
 
print "Content-Type: text/html"
print 
print htmlHeader
print """
<h1>the DISSOLUTE IMAGE</h1>
Right click & 'save as' to download your pixel: <br />

<a href=" """ + image + """ ">Download Pixel</a>
<br />
The hash is """ + pixelHash + """
</body>
</html>"""