#!/usr/bin/python
from datetime import datetime
from typing import List
import sys
import os
import subprocess
from string import Template
header='''
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>GotMead generated archive</title>
<language>en-us</language>
<lastBuildDate>Sat, 25 Mar 2006 11:30:00 -0500</lastBuildDate>
<pubDate>Sat, 25 Mar 2006 11:30:00 -0500</pubDate>
'''
item = Template('''<item>
<title>$title</title>
<guid>$mp3</guid>
<description>$description</description>
<enclosure url="$mp3" type="audio/mpeg"/>
<!--<enclosure url="XXXXXX" length="11779397" type="audio/mpeg"/>-->
<category>Podcasts</category>
<pubDate>$date</pubDate>
</item>
''')
footer='''
</channel>
</rss>'''
command: str = """grep -oE 'https://api\.spreaker\.com/download/episode/[^"]+' --no-filename pages/*/*"""
mp3s: List[str] = subprocess.check_output([command], shell=True).decode().splitlines()
command: str = """grep -oE '<meta property="og:title" content="[^"]+' --no-filename pages/*/* | sed 's/.*"//'"""
titles: List[str] = subprocess.check_output([command], shell=True).decode().splitlines()
command: str = """grep -oE '<meta property="og:description" content="[^"]+' --no-filename pages/*/* | sed 's/.*"//'"""
descriptions: List[str] = subprocess.check_output([command], shell=True).decode().splitlines()
command: str = """grep -m 1 -oE 'timestamp="[^"]+' --no-filename pages/*/* | sed 's/.*"//'"""
timestamps: List[str] = subprocess.check_output([command], shell=True).decode().splitlines()
command: str = """grep -oE 'class="track_player_time_total">[0-9]+:[0-9]+' --no-filename pages/*/* | sed 's/.*>//'"""
durations: List[str] = subprocess.check_output([command], shell=True).decode().splitlines()
print(header);
for i in range(len(titles)):
# Skip this URL if it is coming again on a later page:
# (This doesn't actually seem to help.)
if (mp3s[i] in mp3s[i+1:]):
pass
try:
date = datetime.fromtimestamp(int(timestamps[i])) # if the timestamp is a unix timestamp
except ValueError: # if it's an iso date
date = datetime.fromisoformat(timestamps[i])
# Sat, 25 Mar 2006 11:30:00 -0500
dateStr: str = date.strftime('%a, %d %b %Y %X %z')
print(item.substitute(title=titles[i], description=descriptions[i], mp3=mp3s[i], date=dateStr));
print(footer);