User:Eleanorg/1.2/Thematic/integrated assessment: Difference between revisions
(Created page with "==URL Submission Form==") |
No edit summary |
||
Line 1: | Line 1: | ||
==URL | ==Displaying Image== | ||
<source lang="python"> | |||
#!/usr/bin/python | |||
#-*- coding:utf-8 -*- | |||
import cgi, glob | |||
import cgitb; cgitb.enable() | |||
#-------- ascertain latest image filename -------------# | |||
images = glob.glob('../../forbiddenPixels/images/*') | |||
number = len(images) # counts number of files in images folder | |||
latest = '%05d' % number # converts to 5 digits. note: this changes type from int to str | |||
#print number | |||
#print latest | |||
image = "../../forbiddenPixels/images/" + latest + ".png" | |||
#-------- print html doc with image ------------------# | |||
htmlHeader = """<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title>A Censored Image</title> | |||
<link <link rel="stylesheet" href="../../pixels.css"> | |||
</head> | |||
<body>""" | |||
print "Content-Type: text/html" | |||
print | |||
print htmlHeader | |||
print """ | |||
<div class="container img"> | |||
<img src=" """ + image + """ " alt="Sorry, image not currently available" /> | |||
</div> | |||
<div class="centred"> | |||
This image has been censored. Help to bring it back online, one pixel at a time:<br /><br /> <a href="inputForm.cgi"><button>adopt a pixel</button></a> | |||
</div> | |||
""" | |||
print """ | |||
</body> | |||
</html>""" | |||
</source> | |||
==URL Input Form== | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
#-*- coding:utf-8 -*- | |||
import cgi, random | |||
import cgitb; cgitb.enable() | |||
# prints out the URL input form, giving the user a random pixel string (for testing). | |||
htmlHeader = """<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title>Adopt a Pixel</title> | |||
<link <link rel="stylesheet" href="../../pixels.css"> | |||
</head> | |||
<body>""" | |||
print "Content-Type: text/html" | |||
print | |||
print htmlHeader | |||
print """ | |||
<div class="centred" style="margin-top:50px;"> | |||
Below is the unique identifier for your pixel. | |||
Copy and paste it somewhere public on the internet, exactly as it appears: | |||
</div> | |||
<div class="container txt"> | |||
<span class="pixelString"> | |||
Pixel position:""" + str(random.randint(100,500)) + "." + str(random.randint(100,300)) + "; Color:rgba(" + str(random.randint(100,255)) + "," + str(random.randint(100,255)) + "," + str(random.randint(100,255)) + """,1) | |||
</span> | |||
</div> | |||
<div class="centred" style="margin-top:50px;"> | |||
Then tell us the URL of the page where you pasted it: | |||
</div> | |||
<div class="container txt"> | |||
<form action="saveUrl.cgi" name="inputForm"> | |||
<span class="containerText">Paste url: </span> | |||
<input name="url"> | |||
<input type="submit" value="OK"> | |||
</form> | |||
</div> | |||
<div class="centred"> | |||
Your pixel will appear in the image for as long as the text remains at this URL. | |||
</div> | |||
</body> | |||
</html>""" | |||
</source> | |||
==Saving the URL== | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
#-*- coding:utf-8 -*- | |||
import cgi, os, subprocess | |||
import cgitb; cgitb.enable() | |||
# recieves url from inputForm.html and saves the submitted url as a new line in urls.txt | |||
#------------- get URL from input form -------------------# | |||
form = cgi.FieldStorage() # Grabs whatever input comes from form | |||
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 | |||
#------------- save url to a text file --------------------# | |||
f = open("data/urls.txt", 'a') # opens text file in write mode | |||
f.write(url + '\n') # write url then newline | |||
f.close() | |||
#-----------run scrapeNdraw script and pipe to bash--------# | |||
command = "./scrapeDraw.cgi | bash" # script called here must be executable! | |||
os.popen(command, 'r').read() | |||
#------------- print acknowledgement ----------------------# | |||
htmlHeader = """<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title>A form talking to a python script</title> | |||
<link <link rel="stylesheet" href="../../pixels.css"> | |||
</head> | |||
<body>""" | |||
print "Content-Type: text/html" | |||
print | |||
print htmlHeader | |||
print """ | |||
<div class="container txt" style="margin-top:100px;"> | |||
Thanks, url submitted. | |||
<br /> | |||
<a href="showImage.cgi"><< Back to the image</a> | |||
</div> | |||
""" | |||
print """ | |||
</body> | |||
</html>""" | |||
</source> | |||
==Composing the Image== | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
#-*- coding:utf-8 -*- | |||
import cgi, re, urllib2, glob | |||
import cgitb; cgitb.enable() | |||
# reads submitted urls from the text file where they're stored, scraping each one and adding a corresponding pixel to the image. | |||
# prints bash commands - pipe to bash to run | |||
# open image to be edited | |||
print "convert ../../forbiddenPixels/images/00001.png \\" # opens the original, blank image. This means img is made fresh each time, so strings that have disappeared from the web will also disappear from the image. | |||
# open urls file | |||
f = open("data/urls.txt") | |||
# for each line in file, add a pixel | |||
for line in f: | |||
url = line | |||
if url == "\n": # skips over any blank entries in the text file. TODO: change to 'any invalid url' | |||
continue | |||
text = urllib2.urlopen(url).read() # reads page at the specified URL | |||
pat = r"Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\))" | |||
if not re.search(pat, text): # if pattern 'pat' isn't matched within 'text' | |||
print " " # TODO: Change to 'do nothing/skip' | |||
else: | |||
for m in re.finditer(pat, text): # for each match in text... | |||
#string = m.group(0) # m.group() lets you access data within the match object | |||
xPos = int(m.group(1)) # note: m.group(1) is first capture () - m.group(0) is always whole string | |||
yPos = int(m.group(2)) | |||
color = m.group(3) | |||
print "-fill ' " + color + " ' \\" # this part adds a pixel to the image | |||
print "-draw 'point %d,%d' \\" %(xPos,yPos) | |||
# count how many images are in the 'images' folder | |||
images = glob.glob('../../forbiddenPixels/images/*') | |||
number = len(images) # counts number of files in images folder | |||
latest = '%05d' % number # converts to 5 digits. note: this changes type from int to str | |||
#save new version of the image, adding 1 to previous filename | |||
newNumber = '%05d' % (number + 1) | |||
newImage = "../../forbiddenPixels/images/" + newNumber + ".png" # saves with new filename | |||
print newImage | |||
f.close() | |||
</source> |
Latest revision as of 11:52, 3 April 2012
Displaying Image
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi, glob
import cgitb; cgitb.enable()
#-------- ascertain latest image filename -------------#
images = glob.glob('../../forbiddenPixels/images/*')
number = len(images) # counts number of files in images folder
latest = '%05d' % number # converts to 5 digits. note: this changes type from int to str
#print number
#print latest
image = "../../forbiddenPixels/images/" + latest + ".png"
#-------- print html doc with image ------------------#
htmlHeader = """<!DOCTYPE html>
<html>
<head>
<title>A Censored Image</title>
<link <link rel="stylesheet" href="../../pixels.css">
</head>
<body>"""
print "Content-Type: text/html"
print
print htmlHeader
print """
<div class="container img">
<img src=" """ + image + """ " alt="Sorry, image not currently available" />
</div>
<div class="centred">
This image has been censored. Help to bring it back online, one pixel at a time:<br /><br /> <a href="inputForm.cgi"><button>adopt a pixel</button></a>
</div>
"""
print """
</body>
</html>"""
URL Input Form
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi, random
import cgitb; cgitb.enable()
# prints out the URL input form, giving the user a random pixel string (for testing).
htmlHeader = """<!DOCTYPE html>
<html>
<head>
<title>Adopt a Pixel</title>
<link <link rel="stylesheet" href="../../pixels.css">
</head>
<body>"""
print "Content-Type: text/html"
print
print htmlHeader
print """
<div class="centred" style="margin-top:50px;">
Below is the unique identifier for your pixel.
Copy and paste it somewhere public on the internet, exactly as it appears:
</div>
<div class="container txt">
<span class="pixelString">
Pixel position:""" + str(random.randint(100,500)) + "." + str(random.randint(100,300)) + "; Color:rgba(" + str(random.randint(100,255)) + "," + str(random.randint(100,255)) + "," + str(random.randint(100,255)) + """,1)
</span>
</div>
<div class="centred" style="margin-top:50px;">
Then tell us the URL of the page where you pasted it:
</div>
<div class="container txt">
<form action="saveUrl.cgi" name="inputForm">
<span class="containerText">Paste url: </span>
<input name="url">
<input type="submit" value="OK">
</form>
</div>
<div class="centred">
Your pixel will appear in the image for as long as the text remains at this URL.
</div>
</body>
</html>"""
Saving the URL
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi, os, subprocess
import cgitb; cgitb.enable()
# recieves url from inputForm.html and saves the submitted url as a new line in urls.txt
#------------- get URL from input form -------------------#
form = cgi.FieldStorage() # Grabs whatever input comes from form
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
#------------- save url to a text file --------------------#
f = open("data/urls.txt", 'a') # opens text file in write mode
f.write(url + '\n') # write url then newline
f.close()
#-----------run scrapeNdraw script and pipe to bash--------#
command = "./scrapeDraw.cgi | bash" # script called here must be executable!
os.popen(command, 'r').read()
#------------- print acknowledgement ----------------------#
htmlHeader = """<!DOCTYPE html>
<html>
<head>
<title>A form talking to a python script</title>
<link <link rel="stylesheet" href="../../pixels.css">
</head>
<body>"""
print "Content-Type: text/html"
print
print htmlHeader
print """
<div class="container txt" style="margin-top:100px;">
Thanks, url submitted.
<br />
<a href="showImage.cgi"><< Back to the image</a>
</div>
"""
print """
</body>
</html>"""
Composing the Image
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi, re, urllib2, glob
import cgitb; cgitb.enable()
# reads submitted urls from the text file where they're stored, scraping each one and adding a corresponding pixel to the image.
# prints bash commands - pipe to bash to run
# open image to be edited
print "convert ../../forbiddenPixels/images/00001.png \\" # opens the original, blank image. This means img is made fresh each time, so strings that have disappeared from the web will also disappear from the image.
# open urls file
f = open("data/urls.txt")
# for each line in file, add a pixel
for line in f:
url = line
if url == "\n": # skips over any blank entries in the text file. TODO: change to 'any invalid url'
continue
text = urllib2.urlopen(url).read() # reads page at the specified URL
pat = r"Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\))"
if not re.search(pat, text): # if pattern 'pat' isn't matched within 'text'
print " " # TODO: Change to 'do nothing/skip'
else:
for m in re.finditer(pat, text): # for each match in text...
#string = m.group(0) # m.group() lets you access data within the match object
xPos = int(m.group(1)) # note: m.group(1) is first capture () - m.group(0) is always whole string
yPos = int(m.group(2))
color = m.group(3)
print "-fill ' " + color + " ' \\" # this part adds a pixel to the image
print "-draw 'point %d,%d' \\" %(xPos,yPos)
# count how many images are in the 'images' folder
images = glob.glob('../../forbiddenPixels/images/*')
number = len(images) # counts number of files in images folder
latest = '%05d' % number # converts to 5 digits. note: this changes type from int to str
#save new version of the image, adding 1 to previous filename
newNumber = '%05d' % (number + 1)
newImage = "../../forbiddenPixels/images/" + newNumber + ".png" # saves with new filename
print newImage
f.close()