Explorar o código

basic web frontend

David %!s(int64=10) %!d(string=hai) anos
pai
achega
26e1ba6de7
Modificáronse 8 ficheiros con 553 adicións e 1 borrados
  1. 1 1
      bot.py
  2. BIN=BIN
      front/.index.html.swp
  3. 39 0
      front/index.html
  4. BIN=BIN
      front/js/.app.js.swp
  5. 314 0
      front/js/angular.min.js
  6. 40 0
      front/js/app.js
  7. 86 0
      qmanager.py
  8. 73 0
      web.py

+ 1 - 1
bot.py

@@ -146,7 +146,7 @@ class IRCClient():
             if "searchbot" in f.lower():
                 out.append(self.list_books(f))
 
-        self.OUTPUT=out
+        self.OUTPUT=[item for sublist in out for item in sublist]
         self.handle_close()
 
     def list_books(self,f):

BIN=BIN
front/.index.html.swp


+ 39 - 0
front/index.html

@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html ng-app="app">
+	<head>
+		<script src="js/angular.min.js"></script>
+		<script src="js/app.js"></script>
+	</head>
+<body>
+	<div ng-controller="main">
+		<table>
+			<tr>
+				<th>ID</th>
+				<th>Type</th>
+				<th>Query</th>
+				<th>Status</th>
+				<th>Output</th>
+			</tr>
+			<tr ng-repeat="l in list track by l.ID">
+				<td> {{l.ID}} </td>
+				<td> {{l.TYPE}} </td>
+				<td> {{l.QUERY}} </td>
+				<td> {{l.STATUS}} </td>
+				<td> 
+					<div ng-repeat="o in l.OUT">
+						<a ng-click="download(o)"> {{o }}</a>
+					</div>
+				</td>
+			</tr>
+		</table>
+		<div>
+			Search books:
+			Book:<input type="text" ng-model="book">
+			Extension: <input type="text" ng-model="extension">
+			<input type="button" value="search" ng-click="searchBook(book,extension)">
+		</div>
+		<input type="button" value="refresh" ng-click="getList()">
+	</div>
+</body>
+</html>
+

BIN=BIN
front/js/.app.js.swp


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 314 - 0
front/js/angular.min.js


+ 40 - 0
front/js/app.js

@@ -0,0 +1,40 @@
+app=angular.module('app', []);
+
+app.controller('main', function($scope,$http) {
+	$scope.list=[];
+	$scope.book="";
+	$scope.extension="";
+
+	$scope.getList = function() {
+		$http.get("/test/").then(
+		function(data) {
+			$scope.list=data.data;
+		},
+		function(data){
+			console.log("error");
+			console.log(data);
+		});
+	}
+	$scope.getList();
+
+	$scope.download = function(book) {
+		$http.post("/test/?adasasd",{"type":"BOOK","book":book}).then(
+		function(data) {
+			$scope.getList();
+		},
+		function(data){
+			console.log("error");
+			console.log(data);
+		});
+	};
+	$scope.searchbook = function(book,extension){
+		$http.post("/test/?ewqewqewq", {"type":"search","book":book,"extension":extension}).then(
+		function(data) {
+			console.log("success");
+			$scope.getlist();
+		},
+		function(data){
+			console.log("err");
+		});
+	};
+});

+ 86 - 0
qmanager.py

@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+
+from  bot import IRCClient
+import queue
+import shlex
+import asyncore
+import datetime
+from time import strftime,time
+
+class qManager:
+
+    tasks = []
+    last_id=0
+
+
+    def new_dl(self,string):
+        self.new_task(string,IRCClient(string,"","BOOK",logging=False))
+
+    def new_search(self,keywords,format):
+        self.new_task(keywords,IRCClient(keywords,format,"SEARCH",logging=False))
+
+    def new_task(self,query,t):
+        nt = task(query,t,self.last_id)
+        self.last_id+=1
+        self.tasks.append(nt)
+
+    def task_status(self):
+        ret=[]
+        for t in self.tasks:
+            elapsed= time()-t.START_TIME
+            ret.append({"ID": t.ID,
+                "STATUS": t.get_status(),
+                "OUT": t.get_output(),
+                "ELAPSED": int(elapsed),
+                "QUERY": t.QUERY,
+                "TYPE": t.get_type()})
+
+        return ret
+class task:
+    CUR_STATUS=""
+    OUTPUT=""
+    ID=0
+    CLIENT=None
+    START_TIME=None
+    QUERY=""
+    def __init__(self,query,c,id):
+        self.QUERY=query
+        self.CLIENT=c
+        self.ID=id
+        self.START_TIME=time()
+        pass
+
+    def get_status(self):
+       self.OUTPUT=self.CLIENT.OUTPUT
+       return self.CLIENT.STATUS 
+
+    def get_type(self):
+       return self.CLIENT.TYPE
+    def get_output(self):
+       return self.OUTPUT
+
+
+
+
+#CLI INTERFACE
+if __name__ == "__main__":
+    q = qManager()
+    while True:
+        line=input()
+        words=shlex.split(line)
+        print(q.task_status())
+        if len(words)<2:
+            continue
+        comm=words[0]
+        if comm.lower()=="quit":
+            break
+        if comm.lower()=="search":
+            if len(words)<3:
+                continue
+            q.new_search(words[1],words[2])
+            continue
+        if comm.lower()=="dl":
+            q.new_dl(words[1])
+            continue
+    
+        print("search <'multiple keywords'> <format>|dl <line>|quit")

+ 73 - 0
web.py

@@ -0,0 +1,73 @@
+import os
+import json
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from qmanager import qManager
+
+# Port on which server will run.
+PORT = 8080
+
+
+class HTTPRequestHandler(BaseHTTPRequestHandler):
+    q = qManager()
+
+    def do_GET(self):
+        self.send_200()
+        out=json.dumps(self.q.task_status())
+        #self.wfile.write(bytes()) #fileHandle.read().encode()
+        self.wfile.write(bytes(out,"utf-8")) #fileHandle.read().encode()
+
+    def send_400(self):
+        self.send_response(400, 'NOT OK')
+        self.send_header('Content-type', 'text/json')
+        self.end_headers()
+
+    def send_200(self):
+        self.send_response(200)
+        self.send_header('Content-type', 'text/json')
+        self.end_headers()
+
+    def do_POST(self):
+        # Check if path is there.
+        if self.path:
+            length = self.headers['content-length']
+            if length is None:
+               self.send_400()
+               return
+
+
+            data = self.rfile.read(int(length))
+            data = data.decode("utf-8")
+            try:
+                d=json.loads(data)
+            except:
+                print("no json")
+                self.send_400()
+                return
+
+            if not "type" in d or not "book" in d:
+                self.send_400()
+                return
+            if d["type"].upper()=="SEARCH":
+                self.q.new_search(d["book"],d["extension"])
+                self.send_200()
+                return
+
+            if d["type"]=="BOOK":
+                self.q.new_dl(d["book"])
+                self.send_200()
+                return
+
+
+if __name__ == '__main__':
+
+    HTTPDeamon = HTTPServer(('', PORT), HTTPRequestHandler)
+
+    print("Listening at port", PORT)
+
+    try:
+        HTTPDeamon.serve_forever()
+    except KeyboardInterrupt:
+        pass
+
+    HTTPDeamon.server_close()
+    print("Server stopped")