mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-19 08:12:09 +00:00
Merge branch 'main' of https://github.com/migatu/conjurer
This commit is contained in:
@@ -18,13 +18,14 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import uuid
|
import uuid
|
||||||
|
import threading
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from logging import handlers
|
from logging import handlers
|
||||||
from pathlib import Path, PurePath
|
from pathlib import Path, PurePath
|
||||||
from platform import uname
|
from platform import uname
|
||||||
from sys import platform
|
from sys import platform
|
||||||
from typing import List, Optional, TypedDict
|
from typing import List, Optional, TypedDict
|
||||||
|
from queue import Queue, Empty
|
||||||
# *==============Imported libraries
|
# *==============Imported libraries
|
||||||
import discord
|
import discord
|
||||||
import eyed3
|
import eyed3
|
||||||
@@ -42,6 +43,11 @@ import yt_dlp
|
|||||||
from spotify_dl import spotify
|
from spotify_dl import spotify
|
||||||
from spotify_dl import youtube as youtube_download
|
from spotify_dl import youtube as youtube_download
|
||||||
|
|
||||||
|
from flask import Flask, jsonify, request
|
||||||
|
from waitress import serve
|
||||||
|
|
||||||
|
|
||||||
|
COMM_Q = Queue()
|
||||||
Music_Config = TypedDict(
|
Music_Config = TypedDict(
|
||||||
"Music_Config",
|
"Music_Config",
|
||||||
{
|
{
|
||||||
@@ -50,6 +56,15 @@ Music_Config = TypedDict(
|
|||||||
"requester": List[str],
|
"requester": List[str],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
class QueryControl():
|
||||||
|
def __init__(self, query_author, query_uuid, query_content, uplogger) -> None:
|
||||||
|
self.author = query_author
|
||||||
|
self.uuid = query_uuid
|
||||||
|
self.content = query_content
|
||||||
|
self.logger = uplogger
|
||||||
|
self.logger.info(f"Created Query control for {self.author}, {self.uuid}: {self.content}")
|
||||||
|
self.replies = []
|
||||||
|
|
||||||
|
|
||||||
# *=========================================== Predefines
|
# *=========================================== Predefines
|
||||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||||
@@ -77,6 +92,8 @@ LIBRARIAN_SERVICE_ADDRESS = "http://192.168.1.192:5001"
|
|||||||
SEND_QUERY = "/query"
|
SEND_QUERY = "/query"
|
||||||
TIME_BETWEEN_CALLS = 100000
|
TIME_BETWEEN_CALLS = 100000
|
||||||
LAST_SPONTANEOUS_CALL = datetime.now()
|
LAST_SPONTANEOUS_CALL = datetime.now()
|
||||||
|
HOST_ADDRESS = "http://192.168.1.192"
|
||||||
|
PORT_ADDRESS = "5000"
|
||||||
|
|
||||||
# *=========================================== Platform Specific Predefines
|
# *=========================================== Platform Specific Predefines
|
||||||
|
|
||||||
@@ -265,9 +282,10 @@ logger.info("Done")
|
|||||||
logger.info("Creating discord bot")
|
logger.info("Creating discord bot")
|
||||||
client = commands.Bot(intents=intents, command_prefix="$")
|
client = commands.Bot(intents=intents, command_prefix="$")
|
||||||
logger.info("Done")
|
logger.info("Done")
|
||||||
|
logger.info("Creating flask app")
|
||||||
|
app = Flask(__name__)
|
||||||
|
logger.info("Done")
|
||||||
# *=========================================== Define Events
|
# *=========================================== Define Events
|
||||||
|
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
"""Metoda wywoływana przy połączeniu do serwera."""
|
"""Metoda wywoływana przy połączeniu do serwera."""
|
||||||
@@ -2022,6 +2040,9 @@ async def wyszukaj_linki_do_dokumentow(ctx):
|
|||||||
coroutine = asyncio.to_thread(requests.post, f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}", json=json_query, timeout=360)
|
coroutine = asyncio.to_thread(requests.post, f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}", json=json_query, timeout=360)
|
||||||
#query_response = await requests.post(f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}", json=json_query, timeout=360)
|
#query_response = await requests.post(f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}", json=json_query, timeout=360)
|
||||||
query_response = await coroutine
|
query_response = await coroutine
|
||||||
|
#TODO: tutaj jest informacja UUID o requescie zapisywana w queue i konczymy
|
||||||
|
#TODO: response'y idą do serwera, serwer sobie je mieli i potem wysyła do nas posta albo response
|
||||||
|
|
||||||
if not query_response.status_code == 200:
|
if not query_response.status_code == 200:
|
||||||
await ctx.send("Wołaj szefa - coś się wyjebało")
|
await ctx.send("Wołaj szefa - coś się wyjebało")
|
||||||
return
|
return
|
||||||
@@ -2085,12 +2106,49 @@ async def krecimy_pornola(ctx):
|
|||||||
else:
|
else:
|
||||||
ctx.reply("Jak ładnie szefa poprosisz.")
|
ctx.reply("Jak ładnie szefa poprosisz.")
|
||||||
|
|
||||||
|
@app.route("/", methods=["POST"])
|
||||||
|
def answer_external_command():
|
||||||
|
record = json.loads(request.data)
|
||||||
|
logger.info(record)
|
||||||
|
answer_data = None
|
||||||
|
if "UUID" in record and "QUERY" in record and "AUTHOR" in record:
|
||||||
|
answer_data = "Query logged"
|
||||||
|
return_data = (
|
||||||
|
jsonify(isError=False, message="Success", statusCode=200, data=answer_data),
|
||||||
|
200,
|
||||||
|
)
|
||||||
|
return return_data
|
||||||
|
|
||||||
|
def flask_debug():
|
||||||
|
"""
|
||||||
|
The `flask_debug` function starts a Flask application in debug mode without using the reloader.
|
||||||
|
Do not use for production for fucks sake.
|
||||||
|
"""
|
||||||
|
# trunk-ignore(bandit/B201)
|
||||||
|
app.run(debug=True, use_reloader=False, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
||||||
|
|
||||||
|
def waitress_run():
|
||||||
|
"""
|
||||||
|
The `waitress_run` function serves the `app` on host "0.0.0.0"
|
||||||
|
and port 5000 using the Waitress WSGI server.
|
||||||
|
"""
|
||||||
|
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
||||||
|
|
||||||
|
|
||||||
# *================================== Run
|
# *================================== Run
|
||||||
|
if __name__ == "__main__":
|
||||||
|
#TODO: wpakowac kondzia w wąteki odpalic asynchronicznie rownolegle z flaskiem do sterowania nim
|
||||||
logger.info("Starting discord bot")
|
logger.info("Starting discord bot")
|
||||||
client.run(TOKEN)
|
threads = []
|
||||||
|
threads.append(threading.Thread(target=client.run, args=(TOKEN,)))
|
||||||
|
for worker in threads:
|
||||||
|
worker.start()
|
||||||
|
for worker in threads:
|
||||||
|
worker.join()
|
||||||
|
|
||||||
#widok - generuje się 5 stron po 4 linki
|
#widok - generuje się 5 stron po 4 linki
|
||||||
# odśwież - zamienić na "dalej" - zaciąga kolejne 20 rezultatów
|
# odśwież - zamienić na "dalej" - zaciąga kolejne 20 rezultatów
|
||||||
|
# po wyslaniu czekamy na odbior i dodajemy UUID oraz nick poszukiwacza do queue ktora jest sprawdzana
|
||||||
|
# flask sobie chodzi i jak dostaje posta z wynikami to wyciaga z queue UUID i odpowiada że znalazła
|
||||||
|
# po samym wyszukaniu dajemy info że "Zaczynam szukać ale pare godzin to potrwa."
|
||||||
|
# OPCJONALNIE: Dosyłamy znalezione wyniki na bieżąco - wtedy wkładamy do queue dla UUID wyniki
|
||||||
@@ -199,7 +199,3 @@ if __name__ == "__main__":
|
|||||||
worker.start()
|
worker.start()
|
||||||
for worker in threads:
|
for worker in threads:
|
||||||
worker.join()
|
worker.join()
|
||||||
|
|
||||||
#1. robi liste wyników i ją wyświetla - ale tylko tytuły niezależnych dzieł - jeśli jest coś w containerze to trza pogrupować
|
|
||||||
#2. Tworzy przyciski pozwalające sprawdzić czy jest dostępne - i jeśli jest zwraca link
|
|
||||||
#3. Jest też przycisk "more" - wyszukujemy po 100, wyświetlamy po 20. Po 100 jak jeszcze to dopiero odpytujemy crossref przyciski strzałek przesuwają po obecnej porcji danych
|
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ ENCODING = "utf-8"
|
|||||||
CHUNk = "_chunk.txt"
|
CHUNk = "_chunk.txt"
|
||||||
_sentinel = object()
|
_sentinel = object()
|
||||||
|
|
||||||
|
#TODO: Doisy wchodzą jako lista krotek (doi, False) - po znalezieniu zaznaczamy na True i potem już nie porównujemy
|
||||||
|
#TODO: lista krotek - jeśli wszystkie mają True dajemy terma
|
||||||
|
|
||||||
def producer(out_q, control_q, filename):
|
def producer(out_q, control_q, filename):
|
||||||
with open(DATABASE_PATH + filename, "r+", encoding=ENCODING) as operated_file:
|
with open(DATABASE_PATH + filename, "r+", encoding=ENCODING) as operated_file:
|
||||||
lines = operated_file.readlines()
|
lines = operated_file.readlines()
|
||||||
@@ -29,6 +32,7 @@ def producer(out_q, control_q, filename):
|
|||||||
|
|
||||||
|
|
||||||
#IMPORTANT!!! ONLY ONE CONSUMER THREAD AS WE ARE NOT PUTTING SENTINELS BACK
|
#IMPORTANT!!! ONLY ONE CONSUMER THREAD AS WE ARE NOT PUTTING SENTINELS BACK
|
||||||
|
#TODO: Change doi to tuple list and only put term when all doi are found => (doi, True)
|
||||||
def consumer(in_q, control_q, doi):
|
def consumer(in_q, control_q, doi):
|
||||||
sentinels = 0
|
sentinels = 0
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Reference in New Issue
Block a user