import sys import feedparser import urllib2 from urllib2 import HTTPError, URLError import logging import re def __fetch_feed(url): try: feed = feedparser.parse(url) return feed.entries[0] except HTTPError, e: logging.error('Failed with HTTP status code %d' % e.code) return None except URLError, e: logging.error('Failed to connect with network.') logging.debug('Network failure reason, %s.' % e.reason) return None def __append(entry, suffix, append_fn): latest = __fetch_feed('cmdln_%s.xml' % suffix) if entry.title.find(latest.title) != -1: logging.info('Up to date.') return f = open('cmdln_%s.xml' % suffix) o = open('cmdln_%s_out.xml' % suffix, 'w') first = False try: for line in f: if line.find('') != -1 and not first: append_fn(entry, o) first = True o.write(line) finally: f.close() def __append_mp3(entry, output): (url, mime_type, size) = __enclosure(entry.enclosures, 'http://cmdln.evenflow.nl/mp3', 'mp3') output.write(""" %(title)s (Comment Line 240-949-2638) %(link)s %(pubDate)s %(permalink)s """ % { 'title': entry.title, 'link': entry.link, 'description': __description(entry.content), 'pubDate' : entry.date, 'permalink' : __permalink(entry.title), 'url' : url, 'mime_type' : mime_type, 'size' : size }) logging.info('Insert new MP3 item.') def __permalink(title): permalink = title.lower() permalink = re.sub('-', '', permalink) permalink = re.sub('[^a-z0-9]', '-', permalink) permalink = re.sub('-{2,}', '-', permalink) if len(permalink) > 48: permalink = permalink[:48] return permalink def __description(content): description = content[0].value description = re.sub('

\n', '', description) description = re.sub(re.compile('License.

.*$', re.M | re.S), 'License.

', description) description = re.sub('

\n', '

\n\n', description) return re.sub('

View the More news, commentary, and alternate feeds available at http://thecommandline.net/. View the