summaryrefslogtreecommitdiff
path: root/podcastserver/podcastserver.py
blob: fe06cb3ba5b736d9cd3aadceedd7e3b547dbaf09 (plain)
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
#!/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 = """<!doctype html>
<html><head>
   <title>xapek.org Podcastserver</title>    
   <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
   <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
   <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
   <style>audio { width: 100%; }</style>
</head><body>
   <h1>Podcastserver</h1>
   <div id="podcasts">
       {% for cast,titles in podcasts.items() %}
       <h3>{{cast.decode("utf-8")}}</h3>
       <div class="podcast">
           {% for file in titles[:15] %}
               <a href="/mp3/{{file.decode("utf-8")}}">{{basename(file.decode("utf-8"))}}</a>
   	    (<a class="html5audio" href="#" data-src="/mp3/{{file.decode("utf-8")}}">html5</a>,
   	    <a href="/m3u/{{file.decode("utf-8")}}">playlist</a>)
   	    <br />
           {% endfor %}
       </div>
   {% endfor %}
   </div>
   <script>
       $(window).load(function() {
           $("#podcasts").accordion({collapsible: true, active: false, animate: false, heightStyle: "content"});
           $("a.html5audio").click(function(){
               $(this).parent().after(
                   $("<audio>").attr("src", $(this).data("src"))
                       .attr("preload", "none").attr("controls", "true")
                       .text("No html5 audio support"));
           });
       });
   </script>
</body></html>"""
    return jinja2_template(template, podcasts=podcasts,basename=basename)

@route('/mp3/<filename:path>')
def mp3(filename):
    return static_file(filename, root=DIRECTORY)

@route("/m3u/<filename:path>")
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)