Last active
February 20, 2020 14:21
-
-
Save milgner/2765a398080e7fd72338f7504224267d to your computer and use it in GitHub Desktop.
Auto-generate Podlove Web Player from MP3 folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<script src="https://cdn.podlove.org/web-player/embed.js"></script> | |
{% for episode in episodes %} | |
<div id="player{{ loop.index0 }}"></div> | |
<script> | |
podlovePlayer('#player{{ loop.index0 }}', { | |
title: '{{ episode.title }}', | |
publicationDate: '{{ episode.publication_date }}', | |
poster: '{{ episode.poster_url }}', | |
show: { | |
title: 'Stille Wasser', | |
subtitle: 'Ein Vampire The Masquerade Podcast', | |
summary: 'Hier kommt eine tolle Zusammenfassung rein', | |
poster: '{{ poster_url }}', | |
url: 'https://sites.google.com/view/vtm-stillewasser/startseite' | |
}, | |
duration: '{{ episode.duration }}', | |
chapters: [ | |
{% for chapter in episode.chapters %} | |
{ start:"{{ chapter.start }}", title: '{{ chapter.name }}'}, | |
{% endfor %} | |
], | |
audio: [{ | |
url: 'https://vtmsw.illunis.net/{{episode.filename}}', | |
mimeType: 'audio/mpeg', | |
size: {{ episode.filesize }}, | |
title: 'Audio MP3' | |
}], | |
contributors: [{ | |
name: 'Marcus Ilgner', | |
avatar: 'https://avatars3.githubusercontent.com/u/160025?s=460&v=4', | |
comment: null | |
}] | |
}); | |
</script> | |
{% endfor %} | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import pyinotify | |
import asyncio | |
import asyncore | |
import os | |
import eyed3 | |
from jinja2 import Environment, FileSystemLoader, select_autoescape | |
from types import SimpleNamespace | |
directory = '.' | |
template_file = 'index.html.j2' | |
target_file = './index.html' | |
poster_url = 'https://lh3.googleusercontent.com/plncUw1UDy_wEfCcW02tFKiIciVF324Mn8s-xXKRnLSXBIEbY9nzNUP38Qq0HzAP_nrDbA' | |
env = Environment( | |
loader=FileSystemLoader(directory), | |
autoescape=select_autoescape(['html']) | |
) | |
def duration_from_seconds(s): | |
"""Module to get the convert Seconds to a time like format.""" | |
s = s | |
m, s = divmod(s, 60) | |
h, m = divmod(m, 60) | |
d, h = divmod(h, 24) | |
timelapsed = "{:01d}:{:02d}:{:02d}:{:02d}".format(int(d), | |
int(h), | |
int(m), | |
int(s)) | |
return timelapsed | |
def get_episode_info(filename): | |
data = eyed3.load(filename) | |
episode_info = { | |
'filename': filename, | |
'title': data.tag.title, | |
'duration': duration_from_seconds(data.info.time_secs), | |
'publication_date': data.tag.release_date, | |
'filesize': data.info.size_bytes, | |
'poster_url': poster_url, | |
'chapters': [] | |
} | |
return SimpleNamespace(**episode_info) | |
def update_player(): | |
episodes = [] | |
for filename in os.listdir(directory): | |
if filename.endswith(".mp3") or filename.endswith(".mp4"): | |
episodes.append(get_episode_info(filename)) | |
template = env.get_template(template_file) | |
html = template.render(episodes=episodes, poster_url=poster_url) | |
file = open(target_file, 'w') | |
file.write(html) | |
file.close() | |
class EventProcessor(pyinotify.ProcessEvent): | |
def process_IN_CLOSE_WRITE(self, event): | |
if not event.pathname.endswith('.mp3'): | |
return | |
update_player() | |
wm = pyinotify.WatchManager() | |
loop = asyncio.get_event_loop() | |
notifier = pyinotify.AsyncNotifier(wm, EventProcessor()) | |
wm.add_watch(directory, pyinotify.IN_CLOSE_WRITE) | |
asyncore.loop() | |
notifier.stop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment