User:Andre Castro/prototyping/1.2/traces

From XPUB & Lens-Based wiki

Trance

About the project:



TO DO

  • Emails sent must be UTF-8


Links

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

>>>> http://pzwart3.wdka.hro.nl/~slorusso/traces/

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


database in: http://pzwart3.wdka.hro.nl/~acastro/cgi-bin/traces-database.xml


Part1 Form and email subscription into database

  • online form: where users subscribe their email address
  • cgi receives email address appends it within the today's date node in traces-database.xml


http://pzwart3.wdka.hro.nl/~acastro/cgi-bin/traces_form_handler.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('/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) + '"]'
	 
	#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)
		doc.write('/home/acastro/public_html/cgi-bin/traces-database.xml', xml_declaration=True,  encoding='utf-8') #write
	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

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

According the date decide what part of story will be told

WRITE MORE

#! /usr/bin/python
# coding: utf-8
import lxml.etree, re
import smtplib, email.utils
from email.mime.text import MIMEText
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('traces-text.txt', "r")
text = text_file.read() #CONTENT INTO VARIABLE !!!!

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


f = ('traces-database.xml') # ADD ABSOLUTE PATH
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(19):
	date = date.today()
	delta = timedelta(days= day)
	subtract = date - delta	
	dates.append(str(subtract))
 


#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 =  "\n\n			@		\n" + text_splited[i] + "\n			@		"		
			# EMAIL - (MAKE IT UTF-8)
			msg = MIMEText(email_body) #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 @ part#' + str((i+1))
			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()