Permalink
Browse files

Clean up

  • Loading branch information...
1 parent b62c913 commit 2438182983601a1982133e5a03d30c911fb4b41d @jackuess committed Sep 5, 2012
Showing with 276 additions and 250 deletions.
  1. +33 −249 app.wsgi
  2. +25 −0 config.ini
  3. +2 −0 config.py
  4. +71 −0 lib/api.py
  5. +27 −0 lib/genshi_tool.py
  6. +110 −0 lib/root.py
  7. +7 −0 lib/sitemap.py
  8. +1 −1 static/playbrowser/browser.html
View
@@ -1,268 +1,52 @@
# -*- coding: utf-8 -*-
-import cherrypy, json, os, os.path, re, sys
-from copy import deepcopy
-from urllib import quote
+import cherrypy, os, os.path, sys
-from genshi.template import Context, TemplateLoader
-from genshi.core import Markup
-
-sitemap = {}
-
-def relative_time(s):
- if s>31536000:
- t = s/31536000
- singularis = pluralis = u"år"
- elif s>2592000:
- t = s/2592000;
- singularis, pluralis = (u"månad", u"månader")
- elif s>86400:
- t = s/86400;
- singularis, pluralis = (u"dag", u"dagar")
- elif s>3600:
- t = s/3600;
- singularis, pluralis = (u"timme", u"timmar")
- elif s>60:
- t = s/60;
- singularis, pluralis = (u"minut", u"minuter")
- else:
- t = s;
- singularis, pluralis = (u"sekund", u"sekunder")
-
- t = int(round(t))
- try:
- ts = [u'Noll', u'En', u'Två', u'Tre', u'Fyra', u'Fem', u'Sex', u'Sju', u'Åtta', u'Nio', u'Tio', u'Elva', u'Tolv'][t]
- except IndexError:
- ts = str(t)
-
- if t>1:
- return "%s %s " % (ts, pluralis)
- else:
- return "%s %s " % (ts, singularis)
-
-def service_handler(*args, **kwargs):
- value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
- return pirateplay.js_encoder.encode(value)
-
-class GenshiHandler():
- def __init__(self, template, next_handler, type):
- self.template = template
- self.next_handler = next_handler
- self.type = type
-
- def __call__(self):
- context = Context(url=cherrypy.url, quote=quote)
- context.push(self.next_handler())
- stream = self.template.generate(context)
- cherrypy.response.headers['Content-Type'] = { 'xhtml': 'text/html', 'xml': 'application/xml' }[self.type]
- return stream.render(self.type)
-
-
-class GenshiLoader():
- def __init__(self):
- self.loader = None
-
- def __call__(self, filename, dir, auto_reload = False, type = 'xhtml', sitemap_prio = '-1'):
- if self.loader == None:
- self.loader = TemplateLoader(dir, auto_reload=auto_reload)
- template = self.loader.load(filename)
- cherrypy.request.handler = GenshiHandler(template, cherrypy.request.handler, type)
-
-genshi_template = GenshiLoader()
-cherrypy.tools.genshi_template = cherrypy.Tool('before_handler', genshi_template)
-
-def add_to_sitemap(priority = '0'):
- def decorator(func):
- sitemap['.'.join(func.__name__.replace('index', '/').rsplit('_', 1))] = priority
- return func
- return decorator
-
-class Api():
- def _convert_service_re(self, service):
- s = deepcopy(service)
- try:
- s.items[0].re = re.sub(r'(\(\?P<\w+>)|(\(\?:)', '(', service.items[0].re)
- except IndexError:
- return service
- else:
- return s
-
- def _filter_services(self, titles):
- try:
- titles = [t.lower() for t in titles.split(',')]
- except AttributeError:
- return pirateplay.services
- else:
- return [s for s in pirateplay.services if s.title.lower() in titles]
-
- @cherrypy.expose
- @add_to_sitemap('0.5')
- @cherrypy.tools.genshi_template(filename='api/manual.html')
- def manual_html(self):
- return {}
-
- @cherrypy.expose
- @cherrypy.tools.json_out(handler = service_handler)
- def get_streams_js(self, url, rnd = None):
- return pirateplay.get_streams(url)
-
- @cherrypy.expose
- @cherrypy.tools.genshi_template(filename='api/get_streams.xml', type='xml')
- def get_streams_xml(self, url, rnd = None):
- return {'streams': [s.to_dict() for s in pirateplay.get_streams(url)]}
-
- @cherrypy.expose
- @cherrypy.tools.genshi_template(filename='api/get_streams_old.xml', type='xml')
- def get_streams_old_xml(self, url, librtmp = '0', output_file = '-', parent_function = ''):
- streams = pirateplay.get_streams(url)
-
- if streams[0].url.startswith('rtmp') and librtmp == '0':
- return { 'streams': [{'url': pirateplay.rtmpdump_cmd(s.url, output_file), 'meta': s.metadict()} for s in streams] }
- elif '.m3u8' in streams[0].url or 'manifest.f4m' in streams[0].url:
- return { 'streams': [{ 'meta': { 'quality': u'Inkompatibel ström. Testa appen på Pirateplay.se.' }, 'url': 'http://localhost/' }] }
- else:
- return { 'streams': [s.to_dict() for s in streams] }
-
- @cherrypy.expose
- @cherrypy.tools.json_out(handler = service_handler)
- def services_js(self, titles = None, rnd = ''):
- s = self._filter_services(titles)
- return [self._convert_service_re(z) for z in s]
-
- @cherrypy.expose
- @cherrypy.tools.genshi_template(filename='api/services.xml', type='xml')
- def services_xml(self, titles = None, rnd = ''):
- return {'services': [self._convert_service_re(s).to_dict()
- for s in self._filter_services(titles)]}
-
-class Root():
- def __init__(self):
- self.sitemap = {}
-
- @cherrypy.expose
- @cherrypy.tools.genshi_template(filename='sitemap.xml', type='xml')
- def sitemap_xml(self):
- return { 'sites': sitemap }
-
- @cherrypy.expose
- def sitemap_html(self):
- return '<br />'.join(['<a href="%s">%s</a>' % ((i,)*2)
- for i in sitemap])
-
- @cherrypy.expose
- @cherrypy.tools.genshi_template(filename='index.html')
- @add_to_sitemap('1.0')
- def index(self):
- from urllib2 import urlopen
- import datetime
-
- now = datetime.datetime.now()
- tweets = [{'text': Markup(re.sub(r'(https?://\S+)', '<a href="\\1">\\1</a>', tweet['text'], flags=re.IGNORECASE)),
- 'time': unicode(relative_time((now - datetime.datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S +0000 %Y")).total_seconds()))}
- for tweet in json.load(urlopen('http://twitter.com/statuses/user_timeline.json?screen_name=pirateplay_se&count=10'))
- if not tweet['text'].startswith('@')]
-
- services_se = sorted([s.to_dict() for s in pirateplay.services if len(s.items) > 0 and '\.se/' in s.items[0].re and s.title != ''], key=lambda s: s['title'])
- services_other = sorted([s.to_dict() for s in pirateplay.services if len(s.items) > 0 and not '\.se/' in s.items[0].re and s.title != ''], key=lambda s: s['title'])
-
- return dict(services_se = services_se, services_other = services_other, tweets = tweets)
-
- @cherrypy.expose
- @add_to_sitemap('0.5')
- @cherrypy.tools.genshi_template(filename='app.html')
- def app_html(self):
- return {}
-
-
- @cherrypy.expose
- @add_to_sitemap('0.5')
- @cherrypy.tools.genshi_template(filename='library.html')
- def library_html(self):
- return {}
-
- @cherrypy.expose
- @add_to_sitemap('0.5')
- @cherrypy.tools.genshi_template(filename='hls_guide.html')
- def hls_guide_html(self):
- return {}
-
- @cherrypy.expose
- @add_to_sitemap('0.5')
- @cherrypy.tools.genshi_template(filename='qna.html')
- def qna_html(self):
- qna_txt = open('data/qna.txt', 'r')
- qna = dict([pair.split('<!-- inner_delim -->') for pair in qna_txt.read().decode('utf-8').split('<!-- delim -->')])
- qna = dict([(Markup(q.strip()), Markup(a.strip())) for q, a in qna.items()])
-
- return {'qna': qna}
-
- @cherrypy.expose
- @add_to_sitemap('0.8')
- @cherrypy.tools.genshi_template(filename='player.html')
- def player_html(self):
- return dict(services = sorted([s.to_dict() for s in pirateplay.services if s.title != ''], key=lambda s: s['title']))
-
- @cherrypy.tools.genshi_template(filename='notfound.html')
- def default(self, *args, **kwargs):
- cherrypy.response.status = 404
- return {}
-
- api = Api()
- generate_application_xml = api.get_streams_old_xml
-
-
-def _config(base_dir):
- from string import maketrans
-
- return { 'global': {
- 'server.environment': 'production',
- 'server.socket_host': '0.0.0.0',
- 'request.show_tracebacks': False,
- 'server.socket_port': 80,
- 'tools.genshi_template.dir': os.path.join(base_dir, 'templates'),
- 'tools.genshi_template.auto_reload': False },
- '/': {
- #'request.dispatch': cherrypy.dispatch.Dispatcher(translate=maketrans(';', '_')),
- 'tools.encode.on': True,
- 'tools.encode.encoding': 'utf-8'},
- '/static': {
- 'tools.staticdir.on': True,
- 'tools.staticdir.root': base_dir,
- 'tools.staticdir.dir': 'static' },
- '/playbrowser': {
- 'tools.staticdir.on': True,
- 'tools.staticdir.root': base_dir,
- 'tools.staticdir.dir': 'static/playbrowser' },
- '/googleda06c6c176f69c2f.html': {
- 'tools.staticfile.on': True,
- 'tools.staticfile.filename': os.path.join(base_dir, 'googleda06c6c176f69c2f.html') } }
+#We can't do all imports before we know that cwd is properly set
+def do_imports():
+ global genshi_tool, Root
+ import lib.genshi_tool as genshi_tool
+ genshi_template = genshi_tool.GenshiLoader()
+ cherrypy.tools.genshi_template = cherrypy.Tool('before_handler', genshi_template)
+ from lib.root import Root
def application(environ, start_response):
base_dir = environ.get('pirateplay_base_dir', '')
os.chdir(base_dir)
if not base_dir in sys.path:
sys.path.insert(0, base_dir)
- global pirateplay
- import lib.pirateplay as pirateplay
- config = _config(base_dir)
- config['global']['server.socket_port'] = environ.get('pirateplay_port', '80')
- cherrypy.config.update(config)
+ do_imports()
+
+ cherrypy.config.update('config.ini')
- cherrypy.tree.mount(Root(), config = config, script_name = '')
+ confdict = { 'global': { 'server.socket_port': int(environ.get('pirateplay_port', '80')) } }
+ cherrypy.config.update(confdict)
+
+ app = cherrypy.tree.mount(Root(), '', 'config.ini')
+ app.merge(confdict)
return cherrypy.tree(environ, start_response)
+
if __name__ == "__main__":
- import lib.pirateplay as pirateplay
+ do_imports()
- base_dir = os.getcwd()
+ cherrypy.config.update('config.ini')
- config = _config(base_dir)
- config['global']['server.socket_port'] = 8081
- config['global']['request.show_tracebacks'] = True
- config['global']['tools.genshi_template.auto_reload'] = True
- cherrypy.config.update(config)
+ confdict = { 'global': { 'server.socket_port': 8081,
+ 'request.show_tracebacks': True,
+ 'tools.genshi_template.auto_reload': True } }
+ cherrypy.config.update(confdict)
+
+ app = cherrypy.tree.mount(Root(), '', 'config.ini')
+ app.merge(confdict)
+
+ if hasattr(cherrypy.engine, "signal_handler"):
+ cherrypy.engine.signal_handler.subscribe()
+ if hasattr(cherrypy.engine, "console_control_handler"):
+ cherrypy.engine.console_control_handler.subscribe()
- cherrypy.quickstart(Root(), config = config, script_name='')
+ cherrypy.engine.start()
+ cherrypy.engine.block()
View
@@ -0,0 +1,25 @@
+[global]
+server.environment = 'production'
+server.socket_host = '0.0.0.0'
+request.show_tracebacks = False
+server.socket_port = 80
+tools.genshi_template.dir = os.path.join(config.base_dir, 'templates')
+tools.genshi_template.auto_reload = False
+
+[/]
+tools.encode.on = True,
+tools.encode.encoding = 'utf-8'
+
+[/static]
+tools.staticdir.on = True,
+tools.staticdir.root = config.base_dir
+tools.staticdir.dir = 'static'
+
+[/playbrowser]
+tools.staticdir.on = True,
+tools.staticdir.root = config.base_dir
+tools.staticdir.dir = 'static/playbrowser'
+
+[/googleda06c6c176f69c2f.html]
+tools.staticfile.on = True
+tools.staticfile.filename = os.path.join(config.base_dir, 'googleda06c6c176f69c2f.html')
View
@@ -0,0 +1,2 @@
+from os import getcwd
+base_dir = getcwd()
Oops, something went wrong.

0 comments on commit 2438182

Please sign in to comment.