#!/usr/bin/python

 

#

# See more here: http://captcynicalsarcastic.com/?p=50

#

 

import cgi

import urllib2

import httplib

import BeautifulSoup

 

 

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):

    def http_error_301(self, req, fp, code, msg, headers):

        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)

        result.status = code

        return result

 

    def http_error_302(self, req, fp, code, msg, headers):

        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)

        result.status = code

        return result

 

 

def MakeItSo():

 

  form = cgi.FieldStorage(keep_blank_values=0)

  args = form.keys()

 

  showurl  = form.getvalue('url')

  showname = showurl.strip().split('/')[-1:][0]

 

  if not showurl:

    print 'Content Type: text/html\n'

    print '<h1>No show variable passed in!</h1>'

    return

 

  page = urllib2.urlopen(showurl)

  page = ''.join(page.readlines())

 

  soup = BeautifulSoup.BeautifulSoup(page)

 

  try:

    m3upath  = soup.fetchText('VBR M3U')[0]

    m3upath  = dict(m3upath.parent.attrs)['href'].strip()

  except:

    print 'Content Type: text/plain\n'

    print 'Sorry, but I was unable to find an appropriate playlist link for this show.'

    print m3upath, '\n'

    m3upath  = soup.fetchText('VBR M3U')[0]

    print m3upath, '\n'

    m3upath  = dict(m3upath.parent.attrs)['href']

    print m3upath, '\n'

    return

 

  for td in soup('td','ttl'):

    if '%s_files.xml' % showname in str(td):

      break

 

  xmlpath  = dict(td.findNext('a').attrs)['href']

 

  try:

    if not 'http://www.archive.org/' in m3upath:

      m3upath = 'http://www.archive.org/%s' % m3upath

      m3upath.replace('//','/')

    request = urllib2.Request(m3upath)

    httplib.HTTPConnection.debuglevel = 1

    opener  = urllib2.build_opener(SmartRedirectHandler())

    f = opener.open(request)

    m3upage = urllib2.urlopen(m3upath)

  except:

    print 'Content Type: text/plain\n'

    print 'Sorry, but I was unable to find an appropriate playlist link for this show.'

    print m3upath, '\n'

    m3upath  = soup.fetchText('VBR M3U')[0]

    print m3upath, '\n'

    m3upath  = dict(m3upath.parent.attrs)['href']

    print m3upath, '\n'

    return

 

  m3upage = ''.join(m3upage.readlines())

 

  xmlpage = urllib2.urlopen('http://www.archive.org%s' % xmlpath)

  xmlpage = ''.join(xmlpage.readlines())

 

  xmlsoup = BeautifulSoup.BeautifulSoup(xmlpage)

 

  xml = ''

 

  for file in m3upage.strip().split('\n'):

    mp3name = file.strip().split('/')[-1:][0]

 

    for node in xmlsoup('file'):

      if dict(node.attrs)['name'] == mp3name:

        link     = file

        try:

          album  = node('album')[0].string

        except:

          album  = 'Missing Album Name'

        try:

          artist = node('creator')[0].string

        except:

          artist = 'Missing Artist Name'

        try:

          track  = node('track')[0].string

        except:

          track  = 'Missing Track Name'

        try:

          title  = node('title')[0].string

        except:

          title  = 'Missing Title Name'

        try:

          length = node('length')[0].string

        except:

          length = '0'

        try:

          size   = node('size')[0].string

        except:

          size   = '00:00'

 

        xml += '''

      <item>

        <title>%s</title>

        <link>%s</link>

        <description>%s</description>

        <enclosure url="%s" length="%s" type="audio/mp3" />

        <pubDate>Wed, 26 Jan 2011 05:00:00 GMT</pubDate>

        <guid>%s</guid>

        <itunes:duration>%s</itunes:duration>

        <itunes:author>%s</itunes:author>

        <itunes:explicit>no</itunes:explicit>

        <itunes:keywords>archive.irg</itunes:keywords>

        <itunes:subtitle>%s</itunes:subtitle>

        <itunes:summary>%s</itunes:summary>

      </item>'''  % (title, link, album, link, size, link, length, artist, title, album)

 

  xml += '''

    </channel>

  </rss>'''

 

  xml = '''<?xml version="1.0" encoding="UTF-8"?>

  <rss xmlns:content="http://purl.org/rss/1.0/modules/content/"

       xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"

       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

       xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"

       xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">

    <channel>

      <title>%s - %s</title>

      <link>%s</link>

      <description>%s - %s</description>

      <language>en-us</language>

      <managingEditor>cskaryd@gmail.com</managingEditor>

      <generator>Python tagMe</generator>

      <ttl>60</ttl>

      <itunes:owner>

        <itunes:email>cskaryd@gmail.com</itunes:email>

        <itunes:name>%s -%s</itunes:name>

      </itunes:owner>

      <itunes:image href="http://www.archive.org/images/logo.jpg?cnt=0" />

      <itunes:category text="Music" />

      <itunes:author>%s</itunes:author>

      <itunes:explicit>no</itunes:explicit>

      <itunes:keywords>archive.org</itunes:keywords>

      <itunes:subtitle>%s - %s</itunes:subtitle>

      <itunes:summary>%s - %s</itunes:summary>%s''' % (artist, album,

                                                       'http://www.archive.org%s' % m3upath,

                                                       artist, album, artist, album, artist,

                                                       artist, album, artist, album, xml)

 

  print 'Content Type: text/xml\n'

  print xml

 

 

MakeItSo()