User:Eleanorg/1.3/Dissolute Image/Code2
< User:Eleanorg
Revision as of 10:04, 4 September 2012 by Eleanorg (talk | contribs) (→extract image file from url submitted)
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('filename','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>"""
input form for user to give url
- HTML form to recieve image URL
- Linked from above script - 'upload your img, then give the url /here/'
extract image file from url submitted
- use urlLib to read the page
- Get the image file, check its hash against the db
- if correct, put url into db for that pixel
rough outline:
#!/usr/bin/python
#-*- coding:utf-8 -*-
import urllib
import pymongo
from pymongo import Connection
# check url given by user for correct image file
##--------- connect to DB ------------#
connection = Connection()
myDB = connection['pixelDbQuery1']
collection = myDB.collection
##--------- recieve url from input form ------------#
form = cgi.FieldStorage() # Grabs whatever input comes from form
#pixelHash = form.getvalue("pixelHash") # pixel info is sent invisibly with the form? --> no, hash generated below then looked for in db
#TODO check if valid url/protocol given. urllib2 bit breaks if not.
#url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html")
#for testing:
url = http://myurl.com
## ------ ascertain correct hash for this pixel-------------------#
# get xpos & ypos from image file given by user
# lookup in db to find its hash?
##--------- define function to get hash of user img ------------#
def checkHash():
# get hash of user's image
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()
userHash = md5sum(open(image,'rb'))
# compare it to correct pixel hash
# TODO loop thru db looking for that hash. if found, associate this url with that pixel in DB
#if userHash is found in db
return True
#add to db
else:
return False
##--------- retrieve image from url & check its hash against hash in db ------------#
# TODO plug in charlie's code here from 1checkUrlUrlLib.cgi - it checks if url is valid, then grabs images from that url
# check response code
# if 404: throw error
# else:
# check filetype
# if image:
# checkHash()
# else:
# loop through images on page, for each one, checkHash() ----> this needs to change; throw error and ask for actual img url (or won't be able to embed)
create html doc with embedded images
- embeds images as per database urls
- how to load quickly, not having to go thru db on each load?