def parse_feed(url): if is_empty(url): return None # fix url for use by feedparser clean_url = clean_feed_url(url) # see if it's a file url, 'cause parse feeder doesn't handle them # correctly is_file_url = clean_url.lower().startswith('file://') # Try to retrieve from cache using cleaned url. # In case of file url, don't bother with the cache, just re-read # each time parsed_feed = (None if is_file_url else cache.get(clean_url)) # if we got one, make a etags or modified request to see if any updates if parsed_feed is not None: updated_feed = None if parsed_feed.has_key('etag'): updated_feed = feedparser.parse(clean_url, etag=parsed_feed.etag) elif parsed_feed.has_key('modified'): updated_feed = \ feedparser.parse(clean_url, modified=parsed_feed.modified) # did we get any changes? if updated_feed is not None and updated_feed.status == 200: parsed_feed = updated_feed cache.set(clean_url, parsed_feed) else: # no cached one, go get it, handling files for # dumb ass feedparser to_parse = clean_url if is_file_url: with open(clean_url[7:]) as f: to_parse = f.read() parsed_feed = feedparser.parse(to_parse) if len(parsed_feed.feed) > 0: cache.set(clean_url, parsed_feed) elif cache.has_key(clean_url): del cache[clean_url] return parsed_feed
def render(self, context): """ Expects context to hold: - rss_img_feed_url - rss_img_feed_max_entries (optional) Adds a dictionary named 'rss_img_feed' to the context of form: { feed: { title: 'some title', url: 'http://feedurl...' } images: [ { type: 'image/jpeg', # mime type url: 'http://etc...', caption: 'some caption' }, ... ] } """ # pull vars url = None if context.has_key('rss_img_feed_url'): url = context['rss_img_feed_url'] if is_empty(url): url = None max_ = None if context.has_key('rss_img_feed_max_entries'): max_ = int(context['rss_img_feed_max_entries']) # parse_feed does all the work context['rss_img_feed'] = ( parse_img_feed(url, max_) if \ not url is None else \ { 'feed': {'title': '', 'url': ''}, 'images': []} ) return ''
def testEmpty(self): self.assertTrue(all(utils.is_empty(obj) for obj in self.empty_list)) for obj in self.not_empty_list: self.assertFalse(utils.is_empty(obj))
def __unicode__(self): return ( 'No Name' if is_empty(self.name) else self.name)