pandoralocal

This commit is contained in:
j 2011-12-22 17:50:47 +05:30
commit 47182a28d6
10 changed files with 332 additions and 0 deletions

19
README Normal file
View File

@ -0,0 +1,19 @@
pandoralocal runs a webserver on http://localhost:2620
that provides services that are integrated with a pandora instance
- there should be a way to start pandoralocal with a user session for osx, win32, linux
- possibly some gui to start/stop and enable/disable session startup
once running the configuration is done via web interface
there are 2 modes
a) integration with a pandora instance, this happens via trying to connect to
http://local.pad.ma:2620/api/ and if successfull use the local api in the pandora
site to manage local volumes(upload/sync), cached videos(playback, download for later playback)
b) offline annotations(something like speedtrans) with ability to sync/upload to pandora later
this uses the same cache for videos but runs on http://127.0.0.1:2620/pad.ma/
since there might be no dns lookup for local.pad.ma possible

25
bin/pandoralocal Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
# GPL 2008
import os
import sys
from glob import glob
from optparse import OptionParser
root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
if os.path.exists(os.path.join(root, 'pandoralocal')):
sys.path.insert(0, root)
import pandoralocal
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-c', '--config', dest='config', help='config file', default='config.json')
(opts, args) = parser.parse_args()
if None in (opts.config, ):
parser.print_help()
sys.exit()
pandoralocal.main(opts.config)

22
pandoralocal/__init__.py Normal file
View File

@ -0,0 +1,22 @@
# encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4
import os
from twisted.web.server import Site
from twisted.internet import reactor
from backend import Backend
from server import Server
from version import __version__
def main(config):
base = os.path.abspath(os.path.dirname(__file__))
backend = Backend(config)
root = Server(base, backend)
site = Site(root)
port = 2620
interface = '127.0.0.1'
reactor.listenTCP(port, site, interface=interface)
reactor.run()

12
pandoralocal/api.py Normal file
View File

@ -0,0 +1,12 @@
# encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4
from server import actions, json_response
def echo(backend, site, data):
return json_response(data)
actions.register(echo, cache=False)
def site(backend, site, data):
return json_response({'site': site})
actions.register(site, cache=False)

15
pandoralocal/backend.py Normal file
View File

@ -0,0 +1,15 @@
# encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4
class Backend:
def __init__(self, config):
self.config = config
def get_file(self, site, itemId, filename):
filename, ext = filename.split('.')
resolution, part = filename.split('p')
print site, itemId, resolution, part, ext
path = ''
if resolution == '480' and ext == 'webm':
path = '/home/j/.ox/media/44/c4/b1/11a888e96a/480p.webm'
return path

198
pandoralocal/server.py Normal file
View File

@ -0,0 +1,198 @@
# encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4
import sys
import inspect
import os
import json
from urlparse import urlparse
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.web.util import Redirect
from version import __version__
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
def json_response(data=None, status=200, text='ok'):
if not data:
data = {}
return {'status': {'code': status, 'text': text}, 'data': data}
class ApiActions(dict):
properties = {}
def __init__(self):
def api(site, data):
'''
returns list of all known api actions
param data {
docs: bool
}
if docs is true, action properties contain docstrings
return {
status: {'code': int, 'text': string},
data: {
actions: {
'api': {
cache: true,
doc: 'recursion'
},
'hello': {
cache: true,
..
}
...
}
}
}
'''
docs = data.get('docs', False)
code = data.get('code', False)
_actions = self.keys()
_actions.sort()
actions = {}
for a in _actions:
actions[a] = self.properties[a]
if docs:
actions[a]['doc'] = self.doc(a)
if code:
actions[a]['code'] = self.code(a)
response = json_response({'actions': actions})
return response
self.register(api)
def doc(self, f):
return trim(self[f].__doc__)
def code(self, name):
f = self[name]
if name != 'api' and hasattr(f, 'func_closure') and f.func_closure:
f = f.func_closure[0].cell_contents
info = f.func_code.co_firstlineno
return info, trim(inspect.getsource(f))
def register(self, method, action=None, cache=True):
if not action:
action = method.func_name
self[action] = method
self.properties[action] = {'cache': cache}
def unregister(self, action):
if action in self:
del self[action]
def render(self, backend, site, action, data):
if action in self:
result = self[action](backend, site, data)
else:
result = json_response(status=404, text='not found')
print result
return json.dumps(result)
actions = ApiActions()
class Server(Resource):
def __init__(self, base, backend):
self.base = base
self.backend = backend
Resource.__init__(self)
def static_path(self, path):
return os.path.join(self.base, 'static', path)
def get_site(self, request):
headers = request.getAllHeaders()
#print headers
if 'origin' in headers:
request.headers['Access-Control-Allow-Origin'] = headers['origin']
site = headers['origin']
elif 'referer' in headers:
u = urlparse(headers['referer'])
site = u.scheme + '://' + u.hostname
else:
site = 'http://' + headers['host']
return site
def getChild(self, name, request):
print request
if name in ('icon.png', 'favicon.ico'):
f = File(self.static_path('png/icon16.png'))
f.isLeaf = True
return f
if request.path == '/api/':
return self
if request.path.endswith('.webm'):
video = request.path
site = self.get_site(request)
itemId, filename = video[1:].split('/')
path = self.backend.get_file(site, itemId, filename)
if path:
print itemId, filename, 'use', path
request.headers['Access-Control-Allow-Origin'] = '*'
f = File(path)
f.isLeaf = True
return f
else:
url = site + video
#url = 'http://padmo.local/B/240p.webm'
print "redirect", url
return Redirect(url)
path = request.path
path = path[1:]
if not path:
path = 'index.html'
path = self.static_path(path)
f = File(path)
if not os.path.isdir(path):
f.isLeaf = True
return f
def render_POST(self, request):
print 'render_POST'
request.headers['Server'] = 'pandoralocal/%s' % __version__
site = self.get_site(request)
print "POST", request.args
if 'action' in request.args:
if 'data' in request.args:
data = json.loads(request.args['data'][0])
else:
data = {}
action = request.args['action'][0]
return actions.render(self.backend, site, action, data)
def render_GET(self, request):
print 'render_GET'
request.headers['Server'] = 'pandoralocal/%s' % __version__
f = open('static/index.html')
data = f.read()
f.close()
request.headers['Content-Type'] = 'text/html'
site = self.get_site(request)
data = data.replace('$name', site)
return data

View File

@ -0,0 +1,2 @@
only robots may pass this point
$name

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

1
pandoralocal/version.py Normal file
View File

@ -0,0 +1 @@
__version__ = 0.0

38
setup.py Normal file
View File

@ -0,0 +1,38 @@
# setup.py
# -*- coding: UTF-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
try:
from setuptools import setup
except:
from distutils.core import setup
def get_version():
import os
from pandoralocal.version import __version__
info = os.path.join(os.path.dirname(__file__), '.bzr/branch/last-revision')
if os.path.exists(info):
f = open(info)
rev = int(f.read().split()[0])
f.close()
if rev:
return u'%s.%s' % (__version__, rev)
return '%s'%__version__
setup(name="pandoralocal",
version=get_version() ,
scripts=[
'bin/pandoralocal',
],
packages=[
'pandoralocal',
],
author="0x2620",
author_email="0x2620@0x2620.org",
description="pandoralocal allows users to use local videos on a pan.do/ra site",
classifiers = [
'Development Status :: 1 - Development/Unstable',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
)