User:Andre Castro/prototyping/1.2/traces: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Trance=
<b>About the project:</b>
Traces is a story written by Giulia Ciliberto,
told by email in short parts, one per day
==Links==
==Links==
https://docs.google.com/Doc?docid=dcwxmjc8_17pr7bbsgz original text


http://pzwart3.wdka.hro.nl/~slorusso/email-form.html
'''
Front-end:
http://pzwart3.wdka.hro.nl/~slorusso/traces/'''
 
Form handler:
/home/acastro/public_html/cgi-bin/traces_form_handler.cgi
 
 
Text:
/home/acastro/public_html/cgi-bin/traces-text.txt
 
Database:
/home/acastro/public_html/cgi-bin/traces-database.xml
 
 
==Part1 Form and email subscription into database==


http://pzwart3.wdka.hro.nl/~acastro/cgi-bin/traees_form_handler.cgi
Form handler:
/home/acastro/public_html/cgi-bin/traces_form_handler.cgi


==******part1******==
* users subscribe their email address in front-end
Form and email / date appending email-database.xml


* online form
* html page calls traces_form_handler.cgi


* cgi receives email address and appends it to email-database.xml file
* cgi receives email address appends it within the today's date node in traces-database.xml


TO DO: date format not necessarily the best format - check others


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


<source lang="python">
<source lang="python">
#!/usr/bin/python2.6
#!/usr/bin/python2.6
import cgi, cgitb, lxml.etree, datetime, os
import cgi, cgitb, lxml.etree, datetime, os
from datetime import *
date_now = datetime.now()
today = date_now.strftime("%Y-%m-%d")
date_delta = timedelta(days=22)
date_old = (date_now - date_delta).strftime("%Y-%m-%d")
print date_old


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


# Create instance of FieldStorage  
# Create instance of FieldStorage  
Line 29: Line 53:




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
#APPEND EMAIL AND DATE TO XML FILE
if os.path.exists('../traces/email-database.xml'):
 
print 
if os.path.exists('/home/acastro/public_html/cgi-bin/traces-database.xml'):
else:  
#open file
doc = lxml.etree.parse('/home/acastro/public_html/cgi-bin/traces-database.xml')
entries = doc.find('entries') # find root element
 
node_today = '//date[@date="'+ str(today) + '"]'
old_node = '//date[@date="'+ str(date_old ) + '"]'
 
#remove outdated addresses
for outdated in doc.xpath(old_node):
outdated.getparent().remove(outdated)# here I grab the parent of the element to call the remove directly on it
 
#check if todays date element is present
if today not in doc.xpath('//@date'):
date = lxml.etree.SubElement(entries, "date", attrib={'date':today} )  #append date SubElement date
addrs = lxml.etree.SubElement(date, "address")#append grandchild
addrs.text = str(email)
 
else:
test = doc.xpath(node_today)
addrs = lxml.etree.SubElement(test[0], "address")#append grandchild
addrs.text = email
 
doc.write('/home/acastro/public_html/cgi-bin/traces-database.xml',xml_declaration=True,encoding='utf-8') #write
 
 
 
else:
#Create a new file
root = lxml.etree.Element('email-address')
root = lxml.etree.Element('email-address')
son = lxml.etree.SubElement(root, 'entries')
entries = lxml.etree.SubElement(root, 'entries')
dates = lxml.etree.SubElement(entries, "date", attrib={'date':today} )  #append date
addrs = lxml.etree.SubElement(dates, "address")#append grandchild
addrs.text = str(email)
tree = lxml.etree.ElementTree(root)
tree = lxml.etree.ElementTree(root)
tree.write('../traces/email-database.xml', pretty_print=True, xml_declaration=True, encoding='utf-8')
tree.write('/home/acastro/public_html/cgi-bin/traces-database.xml', pretty_print=True, xml_declaration=True,encoding='utf-8')
tree.close()
 
</source>
 
 
==part2 and 3 Days calculations and sending emails==


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


entries = doc.find('entries') # find root element
/home/acastro/public_html/cgi-bin/traces_date_email.py


#append date SubElement date
Script which parses xml database.
date = lxml.etree.SubElement(entries, "date", attrib={'date':today} ) 
Checks the dates and email address under each date.
According the date decides what part of story will be send


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


#write
<source lang="python">
doc.write('../traces/email-database.xml', xml_declaration=True, encoding='utf-8')  
#! /usr/bin/python
#print(lxml.etree.tostring(doc))
# coding: utf-8
</source>
import lxml.etree, re
import smtplib, email.utils
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import *
 
# Script that:
# 1: parses xml database.  
# 2: Checks the dates and email address under each date
# 3: Sends the corresponding email part
 
#CRON IT FOR 1 x per Day
 
text_file = open('/home/acastro/public_html/cgi-bin/traces-text.txt', "r")
text = text_file.read() #CONTENT INTO VARIABLE !!!!
 
text_splited = text.split("\n\n@\n\n")
 
print text_splited[0]
print '##'
print text_splited[1]
print '##'
print len(text_splited)
 
f = ('/home/acastro/public_html/cgi-bin/traces-database.xml')
database = lxml.etree.parse(f)
dates = database.xpath('//date') # database find dates
#create the dictionary {date: [address, address]}
dateDict = {}
for date_subscription in dates:
   
date_is_attr = date_subscription.get("date")
myAddrs = database.xpath("//date[@date='{0}']/address/text()".format(date_is_attr))
dateDict[date_is_attr] = myAddrs
#print dateDict
 
 
#creat list of 19 day dates, today and before today
dates = []
for day in range(20):
date = date.today()
delta = timedelta(days= day)
subtract = date - delta
dates.append(str(subtract))
print dates




#checks the dates when emails were subscribe
#matchs them with the dates list
#sends the corresponding email part
for date in dateDict.keys():
print date
for i, value in enumerate(dates):


email-database.xml structure:
if str(date) == value:
email_addrs = dateDict[date] # list of receivers
print email_addrs
email_body =  '<html><body><div style="text-align: center; font-family: serif; font-size: 15px;"><br/><br/>@<br/><br/>' + text_splited[i] + '<br/><br/>@<br/><br/></div></body></html>'


<source lang="xml">
msg = MIMEMultipart('alternative') #Create Multipart msg (allows html)
msg['To'] = email.utils.formataddr(('Recipient', 'readers@traces.net'))
msg['From'] = email.utils.formataddr(('Traces', 'traces@noreply.net'))
msg['Subject'] = 'Traces @ part#' + str((i+1))


<email-address>
part_html = MIMEText(email_body, 'html')
  <entries>
msg.attach(part_html)
<date date="20120216">
<address>test@test.org</address>
<address>test2@test.org</address>
</date>
  </entries>
</email-address>


server = smtplib.SMTP('localhost')
server.set_debuglevel(False) # show communication with the server
try:
server.sendmail('traces@noreply.net', email_addrs, msg.as_string())
finally:
server.quit()
</source>
</source>






==******part2******==
Days and Emails calculations


Other py script which:


* parse the email-database.xml (every day)
==Examples on sending email ==


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
* <b>multipart email - allows imgs attachments</b>


<source lang="python">
<source lang="python">
#! /usr/bin/python
import smtplib, email.utils
import lxml.etree, urllib2
from email.mime.text import MIMEText
#Script which parses xml database. Checks the dates and email address under each date
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart


#Reference: http://docs.python.org/library/email-examples.html




f = ('/home/andre/server-pzi/cgi-bin/traces-email-database.xml') # change to local
# address list
#f = urllib2.urlopen('http://pzwart3.wdka.hro.nl/~acastro/cgi-bin/traces-email-database.xml') #ERROR! ??permissions
addr = ['andrecastro@c-e-m.org', 'andrecastro83@gmail.com']#, 'silviolorusso@gmail.com']


msg = MIMEMultipart()
#msg = MIMEText('This is the b@dy of the message.') #Create the message (simple ASCII)
msg['To'] = email.utils.formataddr(('Recipient', 'readers@traces.net'))
msg['From'] = email.utils.formataddr(('Traces', 'traces@noreply.net'))
msg['Subject'] = 'Traces Multipart - test'


msg.attach( MIMEText('This is the b@dy of the email') )


doc = lxml.etree.parse(f)
#attach a picture
print  lxml.etree.tostring(doc)
fp = open('animpnky_e0.gif', 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)


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


# find dates
server = smtplib.SMTP('localhost')
dates = doc.xpath('//@date')
server.set_debuglevel(False) # show communication with the server
try:
    server.sendmail('traces@noreply.net', addr, msg.as_string())
finally:
    server.quit()
</source>


# for each date send one given part


date_is = doc.xpath("//date[@date='20120217']")
* <b>text email - plain text</b>


address = doc.xpath("//date[@date='20120217']/address/text()")
<source lang="python">
import smtplib, email.utils
from email.mime.text import MIMEText


# address list
addr = ['andrecastro@c-e-m.org', 'andrecastro83@gmail.com']
#, 'silviolorusso@gmail.com']


for nodes in doc.xpath("//date[@date='20120217']/address/text()"):
msg = MIMEText('This is the b@dy of the message.') #Create the message (simple ASCII)
print "Send email to " +  nodes
msg['To'] = email.utils.formataddr(('Recipient', 'readers@traces.net'))
#email then sent
msg['From'] = email.utils.formataddr(('Traces', 'traces@noreply.net'))
msg['Subject'] = 'Traces plain - test '


server = smtplib.SMTP('localhost')
server.set_debuglevel(False) # show communication with the server
try:
    server.sendmail('traces@noreply.net', addr, msg.as_string())
finally:
    server.quit()


</source>
</source>
* check the dates
* see what section of the text needs to send to whom
==******part2******==
Email the sections

Latest revision as of 16:05, 29 March 2012

Trance

About the project: Traces is a story written by Giulia Ciliberto, told by email in short parts, one per day

Links

Front-end: http://pzwart3.wdka.hro.nl/~slorusso/traces/

Form handler: /home/acastro/public_html/cgi-bin/traces_form_handler.cgi


Text: /home/acastro/public_html/cgi-bin/traces-text.txt

Database: /home/acastro/public_html/cgi-bin/traces-database.xml


Part1 Form and email subscription into database

Form handler: /home/acastro/public_html/cgi-bin/traces_form_handler.cgi

  • users subscribe their email address in front-end
  • html page calls traces_form_handler.cgi
  • cgi receives email address appends it within the today's date node in traces-database.xml


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

date_now = datetime.now()
today = date_now.strftime("%Y-%m-%d") 
date_delta = timedelta(days=22)

date_old = (date_now - date_delta).strftime("%Y-%m-%d") 
print date_old


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



#APPEND EMAIL AND DATE TO XML FILE

if os.path.exists('/home/acastro/public_html/cgi-bin/traces-database.xml'):
	#open file
	doc = lxml.etree.parse('/home/acastro/public_html/cgi-bin/traces-database.xml')
	entries = doc.find('entries') # find root element

	node_today = '//date[@date="'+ str(today) + '"]'
	old_node = '//date[@date="'+ str(date_old ) + '"]'

	#remove outdated addresses
	for outdated in doc.xpath(old_node):
		outdated.getparent().remove(outdated)# here I grab the parent of the element to call the remove directly on it

	#check if todays date element is present
	if today not in doc.xpath('//@date'): 
		date = lxml.etree.SubElement(entries, "date", attrib={'date':today} )  	#append date SubElement date
		addrs = lxml.etree.SubElement(date, "address")#append grandchild
		addrs.text = str(email)

	else:
		test = doc.xpath(node_today)
		addrs = lxml.etree.SubElement(test[0], "address")#append grandchild
		addrs.text = email

	doc.write('/home/acastro/public_html/cgi-bin/traces-database.xml',xml_declaration=True,encoding='utf-8') #write



else:  
	#Create a new file
	root = lxml.etree.Element('email-address')
	entries = lxml.etree.SubElement(root, 'entries')
	dates = lxml.etree.SubElement(entries, "date", attrib={'date':today} )  	#append date
	addrs = lxml.etree.SubElement(dates, "address")#append grandchild
	addrs.text = str(email)
	tree = lxml.etree.ElementTree(root)
	tree.write('/home/acastro/public_html/cgi-bin/traces-database.xml', pretty_print=True, xml_declaration=True,encoding='utf-8')
	tree.close()


part2 and 3 Days calculations and sending emails

/home/acastro/public_html/cgi-bin/traces_date_email.py

Script which parses xml database. Checks the dates and email address under each date. According the date decides what part of story will be send


#! /usr/bin/python
# coding: utf-8
import lxml.etree, re
import smtplib, email.utils
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import *

# Script that:
# 1: parses xml database. 
# 2: Checks the dates and email address under each date 
# 3: Sends the corresponding email part

#CRON IT FOR 1 x per Day

 
text_file = open('/home/acastro/public_html/cgi-bin/traces-text.txt', "r")
text = text_file.read() #CONTENT INTO VARIABLE !!!!

text_splited = text.split("\n\n@\n\n")

print text_splited[0]
print '##'
print text_splited[1]
print '##'
print len(text_splited)

f = ('/home/acastro/public_html/cgi-bin/traces-database.xml') 
database = lxml.etree.parse(f)
 
dates = database.xpath('//date') # database find dates 
#create the dictionary {date: [address, address]}
dateDict = {}
for date_subscription in dates:
 
	date_is_attr = date_subscription.get("date")
	myAddrs = database.xpath("//date[@date='{0}']/address/text()".format(date_is_attr))
	dateDict[date_is_attr] = myAddrs
#print dateDict


#creat list of 19 day dates, today and before today 
dates = []
for day in range(20):
	date = date.today()
	delta = timedelta(days= day)
	subtract = date - delta	
	dates.append(str(subtract))
 
print dates


#checks the dates when emails were subscribe 
#matchs them with the dates list
#sends the corresponding email part
for date in dateDict.keys():
	print date
	for i, value in enumerate(dates):

		if str(date) == value:
			email_addrs = dateDict[date] # list of receivers
			print email_addrs
			
			email_body =  '<html><body><div style="text-align: center; font-family: serif; font-size: 15px;"><br/><br/>@<br/><br/>' + text_splited[i] + '<br/><br/>@<br/><br/></div></body></html>'		
		

			msg = MIMEMultipart('alternative') #Create Multipart msg (allows html)
			msg['To'] = email.utils.formataddr(('Recipient', 'readers@traces.net'))
			msg['From'] = email.utils.formataddr(('Traces', 'traces@noreply.net'))
			msg['Subject'] = 'Traces @ part#' + str((i+1))

			part_html = MIMEText(email_body, 'html')
			msg.attach(part_html)

			server = smtplib.SMTP('localhost')
			server.set_debuglevel(False) # show communication with the server
			try:
				server.sendmail('traces@noreply.net', email_addrs, msg.as_string())
			finally:
				server.quit()




Examples on sending email

  • multipart email - allows imgs attachments
import smtplib, email.utils
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

#Reference: http://docs.python.org/library/email-examples.html


# address list
addr = ['andrecastro@c-e-m.org', 'andrecastro83@gmail.com']#, 'silviolorusso@gmail.com']

msg = MIMEMultipart()
#msg = MIMEText('This is the b@dy of the message.') #Create the message (simple ASCII)
msg['To'] = email.utils.formataddr(('Recipient', 'readers@traces.net'))
msg['From'] = email.utils.formataddr(('Traces', 'traces@noreply.net'))
msg['Subject'] = 'Traces Multipart - test'

msg.attach( MIMEText('This is the b@dy of the email') )

#attach a picture
fp = open('animpnky_e0.gif', 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)


server = smtplib.SMTP('localhost')
server.set_debuglevel(False) # show communication with the server
try:
    server.sendmail('traces@noreply.net', addr, msg.as_string())
finally:
    server.quit()


  • text email - plain text
import smtplib, email.utils
from email.mime.text import MIMEText

# address list
addr = ['andrecastro@c-e-m.org', 'andrecastro83@gmail.com']
#, 'silviolorusso@gmail.com']

msg = MIMEText('This is the b@dy of the message.') #Create the message (simple ASCII)
msg['To'] = email.utils.formataddr(('Recipient', 'readers@traces.net'))
msg['From'] = email.utils.formataddr(('Traces', 'traces@noreply.net'))
msg['Subject'] = 'Traces plain - test '


server = smtplib.SMTP('localhost')
server.set_debuglevel(False) # show communication with the server
try:
    server.sendmail('traces@noreply.net', addr, msg.as_string())
finally:
    server.quit()