#!/usr/bin/python # -*- coding: UTF-8 -*- import BeautifulSoup import urllib import pickle import datetime from mod_python import util, apache import smtplib import MimeWriter, StringIO, base64 import os def get_soup(): f = open('twentyfour.xml') text = f.read() soup = BeautifulSoup.BeautifulSoup(text) f.close() return soup def fetch_images(): soup = get_soup() images = soup.findAll("images") url_base = "http://kahlo.bampfa.berkeley.edu/twentyfour/images/" my_base = "/Users/k7lim/src/python/burnbam/images/" print images for image in images: name = image['URL'] print urllib.urlretrieve(url_base + name, my_base + name) def fetch_thumbs(): soup = get_soup() images = soup.findAll("images") url_base = "http://kahlo.bampfa.berkeley.edu/twentyfour/images/" my_base = "/Users/k7lim/src/python/burnbam/images/" print images for image in images: name = image['thumb'] print urllib.urlretrieve(url_base + name, my_base + name) def pickle_artists(): artist_dict = {} soup = get_soup() artists = soup.findAll("artist") for artist in artists: name = artist['firstname'] if name and len(name.strip()) > 0: name = name + " " + artist['lastname'] else: name = artist['lastname'] artist_dict[name] = [] for x in range(24): hour_dict = {} hour_dict['mood'] = artist.contents[x+1]['mood'] hour_dict['img'] = artist.contents[x+1]['url'] artist_dict[name].append(hour_dict) output = open('/home/k7lim/src/python/burnbam/artist.pkl', 'wb') pickle.dump(artist_dict, output) output.close() def get_artists(): f = open('/home/k7lim/src/python/burnbam/artist.pkl', 'rb') artist_dict = pickle.load(f) for key in artist_dict.keys(): print key f.close() return artist_dict def is_phone_number(str): try: foo = int(str) return True except: return False def mail(serverURL=None, sender='', to='', subject='', text='', filepath=""): """ Usage: mail('somemailserver.com', 'me@example.com', 'someone@example.com', 'test', 'This is a test') """ message = StringIO.StringIO() writer = MimeWriter.MimeWriter(message) writer.addheader('Subject', subject) writer.startmultipartbody('mixed') # start off with a text/plain part part = writer.nextpart() body = part.startbody('text/plain') body.write(text) # now add an attachment part = writer.nextpart() part.addheader('Content-Transfer-Encoding', 'base64') body = part.startbody('image/jpeg; name=%s' % os.path.basename(filepath)) base64.encode(open(filepath, 'rb'), body) # finish off writer.lastpart() # send the mail smtp = smtplib.SMTP(serverURL) smtp.sendmail(sender, to, message.getvalue()) smtp.quit() def handler(req): output = """ 0024 Online Makes Fake Friends (BETA)

Cronfriend

Based on 24h00 by Valery Grancher

Using this service, one can explore the facets of mobile friendship, using the source material gathered by Grancher.
Fill in the information requested and you will receive a communique from a new, and (literally) digital friend.

Your first name (or whatever name your friends call you)
Your 10-digit mobile phone number (no dashes or space)
Your wireless carrier (Note: Cingular/ATT has some known issues)
Pick a friend

Click submit to get a message from your new friend (standard messaging rates apply)
""" form = util.FieldStorage(req, keep_blank_values=0) if(form.get("submit", None)): valid = True username = form.get("number", None) domain = form.get("carrier", None) artist = form.get("artist", None) nick = form.get("nick", "Friend") if not username or not is_phone_number(username): output += "
Please enter a valid phone number" valid = False if not domain: output += "
Please choose a carrier" valid = False if not artist: output += "
Please choose a friend" valid = False if valid: artist_nick = artist artist_space_loc = artist.find(" ") if artist_space_loc > 0: artist_nick = artist_nick[0:artist_space_loc] from_user = artist.replace(" ", "") fromaddr = from_user + "@secretdomainname.com", toaddr = username + "@" + domain artist_dict = get_artists() curr_hour = datetime.datetime.today().hour if curr_hour == 0: curr_hour = 24 mood = artist_dict[artist][curr_hour]['mood'] img = artist_dict[artist][curr_hour]['img'] subject = "Hi %s!" % nick text = "Hey %s! Thinking of you. Right now, well... there's no other way to say it: %s. I took this photo for you, kinda random but... yeah. -%s" % (nick, mood, artist_nick) attachment_path = 'home/k7lim/src/python/burnbam/images/' + img mail('localhost', fromaddr, toaddr, subject, text, attachment_path) output += "
Your friend will send you a message soon!
" output += """ """ req.content_type = 'text/html' req.write(output.encode('utf-8', 'ignore')) return apache.OK