Commits

Jeremy Sandell committed 79574f1

cleaned it up a bit, added some error handling and logging.

Comments (0)

Files changed (1)

 from urllib2 import Request, urlopen, URLError, HTTPError
 from BeautifulSoup import BeautifulSoup
 from collections import MutableSet
-from twitter import Api
+from twitter import Api, TwitterError
+from logging import getLogger, basicConfig, DEBUG, INFO
+from os.path import dirname, abspath, basename, join, splitext, exists
 
 KEY, PREV, NEXT = range(3)
 
 access_token_secret = None
 
 
-
 class OrderedSet(MutableSet):
     """
     Set that remembers original insertion order. Via the ASPN recipe [1], which
     def __del__(self):
         self.clear()                    # remove circular references
 
-            
 
-def get_kub_status():
-    url = 'http://www.kgis.org/website/kub_outage/GetOutageDate.asp'
-    request = Request(url)
-    result = [None, None, None]
 
-    try:
-        response = urlopen(request)
 
-    except HTTPError, e:
-        print dir(e)
-        result[0] = e
-        return result
+class TweetKUB(object):
+    def __init__(self, url=None, start_tag=None, end_tag=None):
+        self.start_tag = start_tag if start_tag else '#kub status'
+        self.end_tag = end_tag if end_tag else '#knoxpoweroutage'
+        self.url = url if url else 'http://www.kgis.org/website/kub_outage/GetOutageDate.asp'
+        self.log = getLogger(self.__class__.__name__)
 
-    else:
-        page = response.read()
-        response.close()
-        soup = BeautifulSoup(page)
-        main_table = soup.find('table', attrs={'id': 'Tableprime'})
-        outages = main_table.find('table')
 
-        header = outages.find('th', attrs={'colspan': '2'})
+    def _get_kub_status(self):
+        request = Request(self.url)
+        result = [None, None, None]
 
-        # Their page has duplicate information in it. We're using an ordered
-        # set to weed out the dupes.
+        try:
+            response = urlopen(request)
 
-        result = OrderedSet()
+        except HTTPError, e:
+            self.log.debug('Error scraping KGIS: %s', e)
 
-        for tr in main_table.findAll('tr'):
-            for td in tr.findChildren('td'):
+        else:
+            page = response.read()
+            response.close()
+            soup = BeautifulSoup(page)
+            main_table = soup.find('table', attrs={'id': 'Tableprime'})
+            outages = main_table.find('table')
 
-                tags = td.findChildren('b')
+            header = outages.find('th', attrs={'colspan': '2'})
 
-                for tag in tags:
-                    result.add(tag.text)
+            # Their page has duplicate information in it. We're using an ordered
+            # set to weed out the dupes.
 
-        return tuple(result)
+            result = OrderedSet()
+
+            for tr in main_table.findAll('tr'):
+                for td in tr.findChildren('td'):
+
+                    tags = td.findChildren('b')
+
+                    for tag in tags:
+                        result.add(tag.text)
+
+            return tuple(result)
+
+        return False
     
 
+    def emit(self):
+        status = self._get_kub_status()
+
+        if not status:
+            return
+
+        total = len(status) - 1
+
+        message = []
+
+        if self.start_tag and isinstance(self.start_tag, basestring):
+            message.append(self.start_tag)
+
+        for i, stat in enumerate(status):
+            
+            if i == total:
+                break
+                
+            message.append(stat)
+        
+        if self.end_tag and isinstance(self.end_tag, basestring):
+            message.append(self.end_tag)
+
+        tweet = ' '.join(message)
+
+        self.log.debug('registering API...')
+
+        twitter = Api(consumer_key=consumer_key,
+                      consumer_secret=consumer_secret,
+                      access_token_key=access_token_key,
+                      access_token_secret=access_token_secret)
+
+        try:
+            tweet_result = twitter.PostUpdate(tweet)
+        except TwitterError, e:
+            self.log.warning('Error posting status: %s', e)
+
+        else:
+            self.log.debug('Result was: %s', tweet_result)
+
 if __name__ == '__main__':
+    
+    filename = basename(abspath(__file__))
+    current_dir = dirname(abspath(__file__))
+    logfile = '.'.join((splitext(filename)[0], 'log'))
 
-    status = get_kub_status()
-    total = len(status) - 1
+    basicConfig(
+        level=DEBUG, 
+        format = '%(levelname)s %(asctime)s %(name)s  %(message)s',
+        filename = join(current_dir, logfile)
+    )
 
-    message = []
-    message.append('#kub status')
-
-    for i, stat in enumerate(status):
-        
-        if i == total:
-            break
-            
-        message.append(stat)
-    
-    message.append('#knoxpoweroutage')
-
-    tweet = ' '.join(message)
-    twitter = Api(consumer_key=consumer_key,
-                  consumer_secret=consumer_secret,
-                  access_token_key=access_token_key,
-                  access_token_secret=access_token_secret)
-
-    twitter.PostUpdate(tweet)
+    status = TweetKUB()
+    status.emit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.