#!/usr/bin/env python

from sys import stdout
import urllib

fm4_ids = {}
txt_ids = {}
results = {}

stdout.write( "reading episodes.\n")
tf = open( 'episodes.in', 'r')
for line in tf:
    t = line.split( '=')
    txt_ids[t[0]] = '='.join( t[1:]).replace( '&', '&amp;')
tf.close()

stdout.write( "reading episode dates.\n")
i=0

ff = open( '../../fm4kalender/kalender-daten.txt')
for line in ff:
    t = line.split( '=')
    fm4_ids[t[0]] = int(t[1])
    i=i+1
ff.close()

k = txt_ids.keys()
k.sort()
k.reverse()

of = open( 'episodes.html', 'w')
for key in k:
    if txt_ids[key].strip() != '':
        # The URL scheme has changed at the beginning of 2009
        if int(fm4_ids[key]) < 1000000:
            url = 'http://fm4v2.orf.at/station/%s' % fm4_ids[key]
        else:
            url = 'http://fm4.orf.at/radio/stories/%s' % fm4_ids[key]
        of.write( '<tr>')
        of.write( '<td>%s</td>' % ( key, ))
        of.write('<td><a href="%s">%s</a></td>' % (url, txt_ids[key]))
        of.write( '</tr>'+"\n")
of.close()

stdout.write( 'alle sind sie geile tiere, nur von voegeln wird gequatscht\n.')


