changeset 6:9f3eb9a07966

Add config parser. Add ability to print out an example config file. Add -f option to read a saved page.
author darius
date Wed, 29 Aug 2007 07:37:59 +0000
parents 275603a8e2ae
children bf896507faa9
files scrape-vb.py
diffstat 1 files changed, 40 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scrape-vb.py	Tue Aug 28 02:58:50 2007 +0000
+++ b/scrape-vb.py	Wed Aug 29 07:37:59 2007 +0000
@@ -6,7 +6,7 @@
 # Prints out (and emails) when criteria match based on cost,
 # destination, etc
 #
-# $Id: scrape-vb.py,v 1.4 2007/08/28 02:58:50 darius Exp $
+# $Id: scrape-vb.py,v 1.5 2007/08/29 07:37:59 darius Exp $
 ############################################################################
 #
 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
@@ -34,8 +34,37 @@
 #
 ############################################################################
 
-import os, re, BeautifulSoup, datetime, time, smtplib, sys, urllib, ConfigParser
+import os, re, BeautifulSoup, datetime, time, smtplib, sys, urllib
+import ConfigParser, optparse
+
+usage = '''%prog [options]
+Reads configuration from ./scrape-vb.ini and ~/.scrape-vb.ini'''
+
+optparse = optparse.OptionParser(usage, version="$Id: scrape-vb.py,v 1.5 2007/08/29 07:37:59 darius Exp $")
+optparse.add_option('-d', '--debug', action="store_true", default=False,
+                    help="Disable mail sending, prints mail message to stdout")
+optparse.add_option('-f', '--file', help="Do not fetch the page, use this file instead")
+optparse.add_option('-e', '--example', action="store_true", default=False,
+                    help="Print an example configuration file to stdout and exit")
+(options, args) = optparse.parse_args()
 
+if (options.example):
+    print '''[global]
+mailsubj="Subject line for emails"
+# The following 3 options are necessary before email will be sent
+mailfrom=user@host.com
+mailsend=True
+mailhost=mail.server.com
+
+[user@host.com]
+# All fields are optional
+city1=Foo
+city2=Bar
+when=dd/mm/yy
+maxcost=123
+'''
+    sys.exit(0)
+    
 parsetitle = re.compile('([a-z ]+) - ([a-z ]+) \$([0-9]+)', re.IGNORECASE)
 parsetper = re.compile('Travel Period: ([0-9]+/[0-9]+/[0-9]+) - ([0-9]+/[0-9]+/[0-9]+)', re.IGNORECASE)
 
@@ -50,8 +79,10 @@
 conf.read(conflist)
 
 try:
-    #f = open("vb-happyhour.html")
-    f = urllib.urlopen(conf.get('global', 'vburl'))
+    if (options.file != None):
+        f = open(options.file)
+    else:
+        f = urllib.urlopen(conf.get('global', 'vburl'))
 except IOError, e:
     print  "Unable to fetch page - " + str(e)
     sys.exit(1)
@@ -90,7 +121,7 @@
     for email in conf.sections():
         if (email == 'global'):
             continue
-
+        # Stuff configuration into a dictionary for our convenience
         t = {'email' : email}
         for i in conf.items(email):
             t[i[0]] = i[1]
@@ -128,6 +159,10 @@
     mailfrom = conf.get('global', 'mailfrom')
 except ConfigParser.NoOptionError:
     mailsend = False
+
+if (options.debug == True and mailsend):
+    print "mailsend overridden due to debugging"
+    mailsend = False
     
 if (mailsend):
     server = smtplib.SMTP(mailhost)