def post_updates(res): """ Tweet all blog posts that are new since the last call """ updates = 0 print 'Parsing feeds...' for blog, data in res.iteritems(): current_etag = data.get('etag') current_modified = data.get('modified') current_owner = data.get('owner') current_url = data.get('url') latest_link = data.get('latest_link') # Conditional GET feed_data = feedparser.parse(current_url, etag=current_etag, modified=current_modified) print current_url logging.info(current_url) if feed_data.bozo: logging.info('Got malformed XML.') # A nonempty array means there's an update # (A few edge cases produce false positives...see comment below) if feed_data.entries: post = feed_data.entries[0] post_title = post.get('title') post_link = post.get('link') if post_link != latest_link: # The 2xx wasn't a false positive # Send the tweet tweetblog(post_title, current_owner, post_link) # Update the metadata new_etag = feed_data.get('etag') new_modified = feed_data.get('modified') data['latest_link'] = post_link if current_etag: data['etag'] = stripweak(new_etag) if current_modified: data['modified'] = new_modified updates += 1 else: logging.info('No new content.') else: logging.info('No new content.') if updates: fb.patch('blogs/', res, params=auth) print 'Posted %d update(s).' % updates logging.info('Posted %d update(s).' % updates)
def update_firebase(): """ Update the Firebase store from blogroll.yaml Add any blogs in the YAML file that aren't already in the Firebase store, along with its owner and the latest ETag or Last-Modified data from the feed. """ # Firebase instance and auth param fb = firebase.FirebaseApplication(FIREBASE_URL, None) auth = {'auth': FIREBASE_SECRET} # Current blogroll.yaml and FB store yamlpath = path.join(path.dirname(__file__), '..', 'blogroll.yaml') blogroll = loadblogs(yamlpath) fb_store = fb.get('blogs/', None, params=auth) if not fb_store: # Looking at a brand new Firebase store fb_store = {'blogs': None} updates = 0 print 'Migrating to database...' # Check the blogroll against Firebase for blog in blogroll: url = blog['feed_url'] clean_url = sanitize_url(url) if not clean_url in fb_store: feed_data = feedparser.parse(url) if feed_data.get( 'entries') and not feed_data.bozo: # Ignore malformed XML print 'Adding %s to the database.' % url updates += 1 modified = feed_data.get('modified') etag = feed_data.get('etag') normalized_tag = None if etag is not None: normalized_tag = stripweak(etag) fb_store[clean_url] = { 'owner': blog['owner'], 'modified': modified, 'etag': normalized_tag, 'url': url, 'latest_link': feed_data.entries[0].get('link') } # Actually push updates to Firebase if updates: fb.patch('blogs/', fb_store, params=auth) print 'Updated %d records.' % updates else: print "Nothing to update."
def update_firebase(): """ Update the Firebase store from blogroll.yaml Add any blogs in the YAML file that aren't already in the Firebase store, along with its owner and the latest ETag or Last-Modified data from the feed. """ # Firebase instance and auth param fb = firebase.FirebaseApplication(FIREBASE_URL, None) auth = {'auth': FIREBASE_SECRET} # Current blogroll.yaml and FB store yamlpath = path.join(path.dirname(__file__), '..', 'blogroll.yaml') blogroll = loadblogs(yamlpath) fb_store = fb.get('blogs/', None, params=auth) if not fb_store: # Looking at a brand new Firebase store fb_store = {'blogs': None} updates = 0 print 'Migrating to database...' # Check the blogroll against Firebase for blog in blogroll: url = blog['feed_url'] clean_url = sanitize_url(url) if not clean_url in fb_store: feed_data = feedparser.parse(url) if not feed_data.bozo: # Ignore malformed XML print 'Adding %s to the database.' % url updates += 1 modified = feed_data.get('modified') etag = feed_data.get('etag') normalized_tag = None if etag is not None: normalized_tag = stripweak(etag) fb_store[clean_url] = { 'owner': blog['owner'], 'modified': modified, 'etag': normalized_tag, 'url': url, 'latest_link': feed_data.entries[0].get('link') } # Actually push updates to Firebase if updates: fb.patch('blogs/', fb_store, params=auth) print 'Updated %d records.' % updates else: print "Nothing to update."