-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_feed.py
105 lines (89 loc) · 3.33 KB
/
generate_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
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
#!/usr/bin/env python3
import glob
import os
import re
from datetime import datetime, timedelta
from feedgen.feed import FeedGenerator
from mutagen.mp3 import MP3
import pytz
# ----------------------------------
# Configure variables for the feed
# ----------------------------------
# Base URL for where the podcast files and feed will ultimately live
base_url = 'http://files.example.com/fauxcasts/book_name/'
# Name for the RSS file
feed_name = 'feed.rss'
feed_url = base_url + feed_name
# Information about the podcast
feed_title = 'Podcast title'
feed_description = "Description of podcast"
feed_author = 'Some name here'
feed_author_email = '[email protected]'
feed_homepage = 'http://www.example.com'
# Name of the pre-uploaded podcast cover image
cover_image = base_url + 'cover.jpg'
# Absolute or relateive path to MP3 files on your local computer
#
# NB: Use the *.mp3 syntax to select and parse all MP3s in the folder
# Also NB: Make sure each file follows this naming convention:
# n-xx Name of the track.mp3
# where `n` is the disc number (e.g. 1), and `xx` is the track number (e.g. 07)
# Example: 2-14 Act IV Scene iii.mp3
#
# If you want, you can change the regular expression that parses these
# filenames below at `track_name_raw = re.match(...)`
# local_location = '/path/to/ripped/mp3s/*.mp3'
local_location = 'path/to/mp3_files/*.mp3'
# ----------------------
# Generate actual feed
# ----------------------
# Generate feed
fg = FeedGenerator()
fg.load_extension('podcast')
# Add descriptive variables to the feed
fg.id(feed_url)
fg.title(feed_title)
fg.author({'name': feed_author, 'email': feed_author_email})
fg.link(href=feed_homepage, rel='alternate')
fg.logo(cover_image)
fg.subtitle(feed_description)
fg.link(href=feed_url, rel='self')
fg.language('en')
fg.podcast.itunes_block(True)
fg.podcast.itunes_complete(True)
# Loop through each MP3 and add it to the feed as an episode
for i, track in enumerate(sorted(glob.glob(local_location))):
# Some podcast players respect the itunes_order attribute, which is set
# below, but many only look at the date and time of the episode. So, here
# we pretend that the first episode happened 7 days ago, and each
# subsequent episode is released 1 hour later.
episode_date = (datetime.now(tz=pytz.utc) -
timedelta(days=7) +
timedelta(hours=i + 1))
# Get the file size
file_size = os.path.getsize(track)
# Remove the disk and track numbers from the file names and use just the
# title as the episode name
track_filename = os.path.basename(track)
track_name_raw = re.match(r"\d-\d{2} (.*)\.mp3", track_filename)
track_name = track_name_raw.group(1)
# Get the duration
audio = MP3(track)
m, s = divmod(audio.info.length, 60) # Convert seconds to h:m:s
h, m = divmod(m, 60)
if h == 0:
duration = "%02d:%02d" % (m, s)
else:
duration = "%d:%02d:%02d" % (h, m, s)
# Generate entry
fe = fg.add_entry()
fe.guid(base_url + track_filename)
fe.link({'href': base_url + track_filename})
fe.title(track_name)
fe.description(track_name)
fe.published(episode_date)
fe.enclosure(base_url + track_filename, str(file_size), 'audio/mpeg')
fe.podcast.itunes_order(i + 1)
fe.podcast.itunes_duration(duration)
# Write the feed to a file
fg.rss_file(feed_name, pretty=True)