mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Began implementing on-demand websocket server
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import re
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
@@ -14,10 +15,10 @@ class Config:
|
|||||||
"""
|
"""
|
||||||
Initialize main configuration options
|
Initialize main configuration options
|
||||||
"""
|
"""
|
||||||
|
self.root = root
|
||||||
self._fp = "config.json"
|
self._fp = "config.json"
|
||||||
self._cp = pathlib.Path.joinpath(root, self._fp)
|
self._cp = pathlib.Path.joinpath(root, self._fp)
|
||||||
self._data = self.open_file()
|
self._data = self.open_file()
|
||||||
self.root = root
|
|
||||||
self.logger = self.get_logger()
|
self.logger = self.get_logger()
|
||||||
self.book_path = self._data["BOOKPATH"]
|
self.book_path = self._data["BOOKPATH"]
|
||||||
self.TITLE = self._data["TITLE"]
|
self.TITLE = self._data["TITLE"]
|
||||||
@@ -53,5 +54,10 @@ class Config:
|
|||||||
data = json.load(read_file)
|
data = json.load(read_file)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def path(self):
|
||||||
|
rstr = "pyShelf/src"
|
||||||
|
r = re.template(rstr)
|
||||||
|
_pathre = re.match("pyShelf/src")
|
||||||
|
|
||||||
def django_secret(self):
|
def django_secret(self):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import websockets
|
import websockets
|
||||||
|
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
from .library import Catalogue
|
||||||
from .storage import Storage
|
from .storage import Storage
|
||||||
|
|
||||||
|
|
||||||
class InitFiles:
|
class InitFiles:
|
||||||
"""First run file creation operations"""
|
"""First run file creation operations"""
|
||||||
|
|
||||||
def __init__(self, file_array):
|
def __init__(self, file_array):
|
||||||
for _pointer in file_array:
|
for _pointer in file_array:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -35,18 +36,40 @@ class Server:
|
|||||||
:TODO: Document this
|
:TODO: Document this
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
async def __init__(self, root):
|
||||||
self.loop = asyncio.get_event_loop()
|
self.root = root
|
||||||
|
self.config = Config(self.root)
|
||||||
self.instance = None
|
self.instance = None
|
||||||
|
self.serve = await websockets.serve(socketio, "127.0.0.1", 1337)
|
||||||
|
|
||||||
async def entrypoint(self, websocket, path):
|
async def __aexit__(self, *args, **kwargs):
|
||||||
_str = await websocket.recv()
|
await self.serve.__aexit__(*args, **kwargs)
|
||||||
greeting = f"{_str}"
|
|
||||||
await websocket.send(greeting)
|
|
||||||
|
|
||||||
async def start(self):
|
async def runImport(self):
|
||||||
try:
|
_start_time = time.time()
|
||||||
self.instance = await websockets.serve(self.entrypoint, "localhost", 1337)
|
InitFiles(self.config.file_array)
|
||||||
return True
|
_storage = Storage(self.config)
|
||||||
except Exception:
|
_storage.check_ownership()
|
||||||
raise
|
Catalogue(self.config).import_books()
|
||||||
|
_storage.make_collections()
|
||||||
|
_total_time = round(time.time() - _start_time)
|
||||||
|
|
||||||
|
async def socketio(self, websocket, path):
|
||||||
|
async for message in websocket:
|
||||||
|
if message == "ping":
|
||||||
|
config.logger.info("<< Ping")
|
||||||
|
tx = self.pong()
|
||||||
|
elif message == "importBooks":
|
||||||
|
config.logger.info("Starting Import")
|
||||||
|
tx = "Starting Import . . ."
|
||||||
|
await websocket.send(tx)
|
||||||
|
await runImport()
|
||||||
|
tx = "complete"
|
||||||
|
await websocket.send(tx)
|
||||||
|
|
||||||
|
def pong(self):
|
||||||
|
self.config.logger.info(">> Pong")
|
||||||
|
return "pong"
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
asyncio.get_event_loop().run_until_complete(self.serve)
|
||||||
|
|||||||
@@ -172,6 +172,14 @@ $(document).ready(function(){
|
|||||||
});
|
});
|
||||||
$(document).on('click', '.logout-btn', function(){window.location.href = '/logout'});
|
$(document).on('click', '.logout-btn', function(){window.location.href = '/logout'});
|
||||||
$(document).on('click', '.import-btn', async function(){
|
$(document).on('click', '.import-btn', async function(){
|
||||||
|
$.ajax({
|
||||||
|
type: "GET", url: "/live", data: {hook: 'import_books'},
|
||||||
|
success: function (response) {
|
||||||
|
},
|
||||||
|
error: function (response) {
|
||||||
|
customlog(["Failure", response]);
|
||||||
|
}
|
||||||
|
});
|
||||||
let connection = await ImportBooks(server);
|
let connection = await ImportBooks(server);
|
||||||
popover.html('<div id="psout" class="container">');
|
popover.html('<div id="psout" class="container">');
|
||||||
let psout = $('#psout')
|
let psout = $('#psout')
|
||||||
@@ -286,7 +294,7 @@ function OpenSocket(address) {
|
|||||||
ping(connection)
|
ping(connection)
|
||||||
};
|
};
|
||||||
connection.onmessage = function(rcvd){
|
connection.onmessage = function(rcvd){
|
||||||
sock_rx(rcvd)
|
sock_rx(rcvd)
|
||||||
};
|
};
|
||||||
resolve(connection);
|
resolve(connection);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from base64 import b64decode, b64encode
|
from base64 import b64decode, b64encode
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from backend.lib.config import Config
|
from backend.lib.config import Config
|
||||||
|
from backend.lib.pyShelf import Server
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.contrib.auth import authenticate, get_user_model, login, logout
|
from django.contrib.auth import authenticate, get_user_model, login, logout
|
||||||
@@ -23,7 +24,7 @@ from .forms import SignUpForm, UserLoginForm
|
|||||||
from .models import Books, Collections, Favorites, Navigation, User
|
from .models import Books, Collections, Favorites, Navigation, User
|
||||||
|
|
||||||
config = Config(Path("../"))
|
config = Config(Path("../"))
|
||||||
|
server = None
|
||||||
|
|
||||||
|
|
||||||
def index(request, query=None, _set=1, _limit=None, _order='title'):
|
def index(request, query=None, _set=1, _limit=None, _order='title'):
|
||||||
@@ -76,7 +77,7 @@ def userlogin(request):
|
|||||||
username = request.POST['username']
|
username = request.POST['username']
|
||||||
password = request.POST['password']
|
password = request.POST['password']
|
||||||
user = authenticate(request, username=username, password=password)
|
user = authenticate(request, username=username, password=password)
|
||||||
if user is not None:
|
if user is not None:
|
||||||
login(request, user)
|
login(request, user)
|
||||||
user.save()
|
user.save()
|
||||||
return redirect('home')
|
return redirect('home')
|
||||||
@@ -89,8 +90,8 @@ def userlogout(request):
|
|||||||
return redirect('home')
|
return redirect('home')
|
||||||
|
|
||||||
|
|
||||||
def home(request, query=None, _set=1, _limit=None, _order='title'):
|
def home(request, query=None, _set=1, _limit=None, _order='title'):
|
||||||
"""
|
"""
|
||||||
Reset Search Queries & Return Home
|
Reset Search Queries & Return Home
|
||||||
"""
|
"""
|
||||||
_payload = payload(request, query, _set, _limit, _order, reset=True)
|
_payload = payload(request, query, _set, _limit, _order, reset=True)
|
||||||
@@ -148,7 +149,7 @@ def show_collection(request, query, _set, _limit=None, _order='title', **kwargs)
|
|||||||
_bookstats = len(Collections().generic_search(query))
|
_bookstats = len(Collections().generic_search(query))
|
||||||
if (_r_len): _btotal = str(_r_len)
|
if (_r_len): _btotal = str(_r_len)
|
||||||
else: _btotal = str(_bookstats)
|
else: _btotal = str(_bookstats)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
request,
|
request,
|
||||||
"index.html",
|
"index.html",
|
||||||
@@ -235,7 +236,7 @@ def book_set(request, _order, _limit=None, _set=1, _flip=False, **kwargs):
|
|||||||
_set_min = _set_max - _limit
|
_set_min = _set_max - _limit
|
||||||
if _flip:
|
if _flip:
|
||||||
books = BookObject.order_by(_order).reverse()[_set_min:_set_max]
|
books = BookObject.order_by(_order).reverse()[_set_min:_set_max]
|
||||||
else:
|
else:
|
||||||
books = BookObject.order_by(_order)[_set_min:_set_max]
|
books = BookObject.order_by(_order)[_set_min:_set_max]
|
||||||
return mark_favorites(request, books)
|
return mark_favorites(request, books)
|
||||||
|
|
||||||
@@ -410,14 +411,28 @@ def live(request, **kwargs):
|
|||||||
return JsonResponse({"data": html})
|
return JsonResponse({"data": html})
|
||||||
|
|
||||||
elif hook == "import_books":
|
elif hook == "import_books":
|
||||||
|
"""TODO: Spawn websocket server"""
|
||||||
breakpoint()
|
breakpoint()
|
||||||
filename = "../data/{}-{}.sock".format(request.user.username, time.strftime("%H:%M:%S"))
|
###################################################
|
||||||
catalogue = ACatalogue()
|
# async def responder(socket): #
|
||||||
async def responder(socket):
|
# await catalogue.import_books(socket=socket) #
|
||||||
await catalogue.import_books(socket=socket)
|
# return JsonResponse({"data": None}) #
|
||||||
return JsonResponse({"data": filename})
|
# pass #
|
||||||
asyncio.run(responder(filename))
|
# asyncio.run(responder(None)) #
|
||||||
|
###################################################
|
||||||
|
def test_connection(host):
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
|
s.connect(host)
|
||||||
|
s.sendall(b"ping")
|
||||||
|
data = s.recv(1024)
|
||||||
|
return data
|
||||||
|
_host = ("127.0.0.1", 1337)
|
||||||
|
try:
|
||||||
|
_server_response = test_connection(_host)
|
||||||
|
except ConnectionRefusedError as e:
|
||||||
|
config.logger.info(e)
|
||||||
|
if e.errno == 111:
|
||||||
|
request.server = Server(Path.absolute(Path.cwd().parent)).start()
|
||||||
|
|
||||||
else: return JsonResponse(err_txt, status=404)
|
else: return JsonResponse(err_txt, status=404)
|
||||||
|
|
||||||
@@ -494,7 +509,7 @@ def payload(request, query, _set, _limit, _order, **kwargs):
|
|||||||
_r = book_set(request, _order, _limit, _set, False, **kwargs)
|
_r = book_set(request, _order, _limit, _set, False, **kwargs)
|
||||||
else: _r = book_set(request, _order, _limit, _set, True, **kwargs)
|
else: _r = book_set(request, _order, _limit, _set, True, **kwargs)
|
||||||
_r_len, _search = None, None
|
_r_len, _search = None, None
|
||||||
|
|
||||||
_bookstats = Books.objects.all().count()
|
_bookstats = Books.objects.all().count()
|
||||||
if (_r_len): _btotal = str(_r_len)
|
if (_r_len): _btotal = str(_r_len)
|
||||||
else: _btotal = str(_bookstats)
|
else: _btotal = str(_bookstats)
|
||||||
@@ -513,3 +528,6 @@ def payload(request, query, _set, _limit, _order, **kwargs):
|
|||||||
"SearchLen": _r_len,
|
"SearchLen": _r_len,
|
||||||
"Order": _order,
|
"Order": _order,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def start_server(request, **kwargs):
|
||||||
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user