Skip to content

Instantly share code, notes, and snippets.

@om-henners
Created July 3, 2019 14:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save om-henners/3547ce4e3432494e3eaa662e52355ee0 to your computer and use it in GitHub Desktop.
Save om-henners/3547ce4e3432494e3eaa662e52355ee0 to your computer and use it in GitHub Desktop.
Simple flask app to dummy up a Podcast RSS feed using Flask and feedgen. Written with Python 3.7
"""
Simple app that generates a podcasst XML feed for Radicalized
To be fair this could be easily adapted to any audiobook / set of NP3s - you'd
just need to change the image, the path to the music and all the descriptions.
Requires:
- feedgen
- flask
Run using:
FLASK_APP=podcast_app FLASK_ENV=production flask run --host 0.0.0.0 --port 80
Then connect your podcast app to your computer's IP/index.xml and you're good
to go (i.e. http://<ipaddr>/index.xml).
"""
import pathlib
import socket
from feedgen.feed import FeedGenerator
from flask import Flask, Response, send_from_directory
def get_ip_address():
"""Get current IP address.
From https://stackoverflow.com/a/166589/379566
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
# Set file location and server location defaults
IMAGE_DIR = '.'
BOOK_DIR = pathlib.Path('~/Music/Radicalized Audiobook').expanduser()
SERVER_HOME = 'http://{}'.format(get_ip_address())
# Build an RSS feed and load the podcasst extension
fg = FeedGenerator()
fg.load_extension('podcast')
fg.link(href=SERVER_HOME)
fg.podcast.itunes_author('Cory Doctorow')
fg.podcast.itunes_category([
{'cat': 'Book', 'sub': 'Technology'},
{'cat': 'Book', 'sub': 'Science Fiction'}
])
fg.podcast.itunes_complete('yes')
fg.podcast.itunes_image('/'.join([SERVER_HOME, 'image/radicalized-full-US-cover-large.jpg']))
fg.title('radicalized')
fg.podcast.itunes_subtitle('Four tales of our present moment')
fg.description("""If you want a better future tomorrow, you're going to have to fight for it today.
Here are four urgent stories from author and activist Cory Doctorow, four social, technological and economic visions of the world today and its near – all too near – future.
'Unauthorized Bread' is a tale of immigration, toxic economic stratification and a young woman's perilously illegal quest to fix a broken toaster.
In 'Model Minority' a superhero finds himself way out his depth when he confronts the corruption of the police and justice system.
'Radicalized' is the story of a desperate husband, a darknet forum and the birth of a violent uprising against the US health care system.
The final story, 'The Masque of the Red Death', tracks an uber-wealthy survivalist and his followers as they hole up and attempt to ride out the collapse of society.""")
# Loop over the MP3s, pulling the title and chapter number from the filename.
# Added in reverse order so the first chapter is first in the feed
for path in sorted(BOOK_DIR.glob('*.mp3'), reverse=True):
mp3_url = '/'.join([SERVER_HOME, 'mp3', path.name])
chapter, mp3_title = path.name.split('_', maxsplit=1)
mp3_title = mp3_title.rstrip('.mp3')
fe = fg.add_entry()
fe.id(mp3_url)
fe.title(mp3_title)
fe.description(f'Chapter {chapter}')
fe.enclosure(mp3_url, 0, 'audio/mpeg')
# Create the flask server
app = Flask(__name__)
@app.route('/mp3/<path:path>')
def send_mp3(path):
return send_from_directory(BOOK_DIR, path)
@app.route('/image/<path:path>')
def send_image(path):
return send_from_directory(IMAGE_DIR, path)
@app.route('/index.xml')
def get_feed():
return Response(fg.rss_str(), mimetype='application/rss+xml')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment