User:Eleanorg/1.3/Dissolute Image/Code/verify input: Difference between revisions
(Created page with "untested, 15 june 6pm <source lang="python"> #!/usr/bin/python #-*- coding:utf-8 -*- import cgi, os, subprocess, re import cgitb; cgitb.enable() import pymongo from pymongo im...") |
No edit summary |
||
Line 1: | Line 1: | ||
21 june, throwing value error line 74 | |||
Traceback (most recent call last): | |||
File "pixelsScrapeAndVerify.cgi", line 106, in <module> | |||
split = splitString(string) # calls split string function, passing it the string recieved from the input form, and assigns it to var 'split' | |||
File "pixelsScrapeAndVerify.cgi", line 74, in splitString | |||
k,v = s.split('=') # splits each capture on the = into key and value | |||
ValueError: too many values to unpack | |||
(working last night... now what did i change?) | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
#-*- coding:utf-8 -*- | |||
import cgi | |||
import os, subprocess, re, urllib2 | |||
import cgitb; cgitb.enable() | |||
import pymongo | |||
from pymongo import Connection | |||
#from pymongo.objectid import ObjectId | |||
# recieves url from inputForm.html scrapes it to check if pixel has been put there correctly | |||
# if yes = updates db entry for that pixel | |||
# if no = throws error | |||
#-------------- error / thank you messages ----------------# | |||
htmlHeader = """<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title>A form talking to a python script</title> | |||
<link <link rel="stylesheet" href="../../pixels.css"> | |||
</head> | |||
<body> | |||
<div class="container txt" style="margin-top:100px;">""" | |||
htmlFooter = """ | |||
</div> | |||
<div> | |||
<a href="#"><< Back to the image</a> | |||
</div> | |||
</body> | |||
</html>""" | |||
ScrapingError = """Sorry, your pixel details couldn't be found at that URL! <br /> | |||
<li>Did you paste it exactly as it appears?</li> | |||
<li>Is the page where you pasted it publicly accessible?</li> | |||
<a href="inputForm.cgi">Try again</a> | |||
""" | |||
verifyError = """Sorry, the details you've put online for that pixel seem to be wrong. | |||
<li>Did you paste the pixel details exactly as they appear?</li> | |||
<a href="inputForm.cgi">Try again</a>""" | |||
thankyou = """Thanks, URL submitted. <br /> | |||
Your pixel has been added to the image.""" | |||
#------------- get URL from input form -------------------# | |||
#form = cgi.FieldStorage() # Grabs whatever input comes from form | |||
#string = form.getvalue("pixel") | |||
#url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html") # assigns form's input to var 'url'. url on the right is a default value that will be printed for testing if nothing is recieved from the form | |||
#------------- print html header--------------------------# | |||
print "Content-Type: text/html" | |||
print | |||
print htmlHeader | |||
#------------ define splitting function-------------------# | |||
# to split user's string into a hash which can be verified against the one in db | |||
def splitString (stg) : | |||
pat = re.compile("(\w{1,4}=[^;]+);?") # captures each foo=blah pair in the string, separated by ;s | |||
cap = pat.findall(stg) # cap variable contains all four captures - id, x, y, color (if valid) | |||
# put captures into a hash for this pixel | |||
myhash = {} | |||
for s in cap : | |||
k,v = s.split('=') # splits each capture on the = into key and value | |||
myhash[k] = v # puts new k/v pair into hash with these values | |||
# return hash so it can be compared with that in the db | |||
try : | |||
myhash['idNo'] and myhash['x'] and myhash['y'] and myhash['col'] | |||
except: | |||
return # missing parameter! return nothing | |||
return myhash # returns a hash as above, accessed thus: split['x'] etc | |||
# | |||
##------------- scrape and check the url -----------------# | |||
## if correct string not found, throw error | |||
## if found, print thankyou and update db | |||
# connect to db for verification, and possible updating | |||
connection = Connection() | |||
myDB = connection['pixelDbQuery1'] | |||
collection = myDB.collection | |||
## for testing: | |||
##TODO add backslashes to user's string before performing regex | |||
string = r"id=65,x=4,y=3,col=red" | |||
url = "http://ox4.org/~nor/trials/hostedString.html" | |||
text = urllib2.urlopen(url).read() # reads page at the specified URL | |||
#print text | |||
if not re.search(string, text): # if string isn't matched within 'text' | |||
# print error # print error message to user | |||
print scrapingError | |||
else: | |||
split = splitString(string) # calls split string function, passing it the string recieved from the input form, and assigns it to var 'split' | |||
if split: | |||
idNo = int(split['idNo']) # get idNo according to user's string; cast as int to compare with db | |||
#---------- verify scraped pixel info against the db ---------# | |||
pixelHash = myDB.collection.find_one({'ID': idNo}) # this grabs a hash with the right id number (if valid), whose index thingies you can access | |||
if pixelHash: | |||
# print "yes, that pixel id exists in db" | |||
# # now check if attributes from string recieved (xpos, ypos) match attributes for that ID in DB? | |||
if split['x'] == pixelHash['xpos'] and split['y'] == pixelHash['ypos']: | |||
# print "correct x and y value given" | |||
collection.update( {'ID': idNo}, {"$set":{'url': url}}) # set pixel with string matching 'pat' to url given by user | |||
print thankyou | |||
else: | |||
print verifyError | |||
else: | |||
print verifyError | |||
print htmlFooter | |||
##---------------- trigger pixelsCheckDb.py -----------# | |||
##command = "python pixelsCheckDB.py" # script called here must be executable! | |||
##os.popen(command, 'r').read() | |||
</source> | |||
untested, 15 june 6pm | untested, 15 june 6pm | ||
<source lang="python"> | <source lang="python"> |
Latest revision as of 14:00, 21 June 2012
21 june, throwing value error line 74 Traceback (most recent call last):
File "pixelsScrapeAndVerify.cgi", line 106, in <module> split = splitString(string) # calls split string function, passing it the string recieved from the input form, and assigns it to var 'split' File "pixelsScrapeAndVerify.cgi", line 74, in splitString k,v = s.split('=') # splits each capture on the = into key and value
ValueError: too many values to unpack (working last night... now what did i change?)
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi
import os, subprocess, re, urllib2
import cgitb; cgitb.enable()
import pymongo
from pymongo import Connection
#from pymongo.objectid import ObjectId
# recieves url from inputForm.html scrapes it to check if pixel has been put there correctly
# if yes = updates db entry for that pixel
# if no = throws error
#-------------- error / thank you messages ----------------#
htmlHeader = """<!DOCTYPE html>
<html>
<head>
<title>A form talking to a python script</title>
<link <link rel="stylesheet" href="../../pixels.css">
</head>
<body>
<div class="container txt" style="margin-top:100px;">"""
htmlFooter = """
</div>
<div>
<a href="#"><< Back to the image</a>
</div>
</body>
</html>"""
ScrapingError = """Sorry, your pixel details couldn't be found at that URL! <br />
<li>Did you paste it exactly as it appears?</li>
<li>Is the page where you pasted it publicly accessible?</li>
<a href="inputForm.cgi">Try again</a>
"""
verifyError = """Sorry, the details you've put online for that pixel seem to be wrong.
<li>Did you paste the pixel details exactly as they appear?</li>
<a href="inputForm.cgi">Try again</a>"""
thankyou = """Thanks, URL submitted. <br />
Your pixel has been added to the image."""
#------------- get URL from input form -------------------#
#form = cgi.FieldStorage() # Grabs whatever input comes from form
#string = form.getvalue("pixel")
#url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html") # assigns form's input to var 'url'. url on the right is a default value that will be printed for testing if nothing is recieved from the form
#------------- print html header--------------------------#
print "Content-Type: text/html"
print
print htmlHeader
#------------ define splitting function-------------------#
# to split user's string into a hash which can be verified against the one in db
def splitString (stg) :
pat = re.compile("(\w{1,4}=[^;]+);?") # captures each foo=blah pair in the string, separated by ;s
cap = pat.findall(stg) # cap variable contains all four captures - id, x, y, color (if valid)
# put captures into a hash for this pixel
myhash = {}
for s in cap :
k,v = s.split('=') # splits each capture on the = into key and value
myhash[k] = v # puts new k/v pair into hash with these values
# return hash so it can be compared with that in the db
try :
myhash['idNo'] and myhash['x'] and myhash['y'] and myhash['col']
except:
return # missing parameter! return nothing
return myhash # returns a hash as above, accessed thus: split['x'] etc
#
##------------- scrape and check the url -----------------#
## if correct string not found, throw error
## if found, print thankyou and update db
# connect to db for verification, and possible updating
connection = Connection()
myDB = connection['pixelDbQuery1']
collection = myDB.collection
## for testing:
##TODO add backslashes to user's string before performing regex
string = r"id=65,x=4,y=3,col=red"
url = "http://ox4.org/~nor/trials/hostedString.html"
text = urllib2.urlopen(url).read() # reads page at the specified URL
#print text
if not re.search(string, text): # if string isn't matched within 'text'
# print error # print error message to user
print scrapingError
else:
split = splitString(string) # calls split string function, passing it the string recieved from the input form, and assigns it to var 'split'
if split:
idNo = int(split['idNo']) # get idNo according to user's string; cast as int to compare with db
#---------- verify scraped pixel info against the db ---------#
pixelHash = myDB.collection.find_one({'ID': idNo}) # this grabs a hash with the right id number (if valid), whose index thingies you can access
if pixelHash:
# print "yes, that pixel id exists in db"
# # now check if attributes from string recieved (xpos, ypos) match attributes for that ID in DB?
if split['x'] == pixelHash['xpos'] and split['y'] == pixelHash['ypos']:
# print "correct x and y value given"
collection.update( {'ID': idNo}, {"$set":{'url': url}}) # set pixel with string matching 'pat' to url given by user
print thankyou
else:
print verifyError
else:
print verifyError
print htmlFooter
##---------------- trigger pixelsCheckDb.py -----------#
##command = "python pixelsCheckDB.py" # script called here must be executable!
##os.popen(command, 'r').read()
untested, 15 june 6pm <source lang="python">
- !/usr/bin/python
- -*- coding:utf-8 -*-
import cgi, os, subprocess, re import cgitb; cgitb.enable() import pymongo from pymongo import Connection
- recieves url from inputForm.html scrapes it to check if pixel has been put there correctly
- if yes = updates db entry for that pixel
- if no = throws error
- -------------- error / thank you messages ----------------#
htmlHeader = """<!DOCTYPE html> <html>
<head> <title>A form talking to a python script</title> <link <link rel="stylesheet" href="../../pixels.css"> </head> <body>
htmlFooter = """
</body>
</html>"""
error = """Sorry, your pixel details couldn't be found at that URL!
"""
thankyou = """Thanks, URL submitted.
Your pixel has been added to the image."""
- ------------- get URL from input form -------------------#
form = cgi.FieldStorage() # Grabs whatever input comes from form
- string = form.getvalue("pixel")
- url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html") # assigns form's input to var 'url'. url on the right is a default value that will be printed for testing if nothing is recieved from the form
- for testing:
string = "x=123;y=345;col=(255,255,255);idNo=ObjectId('4fdb5b75ed9c8d4743000000')" url = "http://ox4.org/~nor/trials/hostedString.html"
- ------------- print html header--------------------------#
- print "Content-Type: text/html"
- print htmlHeader
- ------------ define splitting function-------------------#
- to split user's string into a hash which can be verified against the one in db
def splitString (stg) : pat = re.compile("(\w{1,4}=[^;]+);?") # captures each foo=blah pair in the string, separated by ;s cap = pat.findall(stg) # cap variable contains all four captures - id, x, y, color (if valid) # put captures into a hash for this pixel myhash = {} for s in cap : k,v = s.split('=') # splits each capture on the = into key and value myhash[k] = v # puts new k/v pair into hash with these values # return hash so it can be compared with that in the db try : myhash['x'] and myhash['y'] and myhash['col'] and myhash['idNo'] except: return # missing parameter! return nothing return myhash # returns a hash as above, accessed thus: paramGet(stg)['x'] etc
- ------------- scrape and check the url -----------------#
- if correct string not found, throw error
- if found, print thankyou and update db
- connect to db for verification, and possible updating
connection = Connection() myDB = connection['pixelsDbTest0'] collection = myDB.collection text = urllib2.urlopen(url).read() # reads page at the specified URL if not re.search(pat, text): # if pattern 'pat' (ie unique pixel string) isn't matched within 'text'
- print error # print error message to user
print "sorry, couldnt scrape ur string" else: pixelHash = myDB.collection.find({"id": idNo}) # check db to see if ID exists if pixelHash: # check if attributes from string recieved (xpos, ypos + colour) match attributes for that ID in DB? split = splitString(string) # calls split string function, passing it the string recieved from the input form, and assigns it to var 'split' if split: print "hurrah, string was split into a hash" # now do verification against db: # split['x'] should == pixelHash['x'] etc # split['idNo'] should == pixelHash['id'] << remember you're not allowed 'id' as a hash key in python # if all 4 attributes are the same, print hurrah and update db else print "boo, splitting didnt work" # if yes: # print thank you # trigger drawing script # else print error else:
- print error
print "sorry, no pixel with that id exists in the db" #print thankyou #collection.update( {"string":pat}, {"$set":{"url": url}}) # set pixel with string matching 'pat' to url given by user
- print htmlFooter
<source>