#!/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 = """
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.