-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_podcast_feed.py
59 lines (45 loc) · 2.04 KB
/
csv_to_podcast_feed.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
import argparse
import pandas as pd
from xml.etree.ElementTree import Element, SubElement, ElementTree
import datetime
import os
def generate_rss(input_csv, output_xml):
# Read the CSV file
df = pd.read_csv(input_csv)
# Create the main RSS element
rss = Element('rss', version='2.0')
# Create the channel element and add it to the RSS element
channel = SubElement(rss, 'channel')
# Add mandatory channel elements
SubElement(channel, 'title').text = 'My Podcast'
SubElement(channel, 'description').text = 'Description of My Podcast'
SubElement(channel, 'link').text = 'http://www.example.com'
# Loop through the DataFrame to add episodes
for index, row in df.iterrows():
if not pd.isna(row['audio']):
# Create an item for each episode and add it to the channel
item = SubElement(channel, 'item')
# Add episode details
SubElement(item, 'title').text = str(row['show'])
SubElement(item, 'description').text = str(row['tease'])
SubElement(item, 'pubDate').text = datetime.datetime.strptime(row['airdate'], '%Y-%m-%d').strftime('%a, %d %b %Y 00:00:00 +0000')
# Add the enclosure (audio file)
SubElement(item, 'enclosure', url=str(row['audio']), length='0', type='audio/mpeg')
# Create an ElementTree and write the RSS feed to a file
tree = ElementTree(rss)
tree.write(output_xml)
if __name__ == '__main__':
# Initialize argument parser
parser = argparse.ArgumentParser(description='Generate RSS feed from CSV.')
# Add input and output file arguments
parser.add_argument('-i', '--input', required=True, help='Input CSV file.')
parser.add_argument('-o', '--output', required=False, help='Output XML file.')
# Parse the arguments
args = parser.parse_args()
# Determine the output filename
if args.output:
output_file = args.output
else:
output_file = os.path.splitext(args.input)[0] + '.xml'
# Generate the RSS feed
generate_rss(args.input, output_file)