User:Andre Castro/prototyping/1.2/traces

From XPUB & Lens-Based wiki

Links

https://docs.google.com/Doc?docid=dcwxmjc8_17pr7bbsgz original text

http://pzwart3.wdka.hro.nl/~acastro/form/email-form.html

http://pzwart3.wdka.hro.nl/~acastro/cgi-bin/email_form_handler.cgi

******part1******

Form and email / date appending email-database.xml

  • online form
  • cgi receives email address and appends it to email-database.xml file

http://pzwart3.wdka.hro.nl/~acastro/traces/email_form_handler-test.cgi

#!/usr/bin/python2.6
import cgi, cgitb, lxml.etree, datetime, os

today = datetime.datetime.now().strftime("%Y%m%d") #not sure is this is the best format

# Create instance of FieldStorage 
form = cgi.FieldStorage() 
# Get email from form 
email = form.getvalue('email') 


print "Content-Type: text/html"  
print                         
print "<TITLE>Form</TITLE>"
print "<H4>Your form was successfully added.<br />Shortly you will receive a message at %s </H4>" % (email)

#APPEND EMAIL AND DATE TO XML FILE
if os.path.exists('../traces/email-database.xml'):
	print  
else: 
	root = lxml.etree.Element('email-address')
	son = lxml.etree.SubElement(root, 'entries')
	tree = lxml.etree.ElementTree(root)
	tree.write('../traces/email-database.xml', pretty_print=True, xml_declaration=True, encoding='utf-8')

doc = lxml.etree.parse(database_url) 	#f = 'email-database.xml'

entries = doc.find('entries') # find root element

#append date SubElement date
date = lxml.etree.SubElement(entries, "date", attrib={'date':today} )  

#append grandchild
addrs = lxml.etree.SubElement(date, "address")
addrs.text = email

#write
doc.write('../traces/email-database.xml', xml_declaration=True,  encoding='utf-8') 
#print(lxml.etree.tostring(doc))
  • TO DO: date format not necessarily the best format - check others

email-database.xml:

<email-address>
  <entries>
		<date date="20120216">
			<address>test@test.org</address>
			<address>test2@test.org</address>
		</date>
  </entries>
</email-address>


******part2******

Days and Emails calculations

Other py script which:

  • parse the email-database.xml (every day)

Script which parses xml database. Checks the dates and email address under each dat3.

According the date decide what part of story will be told

#! /usr/bin/python
import lxml.etree, urllib2
#Script which parses xml database. Checks the dates and email address under each date



f = ('/home/andre/server-pzi/cgi-bin/traces-email-database.xml') # change to local
#f = urllib2.urlopen('http://pzwart3.wdka.hro.nl/~acastro/cgi-bin/traces-email-database.xml') #ERROR! ??permissions



doc = lxml.etree.parse(f)
print  lxml.etree.tostring(doc)

# for emails under the date 20120217 -> send PART#1

# find dates
dates = doc.xpath('//@date')

# for each date send one given part

date_is = doc.xpath("//date[@date='20120217']")

address = doc.xpath("//date[@date='20120217']/address/text()")


for nodes in doc.xpath("//date[@date='20120217']/address/text()"):
	print "Send email to " +  nodes
	#email then sent


  • check the dates
  • see what section of the text needs to send to whom

******part2******

Email the sections