Wiki Change Emailer

Current code for the wiki change emailer minus the password. It is running as a cron job on ultra everyday at 23:59.

# Copyright (c) 2008 Adam Bark
 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
 
import sys
import urllib2
import smtplib
from datetime import date
from email.MIMEText import MIMEText
from BeautifulSoup import BeautifulStoneSoup
 
REMOVAL = "example@example.com"
PASSWORD = ""
 
def get_page(url):
    page = urllib2.urlopen(url)
    page = page.read()
    return page
 
def check_for_updates(last, page):
    page = BeautifulStoneSoup(page)
    newest = page.find("guid").contents[0]
    if newest == last:
        raise SystemExit
    else:
        open("update_check", "w").write(newest)
 
def process_page(page, last):
    page = BeautifulStoneSoup(page)
    data = page.findAll("guid")
    #print data
    updates = {}
    for change in data:
        if change.contents[0] == last:
            return updates
        name = change.findNextSibling("title").contents[0]
        name = name.split(""")[1]
        link = change.findNextSibling("link").contents[0]
        comments = change.findNextSibling("description").contents[0]
        comments = comments.split("Change comments: ")
        if len(comments) > 1:
            comments = comments[1].split("&lt")[0]
        else:
            comments = None
        #date = page.findAll("pubdate").contents[0]
        if name in updates:
            updates[name]["no. of changes"] += 1
            if comments:
                updates[name]["comments"].append(comments)
        elif comments:
            updates[name] = {"link": link,
                             "comments": [comments],
                             "no. of changes": 1}
        else:
            updates[name] = {"link": link,
                             "comments": [],
                             "no. of changes": 1}
    return updates
 
def generate_email(updates, to_addrs):
    text = ""
    comments = ""
    for update in updates:
        comments = ""
        for comment in updates[update]["comments"]:
            if comment:
                comment = comment.replace('&', '')
                comment = comment.replace('quot;', '"')
 
                comments += "        -%s\r\n" % comment
        string = "* %s  %s  number of changes: %d\r\n" % (
            update,
            updates[update]["link"],
            updates[update]["no. of changes"]) + comments + "\r\n"
        text += string
    msg = MIMEText(
"""Wiki changes for %s:\r\n\r\n""" % date.today().strftime("%A %d %B %Y") + text +\
"If you would like to be removed from this list please send an email to %s" % REMOVAL)
    msg["Subject"] = "Cubesat wiki changes for %s"\
                     % date.today().strftime("%A %d %B %Y")
    msg["From"] = "luvsatmail"
    msg["To"] = to_addrs
    return msg.as_string()
 
def send_email(email, to_addrs):
    print "addresses = ", to_addrs
    connection = smtplib.SMTP("smtp.googlemail.com", 587)
    connection.set_debuglevel(True)
    connection.ehlo()
    connection.starttls()
    connection.ehlo()
    connection.login("leicester.cubesat@gmail.com", PASSWORD)
    connection.sendmail("leicester.cubesat@gmail.com", to_addrs, email)
    connection.quit()
 
def main():
    print date.today().strftime("%A %d %B %Y")
    page = get_page("http://cubesat.wikidot.com/feed/site-changes.xml")
    last = open("update_check").read()
    check_for_updates(last, page)
    updates = process_page(page, last)
    addresses = open("addresses.txt", "r").read()
    msg = generate_email(updates, addresses)
    send_email(msg, addresses.split("; "))
 
if __name__ == "__main__":
    import os, sys
    os.chdir(os.path.split(sys.argv[0])[0])
    f = open("luvsat.log", "a")
    sys.stdout = sys.stderr = f
    main()
    f.close()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.