#!/usr/bin/env python
# encoding: utf-8
from os.path import relpath,basename
from bottle import route,run,request,response,static_file,jinja2_template,GunicornServer
from glob import glob
DIRECTORY=None
@route("/")
def index():
podcasts = dict()
for cast in glob(DIRECTORY + "/*"):
podcasts[relpath(cast,DIRECTORY)] = sorted(map(lambda p: relpath(p, DIRECTORY), glob(cast+"/*mp3")),reverse=True)
template = """
xapek.org Podcastserver
Podcastserver
{% for cast,titles in podcasts.items() %}
{{cast.decode("utf-8")}}
{% endfor %}
"""
return jinja2_template(template, podcasts=podcasts,basename=basename)
@route('/mp3/')
def mp3(filename):
return static_file(filename, root=DIRECTORY)
@route("/m3u/")
def m3u(filename):
response.content_type = "audio/x-mpegurl"
response.body = request.environ['wsgi.url_scheme']+"://"+request.environ['HTTP_HOST'] + "/mp3/" + filename
return response
if __name__ == '__main__':
from sys import argv,exit
DIRECTORY=( len(argv) == 2 and argv[1] or exit(1))
server = GunicornServer()
server.options.update({"workers":8,"daemon":False})
run(host="0.0.0.0",port=8080,server=server,reloader=True,debug=True)