comparison backend/AbstractFeedUpdater.py @ 166:04c3b9796b89

feedparser uses the proxy now if one is configured. To implement this the FeedUpdater had to change a bit - sqlalchemy backend is not yet refactored.
author dirk
date Sat, 03 Sep 2011 04:12:35 +0200
parents 86f828096aaf
children a3c945ce434c
comparison
equal deleted inserted replaced
165:a5bda54309ba 166:04c3b9796b89
1 1
2 from datetime import datetime 2 from datetime import datetime
3 import feedparser 3 import feedparser
4 import logging 4 import logging
5 from urllib2 import ProxyHandler
5 6
6 STATUS_ERROR = 400 7 STATUS_ERROR = 400
7 log = logging.getLogger("FeedUpdater") 8 log = logging.getLogger("FeedUpdater")
8 9
9 class AbstractFeedUpdater(object): 10 class AbstractFeedUpdater(object):
10 ''' 11 '''
11 Abstract base class for FeedUpdater implementations - handles all the parsing of the feed. 12 Abstract base class for FeedUpdater implementations - handles all the parsing of the feed.
12 Subclasses need to implement creating and storing the new feed entries. 13 Subclasses need to implement creating and storing the new feed entries.
13 ''' 14 '''
14 15
15 def __init__(self, feed): 16 @staticmethod
17 def parseFeed(url):
18 proxy = ProxyHandler( {"http":"http://your.proxy.here:8080/"} )
19 return feedparser.parse(url, handlers = [proxy])
20
21 def __init__(self, preferences):
22 self.preferences = preferences
23
24 def update(self, feed):
16 self.feed = feed 25 self.feed = feed
17 26 log.info("updating " + feed.rss_url)
18 def update(self, feedDict=None): 27 result = self._retrieveFeed()
19 log.info("updating " + self.feed.rss_url)
20 if feedDict is None:
21 result = self._retrieveFeed()
22 else:
23 result = feedDict
24 self._processEntries(result) 28 self._processEntries(result)
29 self._setFeedTitle(result)
25 30
26 def _retrieveFeed(self): 31 def _retrieveFeed(self):
27 result = feedparser.parse(self.feed.rss_url) 32 if self.preferences.isProxyConfigured():
33 proxyUrl = "http://%s:%i" % (self.preferences.proxyHost(), self.preferences.proxyPort())
34 proxyHandler = ProxyHandler({"http" : proxyUrl})
35 result = feedparser.parse(self.feed.rss_url, handlers=[proxyHandler])
36 else:
37 result = feedparser.parse(self.feed.rss_url)
28 # bozo flags if a feed is well-formed. 38 # bozo flags if a feed is well-formed.
29 # if result["bozo"] > 0: 39 # if result["bozo"] > 0:
30 # raise FeedUpdateException() 40 # raise FeedUpdateException()
31 status = result["status"] 41 status = result["status"]
32 if status >= STATUS_ERROR: 42 if status >= STATUS_ERROR:
56 raise Exception("_processEntry is abstract, subclasses must override") 66 raise Exception("_processEntry is abstract, subclasses must override")
57 67
58 def _incrementFeedUpdateDate(self): 68 def _incrementFeedUpdateDate(self):
59 raise Exception("_incrementNextUpdateDate is abstract, subclasses must override") 69 raise Exception("_incrementNextUpdateDate is abstract, subclasses must override")
60 70
71 def _setFeedTitle(self, feedDict):
72 if self.feed.title is None:
73 if feedDict.feed.has_key("title"):
74 self.feed.title = feedDict.feed.title
75 else:
76 self.feed.title = self.feed.rss_url
77
61 78
62 class FeedUpdateException(Exception): 79 class FeedUpdateException(Exception):
63 pass 80 pass