#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Create RSS podcast file that lists all mp3 files in the directory
#
# Version: 0.1
# 
# Author: Arie Skliarouk <skliarie@gmail.com>
#

import os,sys

#TBD: extract audiobook title
#FeedTitle="The audiobook"

if len(sys.argv) != 2:
  print "abook2podcast: Create RSS podcast file that lists all mp3 files in a directory"
  print
  print "Usage: "
  print "   abook2podcast.py [directory name]"
  sys.exit(1)

DirName=sys.argv[1]

# Site URL without trailing slash
SiteUrl="http://sftpx.catchmedia.com"

#List all mp3 files in alphabetical order

#Create RSS file index.html in the target directory
f = open(DirName+'/index.html', 'w')

f.write("""<?xml version="1.0" encoding="utf-8"?>

<!-- generator="abook2podcast/0.1" -->

<rss version="2.0">
  <channel>
    <title>The audiobook title</title>
    <link>No link</link>
    <description>The audiobook description</description>
    <language>en</language>
    <copyright>Copyright???</copyright>
    <ttl>60</ttl>
""")

# Find all mp3 files
mp3_files=[]
for root, dirs, files in os.walk('.'):
  for name in files:
    if name[-4:]==".mp3":
      mp3_files.append(name)

mp3_files.sort()

for filename in mp3_files:
  f.write("    <item>\n");
  f.write("     <title>"+filename+"</title>\n")
  f.write("     <link>"+filename+"</link>\n")
  f.write("     <pubDate>Wed, 9 Jan 2008 10:00:00 EST</pubDate>\n")
  f.write("     <description>Here be title of the mp3 file</description>\n")
  FileUrl=SiteUrl+"/"+DirName+"/"+filename
  FileLength=os.path.getsize(DirName+"/"+filename)
  f.write('     <enclosure url="'+FileUrl+'" length="'+str(FileLength)+'" type="audio/mpeg" />\n')
  f.write("     <guid>"+FileUrl+"</guid>\n")
  f.write("     <author>TBD</author>\n")
  f.write("    </item>\n")

f.write("  </channel>\n")
f.write("</rss>\n")
f.close()

