/
mtk-rss.py
executable file
·67 lines (60 loc) · 2.38 KB
/
mtk-rss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import re
import requests
import socket
from datetime import datetime
from dateutil import tz
from feedgen.feed import FeedGenerator
def add_episode(fg, match):
"""
Add an episode to the feed.
:param fg: FeedGenerator
:param match: re.match result.
"""
print(f"Episode {match['ep']}: {match['rel']}")
response = requests.get('https://fullduplexradio.us/' + match['rel'])
if response.status_code == 200:
description = []
file_url = None
rows = response.content.decode().split('\n')
for row in rows:
descr = re.match(r'<p .*>(?P<text>.*)</p>', row)
href = re.match(r'<a href="(?P<rel>[^"]*)', row)
if descr:
description.append(descr['text'])
elif href:
file_url = 'https://fullduplexradio.us' + href['rel'] if href['rel'].startswith('/') else href['rel']
break
if file_url:
fe = fg.add_entry()
fe.id(file_url)
pub_time = datetime.strptime(match['date'], '%Y-%m-%d')
local_tz = tz.tzlocal()
pub_time = pub_time.astimezone(local_tz)
fe.published(pub_time)
fe.title(f"Episode {match['ep']}: {match['title']}")
fe.description('\n'.join(description))
fe.enclosure(file_url, 0, 'audio/mpeg')
fdr = 'https://fullduplexradio.us'
fg = FeedGenerator()
fg.load_extension('podcast')
fg.podcast.itunes_category('Music', 'Podcasting')
fg.title('Full Duplex Radio')
fg.description("R&R play what they like, which is a lot. And they tell you about it.")
fg.link(link={'href': fdr})
myhost = socket.getfqdn("0.0.0.0")
# TODO: make this configurable
fg.image('https://foo:bar@{}:8080/FDR.jpg'.format(myhost), title='Full Duplex Radio', link=fdr)
local_tz = tz.tzlocal()
fg.lastBuildDate(datetime.now(tz=local_tz))
fg.rss_str(pretty=True)
response = requests.get(fdr)
if response.status_code == 200:
rows = response.content.decode().split('\n')
# '<a href="pl/FD406.html">Episode #406: Do You Know Any Nice Jewish Girls? (2020-11-07)</a>'
for row in rows:
match = re.match(r'<a href="(?P<rel>[^"]*)">Episode #(?P<ep>[0-9]+): (?P<title>.*) \((?P<date>.*)\)</a>', row)
if match:
add_episode(fg, match)
urls = ['https://fullduplexradio.us/audio/Full%20Duplex%20405%20-%202020-10-31.mp3',]
fg.rss_file('podcast.xml')