-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_feeds.py
More file actions
217 lines (183 loc) · 7.66 KB
/
Copy pathgenerate_feeds.py
File metadata and controls
217 lines (183 loc) · 7.66 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import json
import xml.etree.ElementTree as ET
from email.utils import formatdate
from datetime import datetime
import argparse
import re
def slugify(text):
return re.sub(r"[\W_]+", "-", text.strip().lower())
def load_token_map(path):
"""
Load an nginx-formatted map file like:
abc123 Podcast_Name_One;
xyz456 Podcast_Name_Two;
"""
token_map = {}
with open(path) as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and ";" in line:
token, folder = line.rstrip(";").split(None, 1)
token_map[token] = folder
return token_map
def generate_rss_for_podcast(
podcast_dir: str, podcast_title: str, base_url: str, token: str = None
):
base_url = base_url.rstrip("/")
podcast_basename = os.path.basename(podcast_dir)
url_prefix = (
f"{base_url}/secure/{token}" if token else f"{base_url}/pods/{podcast_basename}"
)
# Add iTunes and media namespaces
rss = ET.Element(
"rss",
version="2.0",
attrib={
"xmlns:itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd",
"xmlns:media": "http://search.yahoo.com/mrss/",
},
)
channel = ET.SubElement(rss, "channel")
ET.SubElement(channel, "title").text = podcast_title
ET.SubElement(channel, "link").text = f"{url_prefix}"
ET.SubElement(channel, "description").text = f"Archived feed for {podcast_title}"
ET.SubElement(channel, "language").text = "en"
ET.SubElement(channel, "itunes:summary").text = f"Archived feed for {podcast_title}"
ET.SubElement(channel, "itunes:author").text = "petit-pois"
ET.SubElement(channel, "itunes:explicit").text = "no"
owner = ET.SubElement(channel, "itunes:owner")
ET.SubElement(owner, "itunes:name").text = "petit-pois"
ET.SubElement(owner, "itunes:email").text = "noreply@example.com"
# Podcast-level cover image (itunes, standard <image>, and media:thumbnail)
image_url = None
for ext in [".jpg", ".jpeg", ".png", ".webp"]:
image_path = os.path.join(podcast_dir, f"cover{ext}")
if os.path.exists(image_path):
image_url = f"{url_prefix}/cover{ext}"
ET.SubElement(channel, "itunes:image", href=image_url)
# RSS 2.0 <image>
image_tag = ET.SubElement(channel, "image")
ET.SubElement(image_tag, "url").text = image_url
ET.SubElement(image_tag, "title").text = podcast_title
ET.SubElement(image_tag, "link").text = url_prefix
# media:thumbnail
ET.SubElement(channel, "media:thumbnail", url=image_url)
break
episodes = sorted(
[f for f in os.listdir(podcast_dir) if f.endswith(".json")], reverse=True
)
for json_file in episodes:
json_path = os.path.join(podcast_dir, json_file)
with open(json_path) as f:
entry = json.load(f)
base_name = os.path.splitext(json_file)[0]
audio_file = entry.get("filename") or next(
(
f
for f in os.listdir(podcast_dir)
if f.startswith(base_name) and not f.endswith(".json")
),
None,
)
if not audio_file:
continue
title = entry.get("title", "Untitled Episode")
pub_date = entry.get("published_parsed")
pub_date_str = (
formatdate(datetime(*pub_date[:6]).timestamp())
if pub_date
else formatdate()
)
description = entry.get("summary") or entry.get("content", [{}])[0].get(
"value", ""
)
subtitle = entry.get("subtitle", "")
duration = entry.get("itunes_duration", "")
itunes_title = entry.get("itunes_title", title)
guid = entry.get("id", audio_file)
author = entry.get("author", "")
audio_path = os.path.join(podcast_dir, audio_file)
file_size = os.path.getsize(audio_path) if os.path.exists(audio_path) else 0
if not file_size:
file_size = int(entry.get("links", [{}])[0].get("length", 0))
audio_url = f"{url_prefix}/{audio_file}"
item = ET.SubElement(channel, "item")
ET.SubElement(item, "title").text = title
ET.SubElement(item, "link").text = audio_url
ET.SubElement(
item, "enclosure", url=audio_url, type="audio/mpeg", length=str(file_size)
)
ET.SubElement(item, "pubDate").text = pub_date_str
ET.SubElement(item, "guid").text = guid
if description:
ET.SubElement(item, "description").text = description
if author:
ET.SubElement(item, "author").text = author
if subtitle:
ET.SubElement(item, "itunes:subtitle").text = subtitle
if itunes_title:
ET.SubElement(item, "itunes:title").text = itunes_title
if duration:
ET.SubElement(item, "itunes:duration").text = str(duration)
# Episode-level image
image_filename = entry.get("image_filename")
if image_filename:
episode_img_url = f"{url_prefix}/{image_filename}"
ET.SubElement(item, "itunes:image", href=episode_img_url)
ET.SubElement(item, "media:thumbnail", url=episode_img_url)
rss_path = os.path.join(podcast_dir, "archive.xml")
tree = ET.ElementTree(rss)
tree.write(rss_path, encoding="utf-8", xml_declaration=True)
# With the nginx config, only /secure/<token>/<filename> is reachable.
# If there's no token, the feed won't be accessible over HTTP.
url_prefix = f"{base_url}/secure/{token}" if token else None
feed_url = f"{url_prefix}/archive.xml"
print(f"✅ Generated RSS: {rss_path}")
print("token", token)
return feed_url
def generate_all_feeds(archive_root: str, base_url: str, map_file: os.PathLike = None):
if map_file is None:
print("⚠️ No token map file provided. RSS feeds will not be obfuscated.")
token_map = load_token_map(map_file) if map_file else {}
print("token_map", token_map, map_file)
results = []
for dir_name in sorted(os.listdir(archive_root)):
dir_path = os.path.join(archive_root, dir_name)
if not os.path.isdir(dir_path):
continue
token = next((t for t, folder in token_map.items() if folder == dir_name), None)
title = dir_name.replace("_", " ")
feed_url = generate_rss_for_podcast(dir_path, title, base_url, token)
if feed_url:
results.append((title, feed_url))
# ---- Print table at the end ----
if results:
title_width = max(len(r[0]) for r in results)
url_width = max(len(r[1]) for r in results)
print("\n" + "─" * (title_width + url_width + 5))
print("Generated Podcast Feeds")
print("─" * (title_width + url_width + 5))
print(f"{'Podcast'.ljust(title_width)} {'Feed URL'.ljust(url_width)}")
print(f"{'-' * title_width} {'-' * url_width}")
for title, url in results:
print(f"{title.ljust(title_width)} {url}")
print()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate RSS feeds from archived podcast metadata."
)
parser.add_argument(
"--archive_dir",
default="pods",
help="Root directory where podcast archives are stored.",
)
parser.add_argument(
"--base_url", required=True, help="Public base URL for serving audio files."
)
parser.add_argument(
"--map_file",
help="Optional path to token map (nginx-formatted map). Enables secureish token-based URLs.",
)
args = parser.parse_args()
generate_all_feeds(args.archive_dir, args.base_url, args.map_file)