-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_feeds.py
More file actions
50 lines (43 loc) · 1.51 KB
/
Copy pathmerge_feeds.py
File metadata and controls
50 lines (43 loc) · 1.51 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
import os
import xml.etree.ElementTree as ET
# Merge multiple RSS feeds into a single feed, ensuring unique items by guid
feeds_dir = "feeds"
output_file = "webservices.xml"
# Store unique items by guid
items_by_guid = {}
# Store channel metadata from the first file
channel_metadata = None
for filename in sorted(os.listdir(feeds_dir)):
if not filename.endswith(".xml"):
continue
path = os.path.join(feeds_dir, filename)
try:
tree = ET.parse(path)
root = tree.getroot()
channel = root.find("channel")
if channel is None:
continue
# Save channel metadata from the first file
if channel_metadata is None:
channel_metadata = [elem for elem in channel if elem.tag != "item"]
# Process items
for item in channel.findall("item"):
guid_elem = item.find("guid")
if guid_elem is not None:
guid = guid_elem.text
if guid not in items_by_guid:
items_by_guid[guid] = item
except Exception as e:
print(f"Error processing {filename}: {e}")
# Build the combined RSS feed
rss = ET.Element("rss", version="2.0")
channel = ET.SubElement(rss, "channel")
if channel_metadata:
for elem in channel_metadata:
channel.append(elem)
for item in items_by_guid.values():
channel.append(item)
# Write to output file
tree = ET.ElementTree(rss)
tree.write(output_file, encoding="utf-8", xml_declaration=True)
print(f"Combined RSS written to {output_file}")