Various tweaks and improvements.

This commit is contained in:
Thomas Gideon 2010-09-25 18:26:55 -04:00
parent cfccf81d7a
commit 5ea8b05767
3 changed files with 37 additions and 17 deletions

View file

@ -8,9 +8,9 @@ import re
from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulSoup
import shutil import shutil
import time import time
import datetime
base_urls = { 'itunes' : 'http://traffic.libsyn.com/cmdln',
'other' : 'http://cmdln.evenflow.nl/mp3' }
def __fetch_feed(url): def __fetch_feed(url):
try: try:
@ -23,15 +23,18 @@ def __fetch_feed(url):
logging.debug('Network failure reason, %s.' % e.reason) logging.debug('Network failure reason, %s.' % e.reason)
return None return None
def __append(feed, suffix, append_fn, args=None):
def __append(feed, suffix, append_fn):
latest = __fetch_feed('cmdln_%s.xml' % suffix).entries[0] latest = __fetch_feed('cmdln_%s.xml' % suffix).entries[0]
entry = feed.entries[0] entry = feed.entries[0]
if latest.title.find(entry.title) != -1: if latest.title.find(entry.title) != -1:
logging.info('%s is up to date.' % suffix) logging.info('%s is up to date.' % suffix)
return return
base_url = 'http://www.archive.org/download/%s' % __archive_slug(entry.title)
filename = 'cmdln_%s.xml' % suffix filename = 'cmdln_%s.xml' % suffix
backup = 'cmdln_%s.xml.bak' % suffix today = datetime.date.today()
backup = '%s.%s' % (filename, today.strftime('%Y-%m-%d'))
shutil.copy(filename, backup) shutil.copy(filename, backup)
f = open(backup) f = open(backup)
o = open(filename, 'w') o = open(filename, 'w')
@ -40,7 +43,7 @@ def __append(feed, suffix, append_fn, args=None):
updated = time.strftime('%a, %d %b %Y %X +0000', feed.updated) updated = time.strftime('%a, %d %b %Y %X +0000', feed.updated)
for line in f: for line in f:
if line.find('<item>') != -1 and not firstItem: if line.find('<item>') != -1 and not firstItem:
append_fn(entry, o, suffix, args) append_fn(entry, o, suffix, base_url)
firstItem = True firstItem = True
if line.startswith(' <pubDate>'): if line.startswith(' <pubDate>'):
line = ' <pubDate>%s</pubDate>\n' % updated line = ' <pubDate>%s</pubDate>\n' % updated
@ -52,8 +55,8 @@ def __append(feed, suffix, append_fn, args=None):
o.close() o.close()
def __append_non_itunes(entry, output, suffix, args): def __append_non_itunes(entry, output, suffix, base_url):
(url, mime_type, size) = __enclosure(entry.enclosures, base_urls['other'], suffix) (url, mime_type, size) = __enclosure(entry.enclosures, base_url, suffix)
output.write(""" <item> output.write(""" <item>
<title>%(title)s (Comment Line 240-949-2638)</title> <title>%(title)s (Comment Line 240-949-2638)</title>
<link>%(link)s</link> <link>%(link)s</link>
@ -73,11 +76,13 @@ def __append_non_itunes(entry, output, suffix, args):
logging.info('Inserted new %s item.' % suffix) logging.info('Inserted new %s item.' % suffix)
def __append_itunes(entry, output, suffix, args): def __append_itunes(entry, output, suffix, base_url):
description = __description(entry.content) description = __description(entry.content)
soup = BeautifulSoup(description) soup = BeautifulSoup(description)
summary = '\n\n'.join([''.join(p.findAll(text=True)) for p in soup.findAll('p')]) summary = '\n\n'.join([''.join(p.findAll(text=True)) for p in soup.findAll('p')])
(url, mime_type, size) = __enclosure(entry.enclosures, base_urls['itunes'], suffix) (url, mime_type, size) = __enclosure(entry.enclosures, base_url, suffix)
if size == 0:
raise Exception('Couldn not find media, %s.' % base_url)
output.write(""" <item> output.write(""" <item>
<title>%(title)s (Comment Line 240-949-2638)</title> <title>%(title)s (Comment Line 240-949-2638)</title>
<link>%(link)s</link> <link>%(link)s</link>
@ -101,7 +106,7 @@ def __append_itunes(entry, output, suffix, args):
'size' : size, 'size' : size,
'subtitle' : ''.join(soup.contents[0].findAll(text = True)), 'subtitle' : ''.join(soup.contents[0].findAll(text = True)),
'summary' : summary, 'summary' : summary,
'duration' : args[1] }) 'duration' : entry.itunes_duration })
logging.info('Inserted new %s item.' % suffix) logging.info('Inserted new %s item.' % suffix)
@ -134,6 +139,16 @@ def __enclosure(enclosures, base_url, suffix):
return (url, mime_type, size) return (url, mime_type, size)
def __archive_slug(title):
slug = re.sub('\([^0-9]\)-\([^0-9]\)', '\1\2', title)
slug = re.sub('[^A-Za-z0-9\-]', ' ', slug)
slug = re.sub(' {2,}', ' ', slug)
tokens = slug.split(' ')
tokens = [t.capitalize() for t in tokens]
slug = ''.join(tokens)
return slug
def main(): def main():
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO,
format='%(message)s') format='%(message)s')
@ -142,12 +157,9 @@ def main():
logging.error('Failed to fetch feed.') logging.error('Failed to fetch feed.')
sys.exit(1) sys.exit(1)
if len(sys.argv) > 1:
base_urls['itunes'] = 'http://www.archive.org/download/%s' % sys.argv[2]
base_urls['other'] = 'http://www.archive.org/download/%s' % sys.argv[2]
__append(feed, 'mp3', __append_non_itunes) __append(feed, 'mp3', __append_non_itunes)
__append(feed, 'ogg', __append_non_itunes) __append(feed, 'ogg', __append_non_itunes)
__append(feed, 'm4a', __append_itunes, sys.argv) __append(feed, 'm4a', __append_itunes)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -72,7 +72,7 @@ lame -b 112 \
--tc "${comment}" \ --tc "${comment}" \
--tg "${genre}" \ --tg "${genre}" \
--id3v2-only \ --id3v2-only \
--noreplaygain i\ --noreplaygain \
cmdln.net_${date}.wav \ cmdln.net_${date}.wav \
cmdln.net_${date}.mp3 cmdln.net_${date}.mp3

View file

@ -5,13 +5,20 @@ import urllib2
from urllib2 import HTTPError, URLError from urllib2 import HTTPError, URLError
import os.path import os.path
import logging import logging
import shutil
import datetime
def __repoint(): def __repoint():
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO,
format='%(message)s') format='%(message)s')
f = open("cmdln_m4a.xml") today = datetime.date.today()
filename = 'cmdln_m4a.xml'
backup = '%s.%s' % (filename, today.strftime('%Y-%m-%d'))
shutil.copy(filename, backup)
f = open(backup)
o = open(filename, 'w')
try: try:
soup = BeautifulStoneSoup(f) soup = BeautifulStoneSoup(f)
enclosures = soup.findAll('enclosure') enclosures = soup.findAll('enclosure')
@ -29,9 +36,10 @@ def __repoint():
enclosure['url'] = rewritten enclosure['url'] = rewritten
enclosure['type'] = mime_type enclosure['type'] = mime_type
enclosure['length'] = length enclosure['length'] = length
print soup o.write(str(soup))
finally: finally:
f.close() f.close()
o.close()
def __archive_slug(title): def __archive_slug(title):