summaryrefslogtreecommitdiffstats
path: root/module/web
diff options
context:
space:
mode:
authorGravatar spoob <spoob@gmx.de> 2009-11-22 18:51:14 +0100
committerGravatar spoob <spoob@gmx.de> 2009-11-22 18:51:14 +0100
commit419a93755b6180bbab69aaf973cd99f49b4a2268 (patch)
tree5996b06cb7713058626fc48c44651df8284dd303 /module/web
parentfixed plugin index (diff)
downloadpyload-419a93755b6180bbab69aaf973cd99f49b4a2268.tar.xz
Webinterface with XMLRPC
Diffstat (limited to 'module/web')
-rw-r--r--module/web/WebServer.py354
-rw-r--r--module/web/main.html29
-rw-r--r--module/web/main.js42
3 files changed, 139 insertions, 286 deletions
diff --git a/module/web/WebServer.py b/module/web/WebServer.py
index 8cef0de2f..0712f1dce 100644
--- a/module/web/WebServer.py
+++ b/module/web/WebServer.py
@@ -1,290 +1,72 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-#
-#Copyright (C) 2009 RaNaN
-#
-#This program is free software; you can redistribute it and/or modify
-#it under the terms of the GNU General Public License as published by
-#the Free Software Foundation; either version 3 of the License,
-#or (at your option) any later version.
-#
-#This program is distributed in the hope that it will be useful,
-#but WITHOUT ANY WARRANTY; without even the implied warranty of
-#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-#See the GNU General Public License for more details.
-#
-#You should have received a copy of the GNU General Public License
-# along with this program; if not, see <http://www.gnu.org/licenses/>.
-#
-###
+import sys
+from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
+from xmlrpclib import ServerProxy
+from time import time
+import re
-import random
-import threading
-import time
-
-import bottle
-from bottle import abort
-from bottle import redirect
-from bottle import request
-from bottle import response
-from bottle import route
-from bottle import run
-from bottle import send_file
-from bottle import template
-from bottle import validate
-
-
-core = None
-
-PATH = "./module/web/"
-TIME = time.strftime("%a, %d %b %Y 00:00:00 +0000", time.localtime()) #set time to current day
-USERS = {}
-
-@route('/login', method='POST')
-def do_login():
- #print request.GET
-
-
- username = core.config['webinterface']['username']
- pw = core.config['webinterface']['password']
-
- if request.POST['u'] == username and request.POST['p'] == pw:
-
- id = int(random.getrandbits(16))
- ua = request.HEADER("HTTP_USER_AGENT")
- ip = request.HEADER("REMOTE_ADDR")
-
- auth = {}
-
- auth['ua'] = ua
- auth['ip'] = ip
- auth['user'] = username
-
- USERS[id] = auth
-
- response.COOKIES['user'] = username
- response.COOKIES['id'] = id
-
- return template('default', page='loggedin', user=username)
- else:
- return template('default', page='login')
-
-@route('/login')
-def login():
-
- if check_auth(request):
- redirect("/")
-
- return template('default', page='login')
-
-@route('/logout')
-def logout():
- try:
- del USERS[int(request.COOKIES.get('id'))]
- except:
- pass
+class Handler(BaseHTTPRequestHandler):
- redirect("/login")
-
-@route('/')
-def home():
-
- if not check_auth(request):
- redirect("/login")
-
- username = request.COOKIES.get('user')
-
- dls = core.get_downloads()
-
- for dl in dls:
- dl['eta'] = str(core.format_time(dl['eta']))
- dl['wait_until'] = str(core.format_time(dl['wait_until'] - time.time()))
-
-
- return template('default', page='home', links=dls, user=username, status=core.server_status())
-
-@route('/queue')
-def queue():
-
- if not check_auth(request):
- redirect("/login")
-
- username = request.COOKIES.get('user')
-
- return template('default', page='queue', links=core.get_links(), user=username, status=core.server_status())
-
-@route('/downloads')
-def downloads():
-
- if not check_auth(request):
- redirect("/login")
-
- username = request.COOKIES.get('user')
-
- return template('default', page='downloads', links=core.get_downloads(), user=username, status=core.server_status())
-
-
-@route('/logs')
-def logs():
-
- if not check_auth(request):
- redirect("/login")
-
- username = request.COOKIES.get('user')
-
- return template('default', page='logs', links=core.get_downloads(), user=username, status=core.server_status())
-
-@route('/json/links')
-def get_links():
- response.header['Cache-Control'] = 'no-cache, must-revalidate'
- response.content_type = 'application/json'
-
- if not check_auth(request):
- abort(404, "No Access")
-
- json = '{ "downloads": ['
-
- downloads = core.get_downloads()
- ids = []
-
- for dl in downloads:
- ids.append(dl['id'])
- json += '{'
- json += '"id": %s, "name": "%s", "speed": %s, "eta": "%s", "kbleft": %s, "size": %s, "percent": %s, "wait": "%s", "status": "%s"'\
- % (str(dl['id']), str(dl['name']), str(int(dl['speed'])), str(core.format_time(dl['eta'])), dl['kbleft'], dl['size'], dl['percent'], str(core.format_time(dl['wait_until'] - time.time())), dl['status'])
-
- json += "},"
-
- if json.endswith(","): json = json[:-1]
-
- json += '], "ids": %s }' % str(ids)
-
- return json
-
-@route('/json/status')
-def get_status():
- response.header['Cache-Control'] = 'no-cache, must-revalidate'
- response.content_type = 'application/json'
-
- if not check_auth(request):
- abort(404, "No Access")
-
- data = core.server_status()
-
- if data['pause']:
- status = "paused"
- else:
- status = "running"
-
- json = '{ "status": "%s", "speed": "%s", "queue": "%s" }' % (status, str(int(data['speed'])), str(data['queue']))
-
- return json
-
-@route('/json/addlinks', method='POST')
-def add_links():
- response.header['Cache-Control'] = 'no-cache, must-revalidate'
- response.content_type = 'application/json'
-
- if not check_auth(request):
- abort(404, "No Access")
-
- links = request.POST['links'].split('\n')
-
- core.add_links(links)
-
- return "{}"
-
-@route('/json/pause')
-def pause():
- response.header['Cache-Control'] = 'no-cache, must-revalidate'
- response.content_type = 'application/json'
-
- if not check_auth(request):
- abort(404, "No Access")
-
- core.thread_list.pause = True
-
- return "{}"
-
-
-@route('/json/play')
-def play():
- response.header['Cache-Control'] = 'no-cache, must-revalidate'
- response.content_type = 'application/json'
-
- if not check_auth(request):
- abort(404, "No Access")
-
- core.thread_list.pause = False
-
- return "{}"
-
-@route('/favicon.ico')
-def favicon():
-
- if request.HEADER("HTTP_IF_MODIFIED_SINCE") == TIME: abort(304, "Not Modified")
-
- response.header['Last-Modified'] = TIME
-
- send_file('favicon.ico', root=(PATH + 'static/'))
-
-@route('static/:section/:filename')
-def static_folder(section, filename):
-
- if request.HEADER("HTTP_IF_MODIFIED_SINCE") == TIME: abort(304, "Not Modified")
-
- response.header['Last-Modified'] = TIME
- send_file(filename, root=(PATH + 'static/' + section))
-
-@route('/static/:filename')
-def static_file(filename):
-
- if request.HEADER("HTTP_IF_MODIFIED_SINCE") == TIME: abort(304, "Not Modified")
-
- response.header['Last-Modified'] = TIME
- send_file(filename, root=(PATH + 'static/'))
-
-
-def check_auth(req):
-
- try:
- user = req.COOKIES.get('user')
- id = int(req.COOKIES.get('id'))
- ua = req.HEADER("HTTP_USER_AGENT")
- ip = req.HEADER("REMOTE_ADDR")
-
- if USERS[id]['user'] == user and USERS[id]['ua'] == ua and USERS[id]['ip'] == ip:
- return True
- except:
- return False
-
- return False
-
-
-class WebServer(threading.Thread):
- def __init__(self, pycore):
- threading.Thread.__init__(self)
-
- global core, TIME
- core = pycore
- self.core = pycore
- self.setDaemon(True)
-
- if pycore.config['general']['debug_mode']:
- bottle.debug(True)
- TIME = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
+ def do_GET(self):
+ global coreserver
+ stdout = sys.stdout
+ sys.stdout = self.wfile
+ if self.path == "/":
+ print "Server Runs"
+ elif self.path == "/downloads":
+ print self.get_downloads()
+ elif re.search("/add=.?", self.path):
+ if re.match(is_url, self.path.split("/add=")[1]):
+ coreserver.add_urls([self.path.split("/add=")[1]])
+ print "Link Added"
else:
- bottle.debug(False)
-
- #@TODO remove
- #TIME = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
-
- bottle.TEMPLATE_PATH.append('./module/web/templates/%s.tpl')
-
- def run(self):
- self.core.logger.info("Starting Webinterface on %s port %s" % (self.core.config['webinterface']['listenaddr'],self.core.config['webinterface']['port']))
+ try:
+ print open(self.path[1:], 'r').read()
+ except IOError:
+ self.send_error(404)
+
+ def format_size(self, size):
+ return str(size / 1024) + " MiB"
+
+ def format_time(self,seconds):
+ seconds = int(seconds)
+ hours, seconds = divmod(seconds, 3600)
+ minutes, seconds = divmod(seconds, 60)
+ return "%.2i:%.2i:%.2i" % (hours, minutes, seconds)
+
+ def get_downloads(self):
+ data = coreserver.status_downloads()
+ for download in data:
+ print "<h3>%s</h3>" % download["name"]
+ if download["status"] == "downloading":
+ percent = download["percent"]
+ z = percent / 4
+ print "<h3>%s</h3>" % dl_name
+ print "<font face='font-family:Fixedsys,Courier,monospace;'>[" + z * "#" + (25-z) * "&nbsp;" + "]</font>" + str(percent) + "%<br />"
+ print "Speed: " + str(int(download['speed'])) + " kb/s"
+ print "Size: " + self.format_size(download['size'])
+ print "Finished in: " + self.format_time(download['eta'])
+ print "ID: " + str(download['id'])
+ dl_status = "[" + z * "#" + (25-z) * " " + "] " + str(percent) + "%" + " Speed: " + str(int(download['speed'])) + " kb/s" + " Size: " + self.format_size(download['size']) + " Finished in: " + self.format_time(download['eta']) + " ID: " + str(download['id'])
+ if download["status"] == "waiting":
+ print "waiting: " + self.format_time(download["wait_until"]- time())
+
+is_url = re.compile("^(((https?|ftp)\:\/\/)?([\w\.\-]+(\:[\w\.\&%\$\-]+)*@)?((([^\s\(\)\<\>\\\"\.\[\]\,@;:]+)(\.[^\s\(\)\<\>\\\"\.\[\]\,@;:]+)*(\.[a-zA-Z]{2,4}))|((([01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([01]?\d{1,2}|2[0-4]\d|25[0-5])))(\b\:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)\b)?((\/[^\/][\w\.\,\?\'\\\/\+&%\$#\=~_\-@]*)*[^\.\,\?\"\'\(\)\[\]!;<>{}\s\x7F-\xFF])?)$",re.IGNORECASE)
+
+coreserver = None
+
+class WebServer():
+
+ def start(self):
try:
- run(host=self.core.config['webinterface']['listenaddr'], port=int(self.core.config['webinterface']['port']), quiet=True)
- except:
- self.core.logger.error("Failed starting webserver, no webinterface available: Can't create socket")
- exit()
+ global coreserver
+ coreserver = ServerProxy("https://testuser:testpw@localhost:1337", allow_none=True)
+ webserver = HTTPServer(('',8080),Handler)
+ print 'server started at port 8080'
+ webserver.serve_forever()
+ except KeyboardInterrupt:
+ webserver.socket.close()
+
+if __name__ == "__main__":
+ web = WebServer()
+ web.start()
+
diff --git a/module/web/main.html b/module/web/main.html
new file mode 100644
index 000000000..87f0d7408
--- /dev/null
+++ b/module/web/main.html
@@ -0,0 +1,29 @@
+<title>pyLoad - Webinterface</title>
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
+ <head>
+ <title>pyLoad - Webinterface</title>
+ <script src="main.js" type="text/javascript"></script>
+ </head>
+ <body>
+ <h1>pyLoad - Webinterface</h1>
+ <div name="status_server">
+ Status: running Speed: 600kb/s Files in queue: 0
+ </div>
+ <div name="actions">
+ <div id="add_urls">
+ <form name="add_urls_form">
+ <input type="text" name="new_url" />
+ <input type="button" value="Add Link" onclick="addUrl(document.add_urls_form.new_url.value)" >
+ </form>
+ </div>
+ (START) (PAUSE) (ADD)
+ </div>
+ <h2>Downloads</h2>
+ <div id="downloads">
+ Lade Downloads
+ </div>
+ </body>
+</html>
+
diff --git a/module/web/main.js b/module/web/main.js
new file mode 100644
index 000000000..a286df991
--- /dev/null
+++ b/module/web/main.js
@@ -0,0 +1,42 @@
+function getXmlHttpRequestObject() {
+ if (window.XMLHttpRequest) {
+ return new XMLHttpRequest(); //Not IE
+ } else if(window.ActiveXObject) {
+ return new ActiveXObject("Microsoft.XMLHTTP"); //IE
+ } else {
+ alert("Your browser doesn't support the XmlHttpRequest object. Better upgrade to Firefox.");
+ }
+}
+var req = getXmlHttpRequestObject();
+
+function getDownloads() {
+ req.onreadystatechange = function() {
+ if (req.readyState == 4) {
+ if(req.status==200) {
+ document.getElementById('downloads').innerHTML = req.responseText;
+ } else {
+ alert("Fehler:\nHTTP-Status: "+req.status+"\nHTTP-Statustext: "+req.statusText);
+ }
+ };
+ }
+ req.open("GET", '/downloads', true);
+ req.send(null);
+}
+
+function addUrl(new_url) {
+ req.onreadystatechange = function() {
+ if (req.readyState == 4) {
+ if(req.status==200) {
+ document.getElementById('add_urls').innerHTML = req.responseText;
+ } else {
+ alert("Fehler:\nHTTP-Status: "+req.status+"\nHTTP-Statustext: "+req.statusText);
+ }
+ };
+ }
+ url = "/add=" + new_url
+ req.open("GET", url, true);
+ req.send(null);
+}
+
+window.setInterval("getDownloads()", 1000);
+