mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-15 22:32:10 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef6eb84921 | |||
| f9a1e7c03a | |||
| f9581fa24b | |||
| 0473159b94 | |||
| 81a25b8c56 | |||
| 8e5e4ce530 | |||
| 92940a4d46 | |||
| e6d3492790 | |||
| d6b33cc614 | |||
| b107f01208 | |||
| 22b33fa984 | |||
| f6ccdb3e34 | |||
| 1f271b4c71 | |||
| f9ad679833 |
@@ -1,2 +1,23 @@
|
|||||||
# conjurer
|
# conjurer
|
||||||
Discord.py bot for fun, sex and BDSM
|
Discord.py bot for fun, sex and BDSM
|
||||||
|
|
||||||
|
## Docker quick start
|
||||||
|
|
||||||
|
Docker definitions live at the repository root and let you run the Discord bot
|
||||||
|
(`thin_client.py`), the musician file service, and the librarian search worker as
|
||||||
|
separate containers.
|
||||||
|
|
||||||
|
1. **Prepare configuration**
|
||||||
|
- Edit the environment files under `docker/env/*.env` and replace the
|
||||||
|
placeholders (`replace-me`, `shared-secret`, etc.) with your real tokens.
|
||||||
|
Keep `CONJURER_API_KEY` identical across all services.
|
||||||
|
2. **Create host directories** listed in `docker-compose.yml` (for example
|
||||||
|
`docker/volumes/bot/config`, `docker/volumes/musician/data`, …) and populate
|
||||||
|
them with the required JSON settings or media assets.
|
||||||
|
3. **Launch the stack**
|
||||||
|
```bash
|
||||||
|
docker compose up --build
|
||||||
|
```
|
||||||
|
|
||||||
|
Logs for each container are mounted under `docker/volumes/*` so you can diff the
|
||||||
|
runtime JSON and log files in git if needed.
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import discord
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
|
|
||||||
from ai_functions import get_random_cyclic_message
|
from conjurer.backup_old_docker.ai_functions import get_random_cyclic_message
|
||||||
from constants import (
|
from conjurer.backup_old_docker.constants import (
|
||||||
ENCODING,
|
ENCODING,
|
||||||
LAST_SPONTANEOUS_CALL,
|
LAST_SPONTANEOUS_CALL,
|
||||||
LOGFILE,
|
LOGFILE,
|
||||||
|
|||||||
+3
-3
@@ -10,11 +10,11 @@ import discord
|
|||||||
import openai
|
import openai
|
||||||
import requests
|
import requests
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from other_functions import discord_friendly_send, discord_friendly_reply
|
from conjurer.backup_old_docker.other_functions import discord_friendly_send, discord_friendly_reply
|
||||||
|
|
||||||
|
|
||||||
import ai_functions
|
import conjurer.backup_old_docker.ai_functions as ai_functions
|
||||||
from constants import (
|
from conjurer.backup_old_docker.constants import (
|
||||||
ASSISTANTS,
|
ASSISTANTS,
|
||||||
DATA,
|
DATA,
|
||||||
GRAPHICS_PATH,
|
GRAPHICS_PATH,
|
||||||
|
|||||||
+2
-2
@@ -6,8 +6,8 @@ import random
|
|||||||
import openai
|
import openai
|
||||||
import tiktoken
|
import tiktoken
|
||||||
import time
|
import time
|
||||||
from other_functions import discord_friendly_send
|
from conjurer.backup_old_docker.other_functions import discord_friendly_send
|
||||||
from constants import (
|
from conjurer.backup_old_docker.constants import (
|
||||||
ASSISTANTS,
|
ASSISTANTS,
|
||||||
CYCLIC_WORDS,
|
CYCLIC_WORDS,
|
||||||
ENCODING,
|
ENCODING,
|
||||||
|
|||||||
+43
-10
@@ -1,12 +1,14 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from queue import Empty, Queue
|
from queue import Empty, Queue
|
||||||
|
from typing import Optional
|
||||||
from urllib import request as urequest
|
from urllib import request as urequest
|
||||||
|
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, abort, jsonify, request
|
||||||
from waitress import serve
|
from waitress import serve
|
||||||
|
|
||||||
HOST_ADDRESS = "192.168.1.191"
|
HOST_ADDRESS = "192.168.1.191"
|
||||||
@@ -31,6 +33,13 @@ PREPPED_TRACKS = {
|
|||||||
}
|
}
|
||||||
logger = logging.getLogger("discord")
|
logger = logging.getLogger("discord")
|
||||||
|
|
||||||
|
API_KEY = os.getenv("CONJURER_API_KEY")
|
||||||
|
|
||||||
|
|
||||||
|
def _authorize_request() -> None:
|
||||||
|
if API_KEY and request.headers.get("X-Conjurer-Api-Key") != API_KEY:
|
||||||
|
abort(401)
|
||||||
|
|
||||||
|
|
||||||
class QueryControl:
|
class QueryControl:
|
||||||
"""
|
"""
|
||||||
@@ -53,6 +62,7 @@ class QueryControl:
|
|||||||
|
|
||||||
@app.route("/prepped_tracks", methods=["POST"])
|
@app.route("/prepped_tracks", methods=["POST"])
|
||||||
def log_radio_tracks():
|
def log_radio_tracks():
|
||||||
|
_authorize_request()
|
||||||
app.logger = logging.getLogger("discord")
|
app.logger = logging.getLogger("discord")
|
||||||
|
|
||||||
app.logger.info(request)
|
app.logger.info(request)
|
||||||
@@ -79,6 +89,7 @@ def log_radio_tracks():
|
|||||||
|
|
||||||
@app.route("/conjurer", methods=["POST"])
|
@app.route("/conjurer", methods=["POST"])
|
||||||
def answer_external_command():
|
def answer_external_command():
|
||||||
|
_authorize_request()
|
||||||
"""
|
"""
|
||||||
The function `answer_external_command` logs the request data, loads the data as JSON, logs the
|
The function `answer_external_command` logs the request data, loads the data as JSON, logs the
|
||||||
record, and then puts the record into an incoming queue before returning a success message.
|
record, and then puts the record into an incoming queue before returning a success message.
|
||||||
@@ -129,7 +140,7 @@ def waitress_run():
|
|||||||
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
||||||
|
|
||||||
|
|
||||||
def scan_queue():
|
def scan_queue(stop_event: Optional[threading.Event] = None):
|
||||||
"""
|
"""
|
||||||
The function `scan_queue` reads data from a queue, logs it, and appends it to another queue.
|
The function `scan_queue` reads data from a queue, logs it, and appends it to another queue.
|
||||||
|
|
||||||
@@ -140,12 +151,18 @@ def scan_queue():
|
|||||||
"""
|
"""
|
||||||
logger = logging.getLogger("discord")
|
logger = logging.getLogger("discord")
|
||||||
while True:
|
while True:
|
||||||
data = OUT_COMM_Q.get()
|
if stop_event and stop_event.is_set():
|
||||||
|
logger.info("scan_queue: stop requested")
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
data = OUT_COMM_Q.get(timeout=1)
|
||||||
|
except Empty:
|
||||||
|
continue
|
||||||
logger.info(data)
|
logger.info(data)
|
||||||
awaiting_q.append(data)
|
awaiting_q.append(data)
|
||||||
|
|
||||||
|
|
||||||
def scan_incoming():
|
def scan_incoming(stop_event: Optional[threading.Event] = None):
|
||||||
"""
|
"""
|
||||||
The `scan_incoming` function continuously checks for incoming data, processes it, and logs when data
|
The `scan_incoming` function continuously checks for incoming data, processes it, and logs when data
|
||||||
is found.
|
is found.
|
||||||
@@ -157,6 +174,9 @@ def scan_incoming():
|
|||||||
"""
|
"""
|
||||||
logger = logging.getLogger("discord")
|
logger = logging.getLogger("discord")
|
||||||
while True:
|
while True:
|
||||||
|
if stop_event and stop_event.is_set():
|
||||||
|
logger.info("scan_incoming: stop requested")
|
||||||
|
break
|
||||||
try:
|
try:
|
||||||
answer = incoming_q.get(block=False)
|
answer = incoming_q.get(block=False)
|
||||||
logger.info("DATA FOUND")
|
logger.info("DATA FOUND")
|
||||||
@@ -204,7 +224,7 @@ def id3(url: str) -> dict:
|
|||||||
return tagdata
|
return tagdata
|
||||||
|
|
||||||
|
|
||||||
def comm_subroutine():
|
def comm_subroutine(stop_event: Optional[threading.Event] = None):
|
||||||
"""
|
"""
|
||||||
The `comm_subroutine` function starts multiple threads to run different tasks concurrently.
|
The `comm_subroutine` function starts multiple threads to run different tasks concurrently.
|
||||||
|
|
||||||
@@ -217,14 +237,27 @@ def comm_subroutine():
|
|||||||
logger.info("Started comms")
|
logger.info("Started comms")
|
||||||
threads = []
|
threads = []
|
||||||
# threads.append(threading.Thread(target=flask_debug))
|
# threads.append(threading.Thread(target=flask_debug))
|
||||||
threads.append(threading.Thread(target=waitress_run))
|
threads.append(
|
||||||
threads.append(threading.Thread(target=scan_queue))
|
threading.Thread(target=waitress_run, daemon=True)
|
||||||
threads.append(threading.Thread(target=scan_incoming))
|
)
|
||||||
|
threads.append(
|
||||||
|
threading.Thread(target=scan_queue, kwargs={"stop_event": stop_event}, daemon=True)
|
||||||
|
)
|
||||||
|
threads.append(
|
||||||
|
threading.Thread(target=scan_incoming, kwargs={"stop_event": stop_event}, daemon=True)
|
||||||
|
)
|
||||||
|
|
||||||
for worker in threads:
|
for worker in threads:
|
||||||
worker.start()
|
worker.start()
|
||||||
for worker in threads:
|
|
||||||
worker.join()
|
try:
|
||||||
|
while any(thread.is_alive() for thread in threads):
|
||||||
|
if stop_event and stop_event.is_set():
|
||||||
|
break
|
||||||
|
time.sleep(0.5)
|
||||||
|
finally:
|
||||||
|
if stop_event:
|
||||||
|
stop_event.set()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -16,34 +16,52 @@ Functions:
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import netrc
|
import os
|
||||||
import threading
|
import threading
|
||||||
import time
|
|
||||||
from json.decoder import JSONDecodeError
|
from json.decoder import JSONDecodeError
|
||||||
from logging import handlers
|
from logging import handlers
|
||||||
|
from pathlib import Path
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import scrape_bot
|
import scrape_bot
|
||||||
import search_bot
|
import search_bot
|
||||||
#import search_bot2 as search_bot
|
# import search_bot2 as search_bot
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request, abort
|
||||||
from habanero import Crossref
|
from habanero import Crossref
|
||||||
from waitress import serve
|
from waitress import serve
|
||||||
|
|
||||||
|
try:
|
||||||
|
import netrc
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
|
netrc = None
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
NETRC_FILE = r"C:\Users\Activcom.pl\.netrc"
|
|
||||||
HOST_ADDRESS = "192.168.1.192"
|
|
||||||
PORT_ADDRESS = 5001
|
|
||||||
MAIN_BOT_ADDRESS = "http://192.168.1.191:5000"
|
|
||||||
SEND_RESULTS = "/conjurer"
|
|
||||||
BDSM_UUID_TEST = "96b7f85a-1142-4908-8986-62a2ea25a147"
|
|
||||||
|
|
||||||
MAX_CR_RESULTS = 500
|
|
||||||
#TEST PURPOSES ONLY!
|
|
||||||
#MAX_CR_RESULTS = 5
|
|
||||||
|
|
||||||
ENCODING = "utf-8"
|
def _env(name: str, default: str) -> str:
|
||||||
|
return os.getenv(name, default)
|
||||||
|
|
||||||
|
|
||||||
|
def _env_path(name: str, default: str) -> Path:
|
||||||
|
return Path(os.getenv(name, default)).expanduser().resolve()
|
||||||
|
|
||||||
|
|
||||||
|
BASE_DIR = Path(
|
||||||
|
os.getenv("CONJURER_LIBRARIAN_BASE", str(Path(__file__).resolve().parent))
|
||||||
|
)
|
||||||
|
NETRC_FILE = _env_path("CONJURER_NETRC_FILE", str(Path.home() / ".netrc"))
|
||||||
|
HOST_ADDRESS = _env("CONJURER_LIBRARIAN_HOST", "0.0.0.0")
|
||||||
|
PORT_ADDRESS = int(_env("CONJURER_LIBRARIAN_PORT", "5001"))
|
||||||
|
MAIN_BOT_ADDRESS = _env("CONJURER_MAIN_BOT", "http://127.0.0.1:5000")
|
||||||
|
SEND_RESULTS = _env("CONJURER_LIBRARIAN_RESULTS_ENDPOINT", "/conjurer")
|
||||||
|
MAX_CR_RESULTS = int(_env("CONJURER_LIBRARIAN_MAX_RESULTS", "500"))
|
||||||
|
ENCODING = _env("CONJURER_ENCODING", "utf-8")
|
||||||
|
API_KEY = os.getenv("CONJURER_API_KEY")
|
||||||
|
LOGFILE_PATH = _env_path(
|
||||||
|
"CONJURER_LIBRARIAN_LOG", str(BASE_DIR / "librarian.log")
|
||||||
|
)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -51,6 +69,17 @@ librarian_queue = Queue()
|
|||||||
librarian_list = []
|
librarian_list = []
|
||||||
|
|
||||||
|
|
||||||
|
def _service_headers() -> Dict[str, str]:
|
||||||
|
if API_KEY:
|
||||||
|
return {"X-Conjurer-Api-Key": API_KEY}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def _authorize_request() -> None:
|
||||||
|
if API_KEY and request.headers.get("X-Conjurer-Api-Key") != API_KEY:
|
||||||
|
abort(401)
|
||||||
|
|
||||||
|
|
||||||
# trunk-ignore(pylint/R0902)
|
# trunk-ignore(pylint/R0902)
|
||||||
class Librarian(object):
|
class Librarian(object):
|
||||||
"""
|
"""
|
||||||
@@ -81,11 +110,24 @@ class Librarian(object):
|
|||||||
- search_result_from_cr: A dictionary to store the search results from Crossref.
|
- search_result_from_cr: A dictionary to store the search results from Crossref.
|
||||||
- done: A flag indicating if the search is done.
|
- done: A flag indicating if the search is done.
|
||||||
"""
|
"""
|
||||||
netrc_mod = netrc.netrc(NETRC_FILE)
|
mailto_contact: Optional[str] = os.getenv("CONJURER_CROSSREF_MAILTO")
|
||||||
|
if netrc:
|
||||||
|
try:
|
||||||
|
netrc_mod = netrc.netrc(str(NETRC_FILE))
|
||||||
auth_tokens = netrc_mod.authenticators("crossref")
|
auth_tokens = netrc_mod.authenticators("crossref")
|
||||||
|
if auth_tokens:
|
||||||
|
mailto_contact = auth_tokens[0]
|
||||||
|
except (FileNotFoundError, netrc.NetrcParseError):
|
||||||
|
logging.getLogger("conjurer_librarian").warning(
|
||||||
|
"Crossref credentials missing in netrc %s", NETRC_FILE
|
||||||
|
)
|
||||||
|
if not mailto_contact:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Crossref credentials not configured. Set CONJURER_CROSSREF_MAILTO or add to netrc."
|
||||||
|
)
|
||||||
self.cr = Crossref(
|
self.cr = Crossref(
|
||||||
mailto=auth_tokens[0],
|
mailto=mailto_contact,
|
||||||
ua_string=f"Conjurer project. mailto:{auth_tokens[0]}"
|
ua_string=f"Conjurer project. mailto:{mailto_contact}"
|
||||||
)
|
)
|
||||||
self.query = query
|
self.query = query
|
||||||
self.uuid = str(uuid)
|
self.uuid = str(uuid)
|
||||||
@@ -132,7 +174,7 @@ class Librarian(object):
|
|||||||
self.fetched = len(cr_result["message"]["items"])
|
self.fetched = len(cr_result["message"]["items"])
|
||||||
self.app.logger.info(self.total)
|
self.app.logger.info(self.total)
|
||||||
self.app.logger.info(self.fetched)
|
self.app.logger.info(self.fetched)
|
||||||
time.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
cr_result = self.cr.works(query=query, cursor_max=15000, cursor='*', progress_bar = True)
|
cr_result = self.cr.works(query=query, cursor_max=15000, cursor='*', progress_bar = True)
|
||||||
@@ -411,18 +453,20 @@ class BackgroundTaskSearch(threading.Thread):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{MAIN_BOT_ADDRESS}{SEND_RESULTS}",
|
f"{MAIN_BOT_ADDRESS}{SEND_RESULTS}",
|
||||||
json=result,
|
json=result,
|
||||||
|
headers=_service_headers(),
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
self.app.logger.info("SENT")
|
self.app.logger.info("SENT")
|
||||||
result = await coroutine
|
result = await coroutine
|
||||||
self.app.logger.info(result.status_code)
|
self.app.logger.info(result.status_code)
|
||||||
self.app.logger.info("SEND CONFIRMED")
|
self.app.logger.info("SEND CONFIRMED")
|
||||||
time.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
# ==================================SERVER ROUTES==========================================
|
# ==================================SERVER ROUTES==========================================
|
||||||
@app.route("/query", methods=["POST"])
|
@app.route("/query", methods=["POST"])
|
||||||
async def query_database():
|
async def query_database():
|
||||||
|
_authorize_request()
|
||||||
"""
|
"""
|
||||||
Endpoint for querying the database.
|
Endpoint for querying the database.
|
||||||
|
|
||||||
@@ -455,6 +499,7 @@ async def query_database():
|
|||||||
|
|
||||||
@app.route("/get_partial_result", methods=["POST"])
|
@app.route("/get_partial_result", methods=["POST"])
|
||||||
async def get_partial():
|
async def get_partial():
|
||||||
|
_authorize_request()
|
||||||
"""
|
"""
|
||||||
Retrieves the partial result for a given UUID.
|
Retrieves the partial result for a given UUID.
|
||||||
|
|
||||||
@@ -478,9 +523,10 @@ async def get_partial():
|
|||||||
# =======================================MAIN===================================================
|
# =======================================MAIN===================================================
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.logger.setLevel(logging.DEBUG)
|
app.logger.setLevel(logging.DEBUG)
|
||||||
|
LOGFILE_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||||
h1 = handlers.RotatingFileHandler(
|
h1 = handlers.RotatingFileHandler(
|
||||||
filename="D:\\logs\\librarian.log",
|
filename=str(LOGFILE_PATH),
|
||||||
encoding="utf-8",
|
encoding=ENCODING,
|
||||||
mode="a",
|
mode="a",
|
||||||
maxBytes=6 * 1024 * 1024,
|
maxBytes=6 * 1024 * 1024,
|
||||||
backupCount=6,
|
backupCount=6,
|
||||||
@@ -488,20 +534,24 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
app.logger.addHandler(h1)
|
app.logger.addHandler(h1)
|
||||||
threads = []
|
threads = []
|
||||||
threads.append(threading.Thread(target=waitress_run))
|
threads.append(threading.Thread(target=waitress_run, daemon=True))
|
||||||
# threads.append(threading.Thread(target=flask_debug))
|
# threads.append(threading.Thread(target=flask_debug))
|
||||||
bgtask = BackgroundTaskSearch()
|
bgtask = BackgroundTaskSearch()
|
||||||
bgtask.app = app
|
bgtask.app = app
|
||||||
|
bgtask.daemon = True
|
||||||
threads.append(bgtask)
|
threads.append(bgtask)
|
||||||
threads.append(threading.Thread(target=scrape_bot.scraper, args=(app.logger,)))
|
threads.append(
|
||||||
|
threading.Thread(
|
||||||
|
target=scrape_bot.scraper, args=(app.logger,), daemon=True
|
||||||
|
)
|
||||||
|
)
|
||||||
i = 0
|
i = 0
|
||||||
for worker in threads:
|
|
||||||
try:
|
try:
|
||||||
|
for worker in threads:
|
||||||
app.logger.info("App number: %s", i)
|
app.logger.info("App number: %s", i)
|
||||||
i += 1
|
i += 1
|
||||||
worker.start()
|
worker.start()
|
||||||
except RuntimeError as e:
|
|
||||||
app.logger.error("Exploded")
|
|
||||||
print(str(e))
|
|
||||||
for worker in threads:
|
for worker in threads:
|
||||||
worker.join()
|
worker.join()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
app.logger.info("Shutdown requested - exiting librarian service")
|
||||||
|
|||||||
@@ -11,17 +11,15 @@ import random
|
|||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# from flask_autoindex import AutoIndex
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from logging import handlers
|
from logging import handlers
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from platform import uname
|
from typing import Dict, List
|
||||||
from sys import platform
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from flask import (
|
from flask import (
|
||||||
Flask,
|
Flask,
|
||||||
|
abort,
|
||||||
jsonify,
|
jsonify,
|
||||||
redirect,
|
redirect,
|
||||||
render_template,
|
render_template,
|
||||||
@@ -29,37 +27,99 @@ from flask import (
|
|||||||
send_from_directory,
|
send_from_directory,
|
||||||
)
|
)
|
||||||
from waitress import serve
|
from waitress import serve
|
||||||
|
|
||||||
import media_search_functions
|
import media_search_functions
|
||||||
|
|
||||||
|
|
||||||
MAIN_BOT_ADDRESS = "http://192.168.1.191:5000"
|
def _env(name: str, default: str) -> str:
|
||||||
MUSIC_TRACKER = "/prepped_tracks"
|
return os.getenv(name, default)
|
||||||
HOST_ADDRESS = "192.168.1.15"
|
|
||||||
PORT_ADDRESS = 5000
|
|
||||||
if platform in ("linux", "linux2"):
|
|
||||||
SEPARATOR_FILE_PATH = "/"
|
|
||||||
if "microsoft-standard" in uname().release:
|
|
||||||
LOGFILE = "/home/mtuszowski/conjurer/discord_mus_service.log"
|
|
||||||
MUSIC_FOLDER = "/mnt/g/Muzyka/"
|
|
||||||
NETRC_FILE = "/home/mtuszowski/.netrc"
|
|
||||||
LOGSTORE = "/home/mtuszowski/conjurer/logs/"
|
|
||||||
ENCODING = "utf-8"
|
|
||||||
RADIOLOG_PATH = "/home/pi/Conjurer/radio_log.log"
|
|
||||||
PERSISTENCE_PATH = "/home/pi/Conjurer/persistence.log"
|
|
||||||
|
|
||||||
else:
|
|
||||||
LOGFILE = "/home/pi/Conjurer/discord_mus_service.log"
|
def _env_path(name: str, default: str) -> Path:
|
||||||
NETRC_FILE = "/home/pi/.netrc"
|
value = os.getenv(name, default)
|
||||||
LOGSTORE = "/home/pi/MediaFolder/logs/"
|
return Path(value).expanduser().resolve()
|
||||||
ENCODING = "utf-8"
|
|
||||||
MUSIC_FOLDER = "/home/pi/MediaFolder/mp3/"
|
|
||||||
PRIORITY_FOLDER = "/home/pi/MediaFoldermp3/Magiczne i chuj/"
|
API_KEY = os.getenv("CONJURER_API_KEY")
|
||||||
RADIOLOG_PATH = "/home/pi/Conjurer/radio_log.log"
|
MAIN_BOT_ADDRESS = _env("CONJURER_MAIN_BOT", "http://127.0.0.1:5000")
|
||||||
PERSISTENCE_PATH = "/home/pi/Conjurer/persistence.log"
|
MUSIC_TRACKER = _env("CONJURER_MUSIC_TRACKER_ENDPOINT", "/prepped_tracks")
|
||||||
|
HOST_ADDRESS = _env("CONJURER_MUSICIAN_HOST", "0.0.0.0")
|
||||||
|
PORT_ADDRESS = int(_env("CONJURER_MUSICIAN_PORT", "5000"))
|
||||||
|
|
||||||
|
BASE_DIR = Path(
|
||||||
|
os.getenv("CONJURER_MUSICIAN_BASE", str(Path(__file__).resolve().parent))
|
||||||
|
)
|
||||||
|
LOGFILE = _env_path(
|
||||||
|
"CONJURER_MUSICIAN_LOG", str(BASE_DIR / "discord_mus_service.log")
|
||||||
|
)
|
||||||
|
LOGSTORE = _env_path("CONJURER_LOGSTORE", str(BASE_DIR / "logs"))
|
||||||
|
MUSIC_FOLDER = _env_path(
|
||||||
|
"CONJURER_MUSIC_FOLDER", str(BASE_DIR / "music")
|
||||||
|
)
|
||||||
|
PRIORITY_FOLDER = _env_path(
|
||||||
|
"CONJURER_PRIORITY_FOLDER", str(MUSIC_FOLDER / "priority")
|
||||||
|
)
|
||||||
|
RADIOLOG_PATH = _env_path(
|
||||||
|
"CONJURER_RADIO_LOG", str(BASE_DIR / "radio_log.log")
|
||||||
|
)
|
||||||
|
PERSISTENCE_PATH = _env_path(
|
||||||
|
"CONJURER_PERSISTENCE_LOG", str(BASE_DIR / "persistence.log")
|
||||||
|
)
|
||||||
|
ALL_PLAYLIST_PATH = _env_path(
|
||||||
|
"CONJURER_ALL_PLAYLIST", str(BASE_DIR / "all_playlist.playlist")
|
||||||
|
)
|
||||||
|
HIT_PLAYLIST_PATH = _env_path(
|
||||||
|
"CONJURER_HIT_PLAYLIST", str(BASE_DIR / "hit.playlist")
|
||||||
|
)
|
||||||
|
REQUEST_PLAYLIST_PATH = _env_path(
|
||||||
|
"CONJURER_REQUEST_PLAYLIST", str(BASE_DIR / "request.playlist")
|
||||||
|
)
|
||||||
|
PRIORITY_PLAYLIST_PATH = _env_path(
|
||||||
|
"CONJURER_PRIORITY_PLAYLIST", str(BASE_DIR / "priority_queue.playlist")
|
||||||
|
)
|
||||||
|
STREAM_TEMPLATE = _env_path(
|
||||||
|
"CONJURER_STREAM_TEMPLATE", str(BASE_DIR / "stream.html")
|
||||||
|
)
|
||||||
|
|
||||||
|
ENCODING = _env("CONJURER_ENCODING", "utf-8")
|
||||||
|
SEPARATOR_FILE_PATH = os.sep
|
||||||
|
|
||||||
|
for playlist_path in (
|
||||||
|
ALL_PLAYLIST_PATH,
|
||||||
|
HIT_PLAYLIST_PATH,
|
||||||
|
REQUEST_PLAYLIST_PATH,
|
||||||
|
PRIORITY_PLAYLIST_PATH,
|
||||||
|
):
|
||||||
|
playlist_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
random.seed()
|
random.seed()
|
||||||
music_file_list = []
|
music_file_list: List[str] = []
|
||||||
priority_list = []
|
priority_list: List[str] = []
|
||||||
|
|
||||||
|
|
||||||
|
def _build_headers() -> Dict[str, str]:
|
||||||
|
headers: Dict[str, str] = {}
|
||||||
|
if API_KEY:
|
||||||
|
headers["X-Conjurer-Api-Key"] = API_KEY
|
||||||
|
return headers
|
||||||
|
|
||||||
|
|
||||||
|
def _authorize_request() -> None:
|
||||||
|
if API_KEY and request.headers.get("X-Conjurer-Api-Key") != API_KEY:
|
||||||
|
abort(401)
|
||||||
|
|
||||||
|
|
||||||
|
def _post_to_bot(payload: List[str]) -> None:
|
||||||
|
response = requests.post(
|
||||||
|
f"{MAIN_BOT_ADDRESS}{MUSIC_TRACKER}",
|
||||||
|
json=payload,
|
||||||
|
headers=_build_headers(),
|
||||||
|
timeout=60,
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("conjurer_musician")
|
||||||
|
logger.info("SENT")
|
||||||
|
logger.info(response.status_code)
|
||||||
|
logger.info("SEND CONFIRMED")
|
||||||
|
|
||||||
|
|
||||||
def rescan():
|
def rescan():
|
||||||
@@ -70,28 +130,29 @@ def rescan():
|
|||||||
logger = logging.getLogger("conjurer_musician")
|
logger = logging.getLogger("conjurer_musician")
|
||||||
logger.info("Rescan triggered")
|
logger.info("Rescan triggered")
|
||||||
|
|
||||||
for mp3_item in Path.glob(Path(MUSIC_FOLDER), "**/*.mp3"):
|
music_file_list.clear()
|
||||||
|
priority_list.clear()
|
||||||
|
|
||||||
|
for mp3_item in MUSIC_FOLDER.glob("**/*.mp3"):
|
||||||
temp_music_file = mp3_item.as_posix()
|
temp_music_file = mp3_item.as_posix()
|
||||||
if platform == "win32":
|
if os.name == "nt":
|
||||||
temp_music_file = temp_music_file.replace("/", "\\")
|
temp_music_file = temp_music_file.replace("/", "\\")
|
||||||
music_file_list.append(temp_music_file)
|
music_file_list.append(temp_music_file)
|
||||||
|
|
||||||
for mp3_item in Path.glob(Path(PRIORITY_FOLDER), "**/*.mp3"):
|
for mp3_item in PRIORITY_FOLDER.glob("**/*.mp3"):
|
||||||
temp_music_file = mp3_item.as_posix()
|
temp_music_file = mp3_item.as_posix()
|
||||||
if platform == "win32":
|
if os.name == "nt":
|
||||||
temp_music_file = temp_music_file.replace("/", "\\")
|
temp_music_file = temp_music_file.replace("/", "\\")
|
||||||
priority_list.append(temp_music_file)
|
priority_list.append(temp_music_file)
|
||||||
|
|
||||||
with open(
|
with ALL_PLAYLIST_PATH.open("w", encoding=ENCODING) as w_file:
|
||||||
"/home/pi/Conjurer/all_playlist.playlist", "w", encoding="utf-8"
|
|
||||||
) as w_file:
|
|
||||||
try:
|
try:
|
||||||
for item in music_file_list:
|
for item in music_file_list:
|
||||||
w_file.write(item)
|
w_file.write(item)
|
||||||
w_file.write("\n")
|
w_file.write("\n")
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
with open("/home/pi/Conjurer/hit.playlist", "w", encoding="utf-8") as w_file:
|
with HIT_PLAYLIST_PATH.open("w", encoding=ENCODING) as w_file:
|
||||||
try:
|
try:
|
||||||
for item in priority_list:
|
for item in priority_list:
|
||||||
w_file.write(item)
|
w_file.write(item)
|
||||||
@@ -116,71 +177,49 @@ def thread_rescan():
|
|||||||
def scan_tracks():
|
def scan_tracks():
|
||||||
# Set the filename and open the file
|
# Set the filename and open the file
|
||||||
logger = logging.getLogger("conjurer_musician")
|
logger = logging.getLogger("conjurer_musician")
|
||||||
|
with open(RADIOLOG_PATH, "r", encoding=ENCODING) as log_file:
|
||||||
|
log_file.seek(os.stat(RADIOLOG_PATH).st_size)
|
||||||
|
prev_size = os.stat(PERSISTENCE_PATH).st_size
|
||||||
|
|
||||||
file = open(RADIOLOG_PATH, "r")
|
while True:
|
||||||
# Find the size of the file and move to the end
|
current_size = os.stat(PERSISTENCE_PATH).st_size
|
||||||
st_results = os.stat(RADIOLOG_PATH)
|
if prev_size != current_size:
|
||||||
st_size = st_results[6]
|
while prev_size != current_size:
|
||||||
file.seek(st_size)
|
prev_size = current_size
|
||||||
st_results1 = os.stat(PERSISTENCE_PATH)
|
|
||||||
prev_st_size1 = st_results[6]
|
|
||||||
|
|
||||||
while 1:
|
|
||||||
|
|
||||||
st_results1 = os.stat(PERSISTENCE_PATH)
|
|
||||||
st_size1 = st_results1[6]
|
|
||||||
if prev_st_size1 != st_size1:
|
|
||||||
while prev_st_size1 != st_size1:
|
|
||||||
prev_st_size1 = st_size1
|
|
||||||
st_results1 = os.stat(PERSISTENCE_PATH)
|
|
||||||
st_size1 = st_results1[6]
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
file1 = open(PERSISTENCE_PATH, "r")
|
current_size = os.stat(PERSISTENCE_PATH).st_size
|
||||||
lines = file1.readlines()
|
with open(PERSISTENCE_PATH, "r", encoding=ENCODING) as persistence:
|
||||||
result = ["next", lines[2]]
|
lines = persistence.readlines()
|
||||||
file1.close()
|
if len(lines) >= 3:
|
||||||
returned = requests.post(
|
_post_to_bot(["next", lines[2]])
|
||||||
f"{MAIN_BOT_ADDRESS}{MUSIC_TRACKER}", json=result, timeout=360
|
|
||||||
)
|
|
||||||
logger.info("SENT")
|
|
||||||
logger.info(returned.status_code)
|
|
||||||
logger.info("SEND CONFIRMED")
|
|
||||||
|
|
||||||
where = file.tell()
|
position = log_file.tell()
|
||||||
line = file.readline()
|
line = log_file.readline()
|
||||||
if not line:
|
if not line:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
file.seek(where)
|
log_file.seek(position)
|
||||||
else:
|
continue
|
||||||
if re.match(".*Prepared.*", line):
|
|
||||||
|
if not re.match(r".*Prepared.*", line):
|
||||||
|
time.sleep(0.1)
|
||||||
|
continue
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
if re.match(".*jingles.*", line):
|
if re.match(r".*jingles.*", line):
|
||||||
logger.info("jingles")
|
|
||||||
logger.info(line) # already has newline
|
|
||||||
result = ["jingles", line]
|
result = ["jingles", line]
|
||||||
elif re.match(".*priority.*", line):
|
elif re.match(r".*priority.*", line):
|
||||||
logger.info("priority")
|
|
||||||
logger.info(line) # already has newline
|
|
||||||
result = ["priority", line]
|
result = ["priority", line]
|
||||||
elif re.match(".*hit.*", line):
|
elif re.match(r".*hit.*", line):
|
||||||
logger.info("hit")
|
|
||||||
logger.info(line) # already has newline
|
|
||||||
result = ["hit", line]
|
result = ["hit", line]
|
||||||
elif re.match(".*all_playlist.*", line):
|
elif re.match(r".*all_playlist.*", line):
|
||||||
logger.info("all")
|
|
||||||
logger.info(line) # already has newline
|
|
||||||
result = ["all", line]
|
result = ["all", line]
|
||||||
elif re.match(".*request.*", line):
|
elif re.match(r".*request.*", line):
|
||||||
logger.info("requests")
|
|
||||||
logger.info(line) # already has newline
|
|
||||||
result = ["requests", line]
|
result = ["requests", line]
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
returned = requests.post(
|
logger.info("Forwarding radio log entry: %s", result[0])
|
||||||
f"{MAIN_BOT_ADDRESS}{MUSIC_TRACKER}", json=result, timeout=360
|
_post_to_bot(result)
|
||||||
)
|
|
||||||
logger.info("SENT")
|
|
||||||
logger.info(returned.status_code)
|
|
||||||
logger.info("SEND CONFIRMED")
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
@@ -290,12 +329,8 @@ def wyszukaj(word_list, how_many, _logger=None, return_to_bot=True):
|
|||||||
if search_weight[itr][0] == item_to_search:
|
if search_weight[itr][0] == item_to_search:
|
||||||
return_list.append(search_weight[itr])
|
return_list.append(search_weight[itr])
|
||||||
if not return_to_bot:
|
if not return_to_bot:
|
||||||
with open(
|
with PRIORITY_PLAYLIST_PATH.open("a", encoding=ENCODING) as s_file:
|
||||||
"/home/pi/Conjurer/priority_queue.playlist",
|
s_file.write(search_weight[itr][1] + "\n")
|
||||||
"r+",
|
|
||||||
encoding="utf-8",
|
|
||||||
) as s_file:
|
|
||||||
s_file.write(search_weight[itr][1])
|
|
||||||
break
|
break
|
||||||
itr += 1
|
itr += 1
|
||||||
else:
|
else:
|
||||||
@@ -336,6 +371,7 @@ def remove_characters(string, character):
|
|||||||
|
|
||||||
@app.route('/get_share_list', methods=['POST'])
|
@app.route('/get_share_list', methods=['POST'])
|
||||||
def get_share_list():
|
def get_share_list():
|
||||||
|
_authorize_request()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
entries = data.get('entries')
|
entries = data.get('entries')
|
||||||
keywords = data.get('keywords')
|
keywords = data.get('keywords')
|
||||||
@@ -352,6 +388,7 @@ def get_share_list():
|
|||||||
|
|
||||||
@app.route('/get_share_links', methods=['POST'])
|
@app.route('/get_share_links', methods=['POST'])
|
||||||
def get_share_links():
|
def get_share_links():
|
||||||
|
_authorize_request()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
file_paths = data.get('file_paths')
|
file_paths = data.get('file_paths')
|
||||||
# Validate file_paths list
|
# Validate file_paths list
|
||||||
@@ -374,7 +411,7 @@ def stream_music():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# return send_from_directory("/tmp/hls", "stream.m3u8")
|
# return send_from_directory("/tmp/hls", "stream.m3u8")
|
||||||
return render_template("/home/pi/Conjurer/stream.html")
|
return render_template(str(STREAM_TEMPLATE))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<string:file_name>")
|
@app.route("/<string:file_name>")
|
||||||
@@ -406,15 +443,14 @@ def stream_music_mp3():
|
|||||||
|
|
||||||
@app.route("/clear_pr_pls", methods=["GET"])
|
@app.route("/clear_pr_pls", methods=["GET"])
|
||||||
def clear_pr_pls():
|
def clear_pr_pls():
|
||||||
|
_authorize_request()
|
||||||
"""
|
"""
|
||||||
The function `clear_pr_pls` clears the contents of the priority queue playlist file.
|
The function `clear_pr_pls` clears the contents of the priority queue playlist file.
|
||||||
|
|
||||||
:return: A JSON response indicating the success of the operation.
|
:return: A JSON response indicating the success of the operation.
|
||||||
"""
|
"""
|
||||||
app.logger.info("CLEARING PLAYLIST")
|
app.logger.info("CLEARING PLAYLIST")
|
||||||
with open(
|
with PRIORITY_PLAYLIST_PATH.open("w", encoding=ENCODING) as cleared_pl:
|
||||||
"/home/pi/Conjurer/priority_queue.playlist", "w", encoding="utf-8"
|
|
||||||
) as cleared_pl:
|
|
||||||
cleared_pl.write("")
|
cleared_pl.write("")
|
||||||
|
|
||||||
return_data = jsonify(isError=False, message="Success", statusCode=200, data=[])
|
return_data = jsonify(isError=False, message="Success", statusCode=200, data=[])
|
||||||
@@ -442,6 +478,7 @@ def update_music_list():
|
|||||||
received and added to the `music_file_list`.
|
received and added to the `music_file_list`.
|
||||||
The status code returned is 200, indicating a successful response.
|
The status code returned is 200, indicating a successful response.
|
||||||
"""
|
"""
|
||||||
|
_authorize_request()
|
||||||
record = json.loads(request.data)
|
record = json.loads(request.data)
|
||||||
app.logger.info(record["item"])
|
app.logger.info(record["item"])
|
||||||
music_file_list.append(record["item"])
|
music_file_list.append(record["item"])
|
||||||
@@ -463,6 +500,7 @@ def look_for_playlist():
|
|||||||
data that was received and added to the `music_file_list`. The status code returned is 200,
|
data that was received and added to the `music_file_list`. The status code returned is 200,
|
||||||
indicating a successful response.
|
indicating a successful response.
|
||||||
"""
|
"""
|
||||||
|
_authorize_request()
|
||||||
record = json.loads(request.data)
|
record = json.loads(request.data)
|
||||||
app.logger.info(record)
|
app.logger.info(record)
|
||||||
app.logger.info(record["lista_slow"])
|
app.logger.info(record["lista_slow"])
|
||||||
@@ -481,13 +519,14 @@ def look_for_playlist():
|
|||||||
|
|
||||||
@app.route("/request_radio_file", methods=["POST"])
|
@app.route("/request_radio_file", methods=["POST"])
|
||||||
def add_request():
|
def add_request():
|
||||||
|
_authorize_request()
|
||||||
record = json.loads(request.data)
|
record = json.loads(request.data)
|
||||||
app.logger.info(record)
|
app.logger.info(record)
|
||||||
app.logger.info(record["lista_slow"])
|
app.logger.info(record["lista_slow"])
|
||||||
app.logger.info(record["UUID"])
|
app.logger.info(record["UUID"])
|
||||||
return_data = wyszukaj(record["lista_slow"], 0, app.logger, False)
|
return_data = wyszukaj(record["lista_slow"], 0, app.logger, False)
|
||||||
|
|
||||||
with open("/home/pi/Conjurer/request.playlist", "a", encoding="utf-8") as s_file:
|
with REQUEST_PLAYLIST_PATH.open("a", encoding=ENCODING) as s_file:
|
||||||
for item in return_data:
|
for item in return_data:
|
||||||
s_file.write(item[1] + "\n")
|
s_file.write(item[1] + "\n")
|
||||||
return_data = (
|
return_data = (
|
||||||
@@ -512,6 +551,7 @@ def create_priority_playlist():
|
|||||||
data that was received and added to the `music_file_list`.
|
data that was received and added to the `music_file_list`.
|
||||||
The status code returned is 200,indicating a successful response.
|
The status code returned is 200,indicating a successful response.
|
||||||
"""
|
"""
|
||||||
|
_authorize_request()
|
||||||
record = json.loads(request.data)
|
record = json.loads(request.data)
|
||||||
app.logger.info(record)
|
app.logger.info(record)
|
||||||
app.logger.info(record["lista_slow"])
|
app.logger.info(record["lista_slow"])
|
||||||
@@ -521,7 +561,7 @@ def create_priority_playlist():
|
|||||||
record["lista_slow"], record["dlugosc_plejlisty"], app.logger, False
|
record["lista_slow"], record["dlugosc_plejlisty"], app.logger, False
|
||||||
)
|
)
|
||||||
random.shuffle(return_data)
|
random.shuffle(return_data)
|
||||||
with open("/home/pi/Conjurer/request.playlist", "a", encoding="utf-8") as s_file:
|
with REQUEST_PLAYLIST_PATH.open("a", encoding=ENCODING) as s_file:
|
||||||
for item in return_data:
|
for item in return_data:
|
||||||
s_file.write(item[1] + "\n")
|
s_file.write(item[1] + "\n")
|
||||||
return_data = (
|
return_data = (
|
||||||
@@ -546,6 +586,7 @@ def add_to_priority():
|
|||||||
data that was received and added to the `music_file_list`.
|
data that was received and added to the `music_file_list`.
|
||||||
The status code returned is 200,indicating a successful response.
|
The status code returned is 200,indicating a successful response.
|
||||||
"""
|
"""
|
||||||
|
_authorize_request()
|
||||||
record = json.loads(request.data)
|
record = json.loads(request.data)
|
||||||
app.logger.info(record)
|
app.logger.info(record)
|
||||||
app.logger.info(record["lista_slow"])
|
app.logger.info(record["lista_slow"])
|
||||||
@@ -554,9 +595,7 @@ def add_to_priority():
|
|||||||
return_data = wyszukaj(
|
return_data = wyszukaj(
|
||||||
record["lista_slow"], record["dlugosc_plejlisty"], app.logger, False
|
record["lista_slow"], record["dlugosc_plejlisty"], app.logger, False
|
||||||
)
|
)
|
||||||
with open(
|
with PRIORITY_PLAYLIST_PATH.open("a", encoding=ENCODING) as s_file:
|
||||||
"/home/pi/Conjurer/priority_queue.playlist", "a", encoding="utf-8"
|
|
||||||
) as s_file:
|
|
||||||
for item in return_data:
|
for item in return_data:
|
||||||
s_file.write(item[1] + "\n")
|
s_file.write(item[1] + "\n")
|
||||||
return_data = (
|
return_data = (
|
||||||
@@ -615,15 +654,19 @@ if __name__ == "__main__":
|
|||||||
logger.info("Started")
|
logger.info("Started")
|
||||||
threads = []
|
threads = []
|
||||||
# threads.append(threading.Thread(target=flask_debug))
|
# threads.append(threading.Thread(target=flask_debug))
|
||||||
threads.append(threading.Thread(target=waitress_run))
|
threads.append(threading.Thread(target=waitress_run, daemon=True))
|
||||||
threads.append(threading.Thread(target=thread_rescan))
|
threads.append(threading.Thread(target=thread_rescan, daemon=True))
|
||||||
|
|
||||||
for worker in threads:
|
for worker in threads:
|
||||||
worker.start()
|
worker.start()
|
||||||
|
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
threads.append(threading.Thread(target=scan_tracks))
|
track_thread = threading.Thread(target=scan_tracks, daemon=True)
|
||||||
threads[2].start()
|
track_thread.start()
|
||||||
|
|
||||||
|
try:
|
||||||
for worker in threads:
|
for worker in threads:
|
||||||
worker.join()
|
worker.join()
|
||||||
|
track_thread.join()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
logger.info("Shutdown requested - exiting musician service")
|
||||||
|
|||||||
+171
-101
@@ -1,13 +1,36 @@
|
|||||||
import json
|
"""Centralised configuration and runtime constants for Conjurer services.
|
||||||
import netrc
|
|
||||||
from datetime import datetime
|
|
||||||
from platform import uname
|
|
||||||
from sys import platform
|
|
||||||
from typing import List, Optional, TypedDict
|
|
||||||
|
|
||||||
import openai
|
This module used to perform heavy filesystem and credential reads at import
|
||||||
import spotipy
|
time which made the project brittle on hosts that did not mirror the original
|
||||||
from spotipy.oauth2 import SpotifyClientCredentials
|
paths. The current implementation defers that work, reads configuration from
|
||||||
|
environment variables (with sensible fallbacks), and protects optional
|
||||||
|
dependencies so the main bot can start even when a secondary service is
|
||||||
|
offline.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, List, Optional, Tuple, TypedDict
|
||||||
|
|
||||||
|
try:
|
||||||
|
import netrc
|
||||||
|
except ImportError: # pragma: no cover - standard on CPython
|
||||||
|
netrc = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import openai
|
||||||
|
except ImportError: # pragma: no cover - optional at runtime
|
||||||
|
openai = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import spotipy
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
|
except ImportError: # pragma: no cover - optional component
|
||||||
|
spotipy = None
|
||||||
|
SpotifyClientCredentials = None
|
||||||
|
|
||||||
Music_Config = TypedDict(
|
Music_Config = TypedDict(
|
||||||
"Music_Config",
|
"Music_Config",
|
||||||
@@ -24,21 +47,43 @@ MASTER_TIMEOUT = datetime.now()
|
|||||||
INITIAL_TIME_WAIT = 500
|
INITIAL_TIME_WAIT = 500
|
||||||
MUZYKA: Music_Config = {"ctx": None, "queue": [], "requester": []}
|
MUZYKA: Music_Config = {"ctx": None, "queue": [], "requester": []}
|
||||||
|
|
||||||
LOGFILE = ""
|
logger = logging.getLogger("discord")
|
||||||
NETRC_FILE = ""
|
|
||||||
MUSIC_FOLDER = ""
|
|
||||||
MEMORY_FIVE_SIARA = ""
|
def _env_path(var_name: str, fallback: Path) -> Path:
|
||||||
MEMORY_FIVE_MUZYKA = ""
|
value = os.getenv(var_name)
|
||||||
SETTINGS_FILE = ""
|
if value:
|
||||||
ENCODING = ""
|
return Path(value).expanduser().resolve()
|
||||||
GRAPHICS_PATH = ""
|
return fallback
|
||||||
|
|
||||||
|
|
||||||
|
def _env(var_name: str, fallback: str) -> str:
|
||||||
|
return os.getenv(var_name, fallback)
|
||||||
|
|
||||||
|
|
||||||
|
BASE_DIR = Path(os.getenv("CONJURER_BASE_DIR", Path(__file__).resolve().parent))
|
||||||
|
|
||||||
|
LOGFILE = _env_path("CONJURER_LOG_FILE", BASE_DIR / "discord.log")
|
||||||
|
NETRC_FILE = _env_path("CONJURER_NETRC_FILE", Path.home() / ".netrc")
|
||||||
|
SETTINGS_FILE = _env_path("CONJURER_SETTINGS_FILE", BASE_DIR / "settings.json")
|
||||||
|
MEMORY_FIVE_SIARA = _env_path("CONJURER_MEMORY_FILE", BASE_DIR / "pamiec.json")
|
||||||
|
MEMORY_FIVE_MUZYKA = _env_path(
|
||||||
|
"CONJURER_MUSIC_MEMORY_FILE", BASE_DIR / "pamiec_muzyki.json"
|
||||||
|
)
|
||||||
|
GRAPHICS_PATH = _env_path(
|
||||||
|
"CONJURER_GRAPHICS_PATH", BASE_DIR / "Conjurer_graphics"
|
||||||
|
)
|
||||||
|
MUSIC_FOLDER = _env_path("CONJURER_MUSIC_FOLDER", BASE_DIR / "music")
|
||||||
|
ENCODING = _env("CONJURER_ENCODING", "utf-8")
|
||||||
|
|
||||||
|
API_SHARED_KEY = os.getenv("CONJURER_API_KEY", "")
|
||||||
MUZYKA_MOJEGO_LUDU_HISTORIA = 1500
|
MUZYKA_MOJEGO_LUDU_HISTORIA = 1500
|
||||||
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = 15
|
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = 15
|
||||||
MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30
|
MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30
|
||||||
|
|
||||||
FILE_SERVICE_ADDRESS = "http://192.168.1.15:5000"
|
FILE_SERVICE_ADDRESS = _env("CONJURER_FILE_SERVICE", "http://127.0.0.1:5000")
|
||||||
RADIO_HARBOR_ADDRESS = "http://192.168.1.15:54321"
|
RADIO_HARBOR_ADDRESS = _env("CONJURER_RADIO_HARBOR", "http://127.0.0.1:54321")
|
||||||
SKIP_TRACK = "/skip"
|
SKIP_TRACK = _env("CONJURER_SKIP_ENDPOINT", "/skip")
|
||||||
|
|
||||||
GET_MP3 = "/mp3"
|
GET_MP3 = "/mp3"
|
||||||
SEND_MP3 = "/update_mp3"
|
SEND_MP3 = "/update_mp3"
|
||||||
@@ -48,98 +93,123 @@ CREATE_PRIO_PLAYLIST = "/create_priority_playlist"
|
|||||||
|
|
||||||
REQUEST_MUSIC = "/request_radio_file"
|
REQUEST_MUSIC = "/request_radio_file"
|
||||||
CLEAR_PRIO = "/clear_pr_pls"
|
CLEAR_PRIO = "/clear_pr_pls"
|
||||||
LIBRARIAN_SERVICE_ADDRESS = "http://192.168.1.192:5001"
|
LIBRARIAN_SERVICE_ADDRESS = _env(
|
||||||
SEND_QUERY = "/query"
|
"CONJURER_LIBRARIAN_SERVICE", "http://127.0.0.1:5001"
|
||||||
|
)
|
||||||
|
SEND_QUERY = _env("CONJURER_LIBRARIAN_QUERY_ENDPOINT", "/query")
|
||||||
TIME_BETWEEN_CALLS = 100000
|
TIME_BETWEEN_CALLS = 100000
|
||||||
LAST_SPONTANEOUS_CALL = datetime.now()
|
LAST_SPONTANEOUS_CALL = datetime.now()
|
||||||
HOST_ADDRESS = "192.168.1.191"
|
HOST_ADDRESS = _env("CONJURER_DISCORD_HOST", "0.0.0.0")
|
||||||
PORT_ADDRESS = 5000
|
PORT_ADDRESS = int(_env("CONJURER_DISCORD_PORT", "5000"))
|
||||||
|
|
||||||
# *=========================================== Platform Specific Predefines
|
# *=========================================== Platform Specific Predefines
|
||||||
|
|
||||||
if platform in ("linux", "linux2"):
|
SEPARATOR_FILE_PATH = _env("CONJURER_PATH_SEPARATOR", os.sep)
|
||||||
SEPARATOR_FILE_PATH = "/"
|
|
||||||
if "microsoft-standard" in uname().release:
|
|
||||||
LOGFILE = "/home/mtuszowski/conjurer/discord.log"
|
|
||||||
MEMORY_FIVE_SIARA = "/home/mtuszowski/conjurer/pamiec.json"
|
|
||||||
SYSTEM_GPT_SETTINGS = "/home/mtuszowski/conjurer/system_gpt_settings.json"
|
|
||||||
MEMORY_FIVE_MUZYKA = "/home/mtuszowski/conjurer/pamiec_muzyki.json"
|
|
||||||
MUSIC_FOLDER = "/mnt/g/Muzyka/"
|
|
||||||
SETTINGS_FILE = "/home/mtuszowski/conjurer/settings.json"
|
|
||||||
NETRC_FILE = "/home/mtuszowski/.netrc"
|
|
||||||
LOGSTORE = "/home/mtuszowski/conjurer/logs/"
|
|
||||||
ACCIDENT_LOG = "/home/mtuszowski/conjurer/accident_log.json"
|
|
||||||
ENCODING = "utf-8"
|
|
||||||
GRAPHICS_PATH = "/home/mtuszowski/conjurer/Conjurer_graphics/"
|
|
||||||
DIR_PATH_SADOX = "/mnt/c/Users/mtusz/OneDrive/Dokumenty/Fansadox"
|
|
||||||
|
|
||||||
else:
|
DIR_PATH_SADOX = _env_path(
|
||||||
LOGFILE = "/home/pi/Conjurer/discord.log"
|
"CONJURER_SADOX_DIR", BASE_DIR / "Fansadox"
|
||||||
MEMORY_FIVE_SIARA = "/home/pi/Conjurer/pamiec.json"
|
|
||||||
SYSTEM_GPT_SETTINGS = "/home/pi/Conjurer/system_gpt_settings.json"
|
|
||||||
MEMORY_FIVE_MUZYKA = "/home/pi/Conjurer/pamiec_muzyki.json"
|
|
||||||
MUSIC_FOLDER = "/home/pi/MediaShare/mp3/"
|
|
||||||
SETTINGS_FILE = "/home/pi/Conjurer/settings.json"
|
|
||||||
NETRC_FILE = "/home/pi/.netrc"
|
|
||||||
LOGSTORE = "/home/pi/MediaShara/logs/"
|
|
||||||
ACCIDENT_LOG = "/home/pi/Conjurer/accident_log.json"
|
|
||||||
ENCODING = "utf-8"
|
|
||||||
GRAPHICS_PATH = "/home/pi/MediaShare/Conjurer_graphics/"
|
|
||||||
DIR_PATH_SADOX = "/home/pi/MediaShare/Fansadox/"
|
|
||||||
|
|
||||||
|
|
||||||
elif platform == "win32":
|
|
||||||
LOGFILE = "discord.log"
|
|
||||||
MEMORY_FIVE_SIARA = "pamiec.json"
|
|
||||||
SYSTEM_GPT_SETTINGS = "system_gpt_settings.json"
|
|
||||||
MEMORY_FIVE_MUZYKA = "pamiec_muzyki.json"
|
|
||||||
MUSIC_FOLDER = "G:\\Muzyka\\"
|
|
||||||
SETTINGS_FILE = "settings.json"
|
|
||||||
NETRC_FILE = "C:\\Users\\mtusz\\.netrc"
|
|
||||||
LOGSTORE = "C:\\Users\\mtusz\\OneDrive\\Pulpit\\Conjurer\\logs\\"
|
|
||||||
ACCIDENT_LOG = "accident_log.json"
|
|
||||||
ENCODING = "utf-8"
|
|
||||||
DIR_PATH_SADOX = "C:\\Users\\mtusz\\OneDrive\\Dokumenty\\Fansadox\\"
|
|
||||||
SEPARATOR_FILE_PATH = "\\"
|
|
||||||
with open(SETTINGS_FILE, "r", encoding=ENCODING) as f_settings_file:
|
|
||||||
DATA = json.load(f_settings_file)
|
|
||||||
REMOTE_HOST_NAME = "openai"
|
|
||||||
netrc_mod = netrc.netrc(NETRC_FILE)
|
|
||||||
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
|
|
||||||
openai.api_key = authTokens[2]
|
|
||||||
OPENAICLIENT = openai.AsyncOpenAI(api_key=openai.api_key)
|
|
||||||
REMOTE_HOST_NAME = "discord"
|
|
||||||
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
|
|
||||||
TOKEN = authTokens[2]
|
|
||||||
|
|
||||||
REMOTE_HOST_NAME = "spotipy"
|
|
||||||
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
|
|
||||||
SPOTIFY_CTRL = spotipy.Spotify(
|
|
||||||
client_credentials_manager=SpotifyClientCredentials(
|
|
||||||
client_id=authTokens[0],
|
|
||||||
client_secret=authTokens[2],
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
REMOTE_HOST_NAME = "youtube"
|
|
||||||
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
|
|
||||||
YOUTUBE_AUTH = [authTokens[0],authTokens[2]]
|
|
||||||
|
|
||||||
WORD_REACTIONS = DATA["word_reactions"]
|
SYSTEM_GPT_SETTINGS = _env_path(
|
||||||
CYCLIC_WORDS = DATA["cyclic_words"]
|
"CONJURER_SYSTEM_GPT_SETTINGS", BASE_DIR / "system_gpt_settings.json"
|
||||||
|
)
|
||||||
|
|
||||||
|
LOGSTORE = _env_path("CONJURER_LOGSTORE", BASE_DIR / "logs")
|
||||||
|
ACCIDENT_LOG = _env_path(
|
||||||
|
"CONJURER_ACCIDENT_LOG", BASE_DIR / "accident_log.json"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _load_json(path: Path, fallback) -> object:
|
||||||
|
try:
|
||||||
|
with path.open("r", encoding=ENCODING) as handle:
|
||||||
|
return json.load(handle)
|
||||||
|
except FileNotFoundError:
|
||||||
|
logger.warning("Missing JSON file at %s – using fallback", path)
|
||||||
|
return fallback
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.warning("Corrupt JSON at %s – resetting to fallback", path)
|
||||||
|
return fallback
|
||||||
|
|
||||||
|
|
||||||
|
DATA = _load_json(SETTINGS_FILE, {})
|
||||||
|
WORD_REACTIONS = DATA.get("word_reactions", {})
|
||||||
|
CYCLIC_WORDS = DATA.get("cyclic_words", {})
|
||||||
for key in WORD_REACTIONS:
|
for key in WORD_REACTIONS:
|
||||||
|
if isinstance(WORD_REACTIONS[key], list) and len(WORD_REACTIONS[key]) >= 3:
|
||||||
WORD_REACTIONS[key][2] = datetime.now()
|
WORD_REACTIONS[key][2] = datetime.now()
|
||||||
with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as temp_memory_file:
|
|
||||||
# First we load existing data into a dict.
|
|
||||||
MESSAGE_TABLE = json.load(temp_memory_file)
|
|
||||||
|
|
||||||
with open(SYSTEM_GPT_SETTINGS, "r+", encoding=ENCODING) as temp_settings_file:
|
MESSAGE_TABLE = _load_json(MEMORY_FIVE_SIARA, {})
|
||||||
# First we load existing data into a dict.
|
GPT_SETTINGS = _load_json(SYSTEM_GPT_SETTINGS, {})
|
||||||
GPT_SETTINGS = json.load(temp_settings_file)
|
MESSAGE_TABLE_MUZYKA = _load_json(MEMORY_FIVE_MUZYKA, {})
|
||||||
with open(MEMORY_FIVE_MUZYKA, "r+", encoding=ENCODING) as temp_music_memory_file:
|
|
||||||
# First we load existing data into a dict.
|
SPECJALNE_ZIEMNIACZKI = GPT_SETTINGS[1] if isinstance(GPT_SETTINGS, list) else {}
|
||||||
MESSAGE_TABLE_MUZYKA = json.load(temp_music_memory_file)
|
ASSISTANTS: Dict[str, Tuple[str, str, int, object]] = {}
|
||||||
SPECJALNE_ZIEMNIACZKI = GPT_SETTINGS[1]
|
|
||||||
ASSISTANTS = {}
|
|
||||||
|
def _load_netrc_credentials(host: str) -> Optional[Tuple[str, str, str]]:
|
||||||
|
if netrc is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
parsed = netrc.netrc(str(NETRC_FILE))
|
||||||
|
except FileNotFoundError:
|
||||||
|
logger.warning("netrc file %s not found", NETRC_FILE)
|
||||||
|
return None
|
||||||
|
except netrc.NetrcParseError:
|
||||||
|
logger.warning("netrc file %s is invalid", NETRC_FILE)
|
||||||
|
return None
|
||||||
|
return parsed.authenticators(host)
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_token(host: str, env_var: str) -> Optional[str]:
|
||||||
|
env_value = os.getenv(env_var)
|
||||||
|
if env_value:
|
||||||
|
return env_value
|
||||||
|
creds = _load_netrc_credentials(host)
|
||||||
|
if creds:
|
||||||
|
return creds[2]
|
||||||
|
logger.warning("Token for %s not configured", host)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
OPENAI_API_KEY = _resolve_token("openai", "OPENAI_API_KEY")
|
||||||
|
if openai and OPENAI_API_KEY:
|
||||||
|
openai.api_key = OPENAI_API_KEY
|
||||||
|
OPENAICLIENT = openai.AsyncOpenAI(api_key=OPENAI_API_KEY)
|
||||||
|
else:
|
||||||
|
OPENAICLIENT = None
|
||||||
|
|
||||||
|
TOKEN = _resolve_token("discord", "DISCORD_TOKEN")
|
||||||
|
|
||||||
|
if spotipy:
|
||||||
|
spotify_creds = _load_netrc_credentials("spotipy")
|
||||||
|
if spotify_creds and SpotifyClientCredentials:
|
||||||
|
SPOTIFY_CTRL = spotipy.Spotify(
|
||||||
|
client_credentials_manager=SpotifyClientCredentials(
|
||||||
|
client_id=spotify_creds[0],
|
||||||
|
client_secret=spotify_creds[2],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
SPOTIFY_CTRL = None
|
||||||
|
else:
|
||||||
|
SPOTIFY_CTRL = None
|
||||||
|
|
||||||
|
youtube_creds = _load_netrc_credentials("youtube")
|
||||||
|
if youtube_creds:
|
||||||
|
YOUTUBE_AUTH = [youtube_creds[0], youtube_creds[2]]
|
||||||
|
else:
|
||||||
|
YOUTUBE_AUTH = [
|
||||||
|
os.getenv("YOUTUBE_USERNAME", ""),
|
||||||
|
os.getenv("YOUTUBE_PASSWORD", ""),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def service_headers() -> Dict[str, str]:
|
||||||
|
"""Shared header dict for internal HTTP calls."""
|
||||||
|
if API_SHARED_KEY:
|
||||||
|
return {"X-Conjurer-Api-Key": API_SHARED_KEY}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
LATEX_TEX_ENGINE = "tectonic"
|
LATEX_TEX_ENGINE = "tectonic"
|
||||||
|
|||||||
@@ -0,0 +1,163 @@
|
|||||||
|
# Conjurer Deployment Guide
|
||||||
|
|
||||||
|
This document walks through deploying the Conjurer stack (Discord bot, music
|
||||||
|
service, librarian service) on two Raspberry Pi 4s and one Windows host, first
|
||||||
|
with plain Docker Compose, then with a future Kubernetes setup.
|
||||||
|
|
||||||
|
## 1. Current Hardware Layout
|
||||||
|
|
||||||
|
- **Windows PC**: Stores persistent data (JSON memories, configs, music
|
||||||
|
catalogue). Shares folders over the network (SMB) for the Pis.
|
||||||
|
- **Raspberry Pi A** (“radio”): Runs the Liquidsoap/liq radio pipeline and the
|
||||||
|
musician service (Flask + file watcher).
|
||||||
|
- **Raspberry Pi B** (“bot”): Runs the Discord bot, communication bridge, and
|
||||||
|
librarian Flask service.
|
||||||
|
|
||||||
|
You can rebalance as follows:
|
||||||
|
|
||||||
|
| Service | Recommended Host | Notes |
|
||||||
|
|---------------------|------------------|-------|
|
||||||
|
| Discord bot + comms | Raspberry Pi B | Needs outbound internet, moderate CPU |
|
||||||
|
| Librarian service | Raspberry Pi B | CPU-heavy during Crossref queries; keep close to bot |
|
||||||
|
| Musician service | Raspberry Pi A | Has direct disk access to music, same box as Liquidsoap |
|
||||||
|
| Data storage | Windows | Expose via SMB; mount inside containers |
|
||||||
|
|
||||||
|
## 2. Prepare Shared Storage on Windows
|
||||||
|
|
||||||
|
1. Create directories, e.g. `C:\Conjurer\config`, `C:\Conjurer\logs`,
|
||||||
|
`C:\Conjurer\music`, `C:\Conjurer\playlists`, `C:\Conjurer\secrets`.
|
||||||
|
2. Copy your existing JSON settings (`settings.json`, `pamiec.json`,
|
||||||
|
`pamiec_muzyki.json`, `system_gpt_settings.json`, etc.) into `config`.
|
||||||
|
3. Create blank placeholder files if they do not exist yet.
|
||||||
|
4. Share the root folder (`C:\Conjurer`) over SMB with read/write access for the
|
||||||
|
Pi user (create credentials if necessary).
|
||||||
|
|
||||||
|
## 3. Configure Environment Files
|
||||||
|
|
||||||
|
1. On your workstation, copy the example env files:
|
||||||
|
```bash
|
||||||
|
cp docker/env/bot.env.example docker/env/bot.env
|
||||||
|
cp docker/env/musician.env.example docker/env/musician.env
|
||||||
|
cp docker/env/librarian.env.example docker/env/librarian.env
|
||||||
|
```
|
||||||
|
2. Edit each `docker/env/*.env` to replace `HACKME!` with real values:
|
||||||
|
- `DISCORD_TOKEN`, `OPENAI_API_KEY`, `CONJURER_API_KEY` (use the same value for
|
||||||
|
all services).
|
||||||
|
- For musician/librarian, adjust mounts to the SMB paths you will mount on the
|
||||||
|
Pis, e.g. `/mnt/conjurer/music`.
|
||||||
|
- Set `CONJURER_CROSSREF_MAILTO` to a real email as required by Crossref.
|
||||||
|
3. If you rely on `.netrc`, copy it to `C:\Conjurer\secrets\.netrc` and set
|
||||||
|
`CONJURER_NETRC_FILE` accordingly.
|
||||||
|
|
||||||
|
## 4. Install Docker on Raspberry Pis and Windows
|
||||||
|
|
||||||
|
### Raspberry Pi
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://get.docker.com | sh
|
||||||
|
sudo usermod -aG docker $USER
|
||||||
|
sudo reboot
|
||||||
|
|
||||||
|
# Install docker compose plugin
|
||||||
|
sudo apt-get install docker-compose-plugin
|
||||||
|
```
|
||||||
|
|
||||||
|
### Windows
|
||||||
|
- Install **Docker Desktop**.
|
||||||
|
- Enable WSL2 backend and expose the shared Windows folders to the containers
|
||||||
|
(Docker Desktop settings → Resources → File Sharing).
|
||||||
|
|
||||||
|
## 5. Deploy Musician Service (Pi A)
|
||||||
|
|
||||||
|
1. SSH into Raspberry Pi A.
|
||||||
|
2. Mount the Windows SMB share:
|
||||||
|
```bash
|
||||||
|
sudo mkdir -p /mnt/conjurer
|
||||||
|
sudo apt-get install cifs-utils
|
||||||
|
sudo mount -t cifs //WINDOWS_HOST/Conjurer /mnt/conjurer -o user=YOURUSER
|
||||||
|
```
|
||||||
|
Add an entry to `/etc/fstab` for persistence.
|
||||||
|
3. Copy the repo to the Pi or `git clone` it.
|
||||||
|
4. On Pi A, create override compose file (optional) pointing volumes to
|
||||||
|
`/mnt/conjurer`.
|
||||||
|
5. Start only the musician service:
|
||||||
|
```bash
|
||||||
|
docker compose up --build -d musician
|
||||||
|
```
|
||||||
|
Alternatively, duplicate `docker-compose.yml`, strip other services, and run
|
||||||
|
`docker compose up -d`.
|
||||||
|
|
||||||
|
## 6. Deploy Bot + Librarian (Pi B)
|
||||||
|
|
||||||
|
1. Repeat SMB mount on Pi B (same mount path).
|
||||||
|
2. Copy repo / pull latest changes.
|
||||||
|
3. Create `.env` files with tokens (or copy from control machine).
|
||||||
|
4. Start bot and librarian:
|
||||||
|
```bash
|
||||||
|
docker compose up -d bot librarian
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7. Optional: Run Supporting Liquidsoap Radio
|
||||||
|
|
||||||
|
- Keep Liquidsoap on Pi A as-is, using the same music directories. Ensure the
|
||||||
|
musician container has read access to those directories (bind mount).
|
||||||
|
|
||||||
|
## 8. Verifying
|
||||||
|
|
||||||
|
1. `docker ps` on each Pi to confirm containers running.
|
||||||
|
2. Inspect logs under the mounted logs directory (`/mnt/conjurer/logs`).
|
||||||
|
3. Join Discord server; issue commands to confirm functionality.
|
||||||
|
4. Hit health endpoints manually (e.g. `curl http://PIB:5000/conjurer`).
|
||||||
|
|
||||||
|
## Rebalancing Suggestions
|
||||||
|
|
||||||
|
- If librarian CPU spikes become an issue, move it to Pi A or another host.
|
||||||
|
- If you add a dedicated NAS, mount the network share read-only for the musician
|
||||||
|
container and read/write for other services.
|
||||||
|
|
||||||
|
## 9. Future Kubernetes Deployment (Outline)
|
||||||
|
|
||||||
|
### Hardware Considerations
|
||||||
|
|
||||||
|
- Minimum three nodes for HA: use the existing two Pis plus one additional Pi 4
|
||||||
|
(8 GB preferred). Use Windows PC as storage provider via NFS/SMB CSI driver or
|
||||||
|
as a data gateway.
|
||||||
|
- Consider Pi clusters with USB SSDs for better I/O.
|
||||||
|
|
||||||
|
### Cluster Setup Steps
|
||||||
|
|
||||||
|
1. Install a lightweight Kubernetes distribution (e.g., k3s) on each Pi:
|
||||||
|
```bash
|
||||||
|
curl -sfL https://get.k3s.io | sh -
|
||||||
|
# On additional nodes
|
||||||
|
curl -sfL https://get.k3s.io | K3S_URL=https://MASTER:6443 K3S_TOKEN=HACKME sh -
|
||||||
|
```
|
||||||
|
2. Install MetalLB for load balancer support on LAN.
|
||||||
|
3. Configure persistent volumes using:
|
||||||
|
- `nfs-subdir-external-provisioner` pointing to Windows share (ensure Windows
|
||||||
|
host supports NFS or run an NFS gateway on another machine).
|
||||||
|
- Alternatively, attach individual USB drives to each Pi and use
|
||||||
|
`local-path-provisioner` for node-local storage.
|
||||||
|
4. Create Kubernetes `Secret` objects for tokens (`DISCORD_TOKEN`, etc.).
|
||||||
|
5. Define `Deployment` manifests for each service (bot, musician, librarian) and
|
||||||
|
associated `Services`.
|
||||||
|
6. Expose Discord bot ports via `NodePort` or Ingress.
|
||||||
|
7. Use `StatefulSet` if you need stable identity for the musician service (due to
|
||||||
|
local storage).
|
||||||
|
|
||||||
|
### Optimisation Tips
|
||||||
|
|
||||||
|
- Keep CPU-heavy librarian pods optionally on a beefier node; use
|
||||||
|
`nodeSelector`/`affinity` to pin workloads.
|
||||||
|
- Consider splitting the persistent storage: music on Pi A (USB disk), logs and
|
||||||
|
configs on Pi B, backups on Windows.
|
||||||
|
- For improved reliability, add at least one extra Pi for quorum and to host the
|
||||||
|
communication bridge if the bot node fails.
|
||||||
|
|
||||||
|
## Summary Checklist
|
||||||
|
|
||||||
|
1. Prepare Windows shares & tokens.
|
||||||
|
2. Configure `docker/env/*.env` using `HACKME!` templates as reference.
|
||||||
|
3. Install Docker on Pis, mount network shares.
|
||||||
|
4. Launch musician on Pi A, bot + librarian on Pi B.
|
||||||
|
5. Verify Discord functionality and API endpoints.
|
||||||
|
6. Plan Kubernetes migration when ready (k3s + MetalLB + storage provisioner).
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import file_search_functions
|
import conjurer.backup_old_docker.file_search_functions as file_search_functions
|
||||||
|
|
||||||
class FileSelectView(discord.ui.View):
|
class FileSelectView(discord.ui.View):
|
||||||
def __init__(self, files):
|
def __init__(self, files):
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ sudo apt-get install python3-dev
|
|||||||
sudo apt-get install portaudio19-dev python3-pyaudio
|
sudo apt-get install portaudio19-dev python3-pyaudio
|
||||||
sudo apt-get install
|
sudo apt-get install
|
||||||
cd /home/pi || exit
|
cd /home/pi || exit
|
||||||
mdkir Conjurer
|
mkdir Conjurer
|
||||||
cd Conjurer ||exit
|
cd Conjurer ||exit
|
||||||
python3 -m venv /home/pi/Conjurer/.env
|
python3 -m venv /home/pi/Conjurer/.env
|
||||||
cp /home/pi/conjurer/requirements_bot.txt /home/pi/Conjurer/
|
cp /home/pi/conjurer/requirements_bot.txt /home/pi/Conjurer/
|
||||||
|
|||||||
+2
-2
@@ -10,7 +10,7 @@ import discord
|
|||||||
from discord import app_commands
|
from discord import app_commands
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
from constants import (
|
from conjurer.backup_old_docker.constants import (
|
||||||
ALLOWED_ROLES,
|
ALLOWED_ROLES,
|
||||||
GUILD_ID,
|
GUILD_ID,
|
||||||
LATEX_MAX_ATTACH_MB,
|
LATEX_MAX_ATTACH_MB,
|
||||||
@@ -19,7 +19,7 @@ from constants import (
|
|||||||
LATEX_TEX_ENGINE,
|
LATEX_TEX_ENGINE,
|
||||||
OPENAI_MODEL,
|
OPENAI_MODEL,
|
||||||
)
|
)
|
||||||
from latex_functions import (
|
from conjurer.backup_old_docker.latex_functions import (
|
||||||
compile_single_tex_bytes,
|
compile_single_tex_bytes,
|
||||||
compile_zip_to_zip,
|
compile_zip_to_zip,
|
||||||
is_safe_asset_name,
|
is_safe_asset_name,
|
||||||
|
|||||||
+12
-3
@@ -13,9 +13,16 @@ import PyPDF2
|
|||||||
import requests
|
import requests
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
|
|
||||||
from ai_functions import handle_response
|
from conjurer.backup_old_docker.ai_functions import handle_response
|
||||||
from communication_subroutine import IN_COMM_Q, OUT_COMM_Q, QueryControl
|
from conjurer.backup_old_docker.communication_subroutine import IN_COMM_Q, OUT_COMM_Q, QueryControl
|
||||||
from constants import DIR_PATH_SADOX, LIBRARIAN_SERVICE_ADDRESS, SEND_QUERY
|
from conjurer.backup_old_docker.constants import (
|
||||||
|
DIR_PATH_SADOX,
|
||||||
|
LIBRARIAN_SERVICE_ADDRESS,
|
||||||
|
SEND_QUERY,
|
||||||
|
service_headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
SERVICE_HEADERS = service_headers()
|
||||||
|
|
||||||
|
|
||||||
class DataModule(commands.Cog):
|
class DataModule(commands.Cog):
|
||||||
@@ -165,6 +172,7 @@ class DataModule(commands.Cog):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}",
|
f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}",
|
||||||
json=json_query,
|
json=json_query,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
@@ -260,6 +268,7 @@ class DataModule(commands.Cog):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}",
|
f"{LIBRARIAN_SERVICE_ADDRESS}{SEND_QUERY}",
|
||||||
json=json_query,
|
json=json_query,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
|
|||||||
+21
-6
@@ -9,7 +9,7 @@ import discord
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
from constants import (
|
from conjurer.backup_old_docker.constants import (
|
||||||
FILE_SERVICE_ADDRESS,
|
FILE_SERVICE_ADDRESS,
|
||||||
GET_MP3,
|
GET_MP3,
|
||||||
GET_PLAYLIST,
|
GET_PLAYLIST,
|
||||||
@@ -17,11 +17,16 @@ from constants import (
|
|||||||
SEND_MP3,
|
SEND_MP3,
|
||||||
SPOTIFY_CTRL,
|
SPOTIFY_CTRL,
|
||||||
YOUTUBE_AUTH,
|
YOUTUBE_AUTH,
|
||||||
|
service_headers,
|
||||||
)
|
)
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
SERVICE_HEADERS = service_headers()
|
||||||
|
MUSIC_ROOT = Path(MUSIC_FOLDER)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MusicFileList(object):
|
class MusicFileList(object):
|
||||||
"""
|
"""
|
||||||
@@ -43,11 +48,15 @@ class MusicFileList(object):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.logger.info("Attempt to connect to file service")
|
self.logger.info("Attempt to connect to file service")
|
||||||
response = requests.get(f"{FILE_SERVICE_ADDRESS}{GET_MP3}", timeout=360)
|
response = requests.get(
|
||||||
|
f"{FILE_SERVICE_ADDRESS}{GET_MP3}",
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
|
timeout=60,
|
||||||
|
)
|
||||||
self.music_file_list = response.json()["music_file_list"]
|
self.music_file_list = response.json()["music_file_list"]
|
||||||
self.file_service_active = True
|
self.file_service_active = True
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
for mp3_item in Path.glob(Path(MUSIC_FOLDER), "**/*.mp3"):
|
for mp3_item in MUSIC_ROOT.glob("**/*.mp3"):
|
||||||
temp_music_file = mp3_item.as_posix()
|
temp_music_file = mp3_item.as_posix()
|
||||||
if platform == "win32":
|
if platform == "win32":
|
||||||
temp_music_file = temp_music_file.replace("/", "\\")
|
temp_music_file = temp_music_file.replace("/", "\\")
|
||||||
@@ -98,7 +107,12 @@ class MusicFileList(object):
|
|||||||
"""
|
"""
|
||||||
self.music_file_list.append(item)
|
self.music_file_list.append(item)
|
||||||
post_data = {"item": str(item)}
|
post_data = {"item": str(item)}
|
||||||
requests.post(f"{FILE_SERVICE_ADDRESS}{SEND_MP3}", json=post_data, timeout=360)
|
requests.post(
|
||||||
|
f"{FILE_SERVICE_ADDRESS}{SEND_MP3}",
|
||||||
|
json=post_data,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
|
timeout=60,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
MUSIC_FILE_LIST = MusicFileList("discord")
|
MUSIC_FILE_LIST = MusicFileList("discord")
|
||||||
@@ -142,7 +156,7 @@ async def get_file(ctx, source, link):
|
|||||||
url_data = {"urls": []}
|
url_data = {"urls": []}
|
||||||
url_dict = {}
|
url_dict = {}
|
||||||
url_dict["save_path"] = Path(
|
url_dict["save_path"] = Path(
|
||||||
PurePath.joinpath(Path(MUSIC_FOLDER), Path(directory_name))
|
PurePath.joinpath(MUSIC_ROOT, Path(directory_name))
|
||||||
)
|
)
|
||||||
url_dict["save_path"].mkdir(parents=True, exist_ok=True)
|
url_dict["save_path"].mkdir(parents=True, exist_ok=True)
|
||||||
url_dict["songs"] = file_list
|
url_dict["songs"] = file_list
|
||||||
@@ -152,7 +166,7 @@ async def get_file(ctx, source, link):
|
|||||||
coro = asyncio.to_thread(
|
coro = asyncio.to_thread(
|
||||||
youtube_download.download_songs,
|
youtube_download.download_songs,
|
||||||
songs=url_data,
|
songs=url_data,
|
||||||
output_dir=MUSIC_FOLDER,
|
output_dir=str(MUSIC_ROOT),
|
||||||
format_str="bestaudio/best",
|
format_str="bestaudio/best",
|
||||||
skip_mp3=False,
|
skip_mp3=False,
|
||||||
keep_playlist_order=False,
|
keep_playlist_order=False,
|
||||||
@@ -308,6 +322,7 @@ async def search_music(ctx, how_many=0, slowa_kluczowe=None):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{FILE_SERVICE_ADDRESS}{GET_PLAYLIST}",
|
f"{FILE_SERVICE_ADDRESS}{GET_PLAYLIST}",
|
||||||
json=jrequest,
|
json=jrequest,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
return_data = await coroutine
|
return_data = await coroutine
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ from typing import Optional
|
|||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
from constants import ACCIDENT_LOG, DATA, ENCODING
|
from conjurer.backup_old_docker.constants import ACCIDENT_LOG, DATA, ENCODING
|
||||||
|
|
||||||
historia_fabryczki = DATA["fabryczka"]
|
historia_fabryczki = DATA["fabryczka"]
|
||||||
|
|
||||||
|
|||||||
+16
-1
@@ -8,7 +8,18 @@ import uuid
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from constants import RADIO_HARBOR_ADDRESS, SKIP_TRACK, FILE_SERVICE_ADDRESS, ADD_TO_PRIO_PLAYLIST, REQUEST_MUSIC, CREATE_PRIO_PLAYLIST, CLEAR_PRIO
|
from conjurer.backup_old_docker.constants import (
|
||||||
|
RADIO_HARBOR_ADDRESS,
|
||||||
|
SKIP_TRACK,
|
||||||
|
FILE_SERVICE_ADDRESS,
|
||||||
|
ADD_TO_PRIO_PLAYLIST,
|
||||||
|
REQUEST_MUSIC,
|
||||||
|
CREATE_PRIO_PLAYLIST,
|
||||||
|
CLEAR_PRIO,
|
||||||
|
service_headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
SERVICE_HEADERS = service_headers()
|
||||||
|
|
||||||
class RadioModule(commands.Cog):
|
class RadioModule(commands.Cog):
|
||||||
def __init__(self, bot, logger_name):
|
def __init__(self, bot, logger_name):
|
||||||
@@ -95,6 +106,7 @@ class RadioModule(commands.Cog):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{FILE_SERVICE_ADDRESS}{ADD_TO_PRIO_PLAYLIST}",
|
f"{FILE_SERVICE_ADDRESS}{ADD_TO_PRIO_PLAYLIST}",
|
||||||
json=jrequest,
|
json=jrequest,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
result = await coroutine
|
result = await coroutine
|
||||||
@@ -130,6 +142,7 @@ class RadioModule(commands.Cog):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{FILE_SERVICE_ADDRESS}{REQUEST_MUSIC}",
|
f"{FILE_SERVICE_ADDRESS}{REQUEST_MUSIC}",
|
||||||
json=jrequest,
|
json=jrequest,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
result = await coroutine
|
result = await coroutine
|
||||||
@@ -167,6 +180,7 @@ class RadioModule(commands.Cog):
|
|||||||
requests.post,
|
requests.post,
|
||||||
f"{FILE_SERVICE_ADDRESS}{CREATE_PRIO_PLAYLIST}",
|
f"{FILE_SERVICE_ADDRESS}{CREATE_PRIO_PLAYLIST}",
|
||||||
json=jrequest,
|
json=jrequest,
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
result = await coroutine
|
result = await coroutine
|
||||||
@@ -194,6 +208,7 @@ class RadioModule(commands.Cog):
|
|||||||
coroutine = asyncio.to_thread(
|
coroutine = asyncio.to_thread(
|
||||||
requests.get,
|
requests.get,
|
||||||
f"{FILE_SERVICE_ADDRESS}{CLEAR_PRIO}",
|
f"{FILE_SERVICE_ADDRESS}{CLEAR_PRIO}",
|
||||||
|
headers=SERVICE_HEADERS,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
)
|
)
|
||||||
result = await coroutine
|
result = await coroutine
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ tiktoken
|
|||||||
PyNaCl
|
PyNaCl
|
||||||
flask[async]
|
flask[async]
|
||||||
waitress
|
waitress
|
||||||
clickupython
|
|
||||||
assemblyai[extras]
|
assemblyai[extras]
|
||||||
SpeechRecognition
|
SpeechRecognition
|
||||||
git+https://github.com/imayhaveborkedit/discord-ext-voice-recvO
|
git+https://github.com/imayhaveborkedit/discord-ext-voice-recvO
|
||||||
@@ -15,7 +15,6 @@ PyNaCl
|
|||||||
flask[async]
|
flask[async]
|
||||||
PyMuPDF
|
PyMuPDF
|
||||||
waitress
|
waitress
|
||||||
clickupython
|
|
||||||
assemblyai[extras]
|
assemblyai[extras]
|
||||||
SpeechRecognition
|
SpeechRecognition
|
||||||
git+https://github.com/imayhaveborkedit/discord-ext-voice-recv
|
git+https://github.com/imayhaveborkedit/discord-ext-voice-recv
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
name,type,locations,ap_head,ap_body,ap_larm,ap_rarm,ap_lleg,ap_rleg,max_ag,traits,weight,availability,source,notes
|
|
||||||
Flak Vest,armor,"Body",0,4,0,0,0,0,,Flak,7,Common,"DH2 CRB","PRZYKŁAD – ZASTĄP"
|
|
||||||
Power Armour (Astartes),armor,"All",8,10,8,8,9,9,,Environmental; Auto-senses,100,"Very Rare","DW CRB","PRZYKŁAD – ZASTĄP"
|
|
||||||
|
@@ -1,60 +0,0 @@
|
|||||||
// DH2 Attack Test v2 — presets: Aim/Range/Fire + Size/Light/Cover
|
|
||||||
const actor = canvas.tokens.controlled[0]?.actor ?? game.user.character;
|
|
||||||
if (!actor) return ui.notifications.warn("Zaznacz token albo przypisz postać.");
|
|
||||||
new Dialog({
|
|
||||||
title: "🎯 Attack Test (WS/BS)",
|
|
||||||
content: `
|
|
||||||
<form>
|
|
||||||
<div class="form-group"><label>Base Target (WS/BS)</label><input name="base" type="number" value="40"/></div>
|
|
||||||
<div class="form-group"><label>Aim</label>
|
|
||||||
<select name="aim"><option value="0">None</option><option value="10">Half (+10)</option><option value="20">Full (+20)</option></select></div>
|
|
||||||
<div class="form-group"><label>Range</label>
|
|
||||||
<select name="range">
|
|
||||||
<option value="0">Standard</option>
|
|
||||||
<option value="30">Point Blank (+30)</option>
|
|
||||||
<option value="10">Short (+10)</option>
|
|
||||||
<option value="-10">Long (-10)</option>
|
|
||||||
<option value="-30">Extreme (-30)</option>
|
|
||||||
</select></div>
|
|
||||||
<div class="form-group"><label>Fire / Attack</label>
|
|
||||||
<select name="stance">
|
|
||||||
<option value="0">Standard / Single</option>
|
|
||||||
<option value="10">Semi (+10)</option>
|
|
||||||
<option value="-10">Full Auto (-10)</option>
|
|
||||||
<option value="30">All Out (Melee +30)</option>
|
|
||||||
</select></div>
|
|
||||||
<div class="form-group"><label>Target Size</label>
|
|
||||||
<select name="size">
|
|
||||||
<option value="0">Average</option>
|
|
||||||
<option value="10">Hulking (+10)</option>
|
|
||||||
<option value="20">Enormous (+20)</option>
|
|
||||||
<option value="30">Massive (+30)</option>
|
|
||||||
<option value="-10">Puny (-10)</option>
|
|
||||||
</select></div>
|
|
||||||
<div class="form-group"><label>Lighting</label>
|
|
||||||
<select name="light"><option value="0">Normal</option><option value="10">Good (+10)</option><option value="-10">Poor (-10)</option></select></div>
|
|
||||||
<div class="form-group"><label>Cover</label>
|
|
||||||
<select name="cover"><option value="0">None</option><option value="-10">Light (-10)</option><option value="-20">Heavy (-20)</option></select></div>
|
|
||||||
<div class="form-group"><label>Other Modifiers</label><input name="mod" type="number" value="0"/></div>
|
|
||||||
</form>`,
|
|
||||||
buttons: {
|
|
||||||
roll: {
|
|
||||||
label: "Roll",
|
|
||||||
callback: async (html) => {
|
|
||||||
const get = n => Number(html.find(`[name="${n}"]`).val());
|
|
||||||
const base = get("base");
|
|
||||||
const total = base + get("aim") + get("range") + get("stance") + get("size") + get("light") + get("cover") + get("mod");
|
|
||||||
const r = await(new Roll("1d100")).roll({async:true});
|
|
||||||
const ok = r.total <= total;
|
|
||||||
const margin = Math.abs(total - r.total);
|
|
||||||
const dox = ok ? 1 + Math.floor(margin/10) : Math.floor(margin/10);
|
|
||||||
const table = `
|
|
||||||
<table style="width:100%;border-collapse:collapse">
|
|
||||||
<tr><td><b>Target</b></td><td>${total}</td><td><b>Roll</b></td><td>${r.total}</td></tr>
|
|
||||||
<tr><td><b>Result</b></td><td colspan="3">${ok?'<span style="color:green">SUCCESS</span>':'<span style="color:red">FAIL</span>'} — ${ok?dox+' DoS':dox+' DoF'}</td></tr>
|
|
||||||
</table>`;
|
|
||||||
r.toMessage({speaker: ChatMessage.getSpeaker({actor}), flavor: `🎯 <b>Attack Test</b><br/>${table}`});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).render(true);
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
Roll,Result
|
|
||||||
1,Energy/Head — wpis 1
|
|
||||||
2,Energy/Head — wpis 2
|
|
||||||
3,Energy/Head — wpis 3
|
|
||||||
4,Energy/Head — wpis 4
|
|
||||||
5,Energy/Head — wpis 5
|
|
||||||
|
@@ -1,50 +0,0 @@
|
|||||||
// 🧠 Focus Power (DH2) — WP test + Phenomena/Perils with mode presets
|
|
||||||
const actor = canvas.tokens.controlled[0]?.actor ?? game.user.character;
|
|
||||||
if (!actor) return ui.notifications.warn("Zaznacz token.");
|
|
||||||
new Dialog({
|
|
||||||
title: "🧠 Focus Power",
|
|
||||||
content: `
|
|
||||||
<form>
|
|
||||||
<div class="form-group"><label>Willpower (target)</label><input name="wp" type="number" value="40"/></div>
|
|
||||||
<div class="form-group"><label>Psychic Rating (PR)</label><input name="pr" type="number" value="3"/></div>
|
|
||||||
<div class="form-group"><label>Mode</label>
|
|
||||||
<select name="mode"><option value="fettered">Fettered (no PP; PR/2)</option><option value="unfettered" selected>Unfettered (PP on doubles)</option><option value="push">Push (always PP; +PR)</option></select></div>
|
|
||||||
<div class="form-group"><label>Power difficulty/gear/etc. (flat mod)</label><input name="flat" type="number" value="0"/></div>
|
|
||||||
<div class="form-group"><label>Perils threshold</label><input name="thr" type="number" value="75"/></div>
|
|
||||||
</form>`,
|
|
||||||
buttons: {
|
|
||||||
roll: { label: "Roll", callback: async html => {
|
|
||||||
const wp = Number(html.find('[name="wp"]').val());
|
|
||||||
const pr = Number(html.find('[name="pr"]').val());
|
|
||||||
const mode = html.find('[name="mode"]').val();
|
|
||||||
const flat = Number(html.find('[name="flat"]').val());
|
|
||||||
const thr = Number(html.find('[name="thr"]').val());
|
|
||||||
let effPR = pr, ppmod = 0, ppAlways = false, note = "";
|
|
||||||
if (mode==="fettered"){ effPR = Math.max(1, Math.floor(pr/2)); note="(Fettered: PR/2, brak Phenomena)"; }
|
|
||||||
if (mode==="push"){ effPR = pr+3; ppmod=10; ppAlways = true; note="(Push: +3 PR, Phenomena zawsze, +10)"; }
|
|
||||||
const target = wp + flat;
|
|
||||||
const roll = await (new Roll("1d100")).roll({async:true});
|
|
||||||
const ok = roll.total <= target;
|
|
||||||
const dos = ok ? 1 + Math.floor((target - roll.total)/10) : Math.floor((roll.total - target)/10);
|
|
||||||
const doubles = (roll.total%11===0) || (roll.total===100);
|
|
||||||
const info = `<table style="width:100%;border-collapse:collapse">
|
|
||||||
<tr><td><b>Target</b></td><td>${target}</td><td><b>Roll</b></td><td>${roll.total}</td></tr>
|
|
||||||
<tr><td><b>Result</b></td><td colspan="3">${ok?'<span style="color:green">SUCCESS</span>':'<span style="color:red">FAIL</span>'} — ${ok?dos+' DoS':dos+' DoF'} ${doubles?' — <b>DOUBLES</b>':''}</td></tr>
|
|
||||||
<tr><td><b>Eff. PR</b></td><td>${effPR}</td><td><b>Range hint</b></td><td>${effPR*10} m (jeśli moc tak działa)</td></tr>
|
|
||||||
</table>`;
|
|
||||||
roll.toMessage({speaker: ChatMessage.getSpeaker({actor}), flavor:`🧠 <b>Focus Power</b> ${note}<br/>${info}`});
|
|
||||||
const needPP = (mode==="unfettered" && doubles) || (mode==="push") ;
|
|
||||||
if (needPP){
|
|
||||||
const tbl = game.tables.getName("Psychic Phenomena");
|
|
||||||
if (tbl){
|
|
||||||
const r = await (new Roll(`1d100 + ${ppmod}`)).roll({async:true});
|
|
||||||
await tbl.draw({displayResults:true, roll:r});
|
|
||||||
if (r.total >= thr){
|
|
||||||
const per = game.tables.getName("Perils of the Warp");
|
|
||||||
if (per) await per.draw({displayResults:true});
|
|
||||||
}
|
|
||||||
} else ChatMessage.create({content:"Utwórz RollTable: <b>Psychic Phenomena</b> (+ <b>Perils of the Warp</b>)"});
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
}).render(true);
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
// 🎯 Hit Location (DH mapping by reversed roll)
|
|
||||||
new Dialog({
|
|
||||||
title:"🎯 Hit Location",
|
|
||||||
content:`<form>
|
|
||||||
<div class="form-group"><label>Attack d100 roll</label><input name="roll" type="number" value="37"/></div>
|
|
||||||
</form>`,
|
|
||||||
buttons:{
|
|
||||||
go:{label:"Resolve", callback: html=>{
|
|
||||||
const n = Math.max(1, Math.min(100, Number(html.find('[name="roll"]').val())));
|
|
||||||
const rev = Number(String(n).padStart(2,"0").split("").reverse().join(""));
|
|
||||||
let loc = "";
|
|
||||||
if (rev<=10) loc="Head";
|
|
||||||
else if (rev<=20) loc="Right Arm";
|
|
||||||
else if (rev<=30) loc="Left Arm";
|
|
||||||
else if (rev<=70) loc="Body";
|
|
||||||
else if (rev<=85) loc="Right Leg";
|
|
||||||
else loc="Left Leg";
|
|
||||||
ChatMessage.create({content:`🎯 <b>Hit Location</b>: roll ${n} → reversed ${rev} → <b>${loc}</b>`});
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
}).render(true);
|
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
const text=`<b>House Rules — DH2 chassis</b><br/>
|
|
||||||
• Unnatural mapowanie: dawne ×2 bonus → Unnatural (+X) tak, by SB/TB odzwierciedlały linię źródłową.<br/>
|
|
||||||
• Pancerz Astartes: zachowaj AP; zużycie zasilania jako +1 Fatigue co X scen zamiast liczenia minut.<br/>
|
|
||||||
• Aptitudes: archetypy z RT/DW/BC mają przypisane 2–3 Aptitudes DH2 dla kosztów XP.<br/>
|
|
||||||
• Psy: testy i PR z DH2 dla wszystkich; GK posiada Aegis Discipline (1×/scena reroll Perils) + 1–2 signature powers z DW.<br/>
|
|
||||||
• RT Acquisition → DH2 Influence z modyfikatorami kontekstowymi (teatr działań, mandat Inkwizycji, czas).`;ChatMessage.create({content:text});
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
// 🚦 Initiative for selected tokens (1d10 + AG Bonus prompt)
|
|
||||||
if (!canvas.tokens.controlled.length) return ui.notifications.warn("Zaznacz co najmniej jeden token.");
|
|
||||||
const combat = game.combat ?? await Combat.implementation.create({});
|
|
||||||
for (const t of canvas.tokens.controlled){
|
|
||||||
if (!combat.combatants.some(c=>c.tokenId===t.id)) await combat.createEmbeddedDocuments("Combatant",[ {tokenId:t.id, sceneId: canvas.scene.id, hidden:false} ]);
|
|
||||||
const ag = Number(await Dialog.prompt({title:`AG Bonus for ${t.name}`, content:`<input type="number" value="4">`, label:"OK"}));
|
|
||||||
const r = await (new Roll(`1d10 + ${ag}`)).roll({async:true});
|
|
||||||
await combat.setInitiative(combat.combatants.find(c=>c.tokenId===t.id).id, r.total);
|
|
||||||
r.toMessage({flavor:`🚦 <b>Initiative</b> — ${t.name}: ${r.total}`});
|
|
||||||
}
|
|
||||||
ui.notifications.info("Inicjatywy ustawione.");
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
Roll,Result
|
|
||||||
1-5,Perils 1–5 — WPISZ
|
|
||||||
6-10,Perils 6–10 — WPISZ
|
|
||||||
11-15,Perils 11–15 — WPISZ
|
|
||||||
16-20,Perils 16–20 — WPISZ
|
|
||||||
21-25,Perils 21–25 — WPISZ
|
|
||||||
26-30,Perils 26–30 — WPISZ
|
|
||||||
31-35,Perils 31–35 — WPISZ
|
|
||||||
36-40,Perils 36–40 — WPISZ
|
|
||||||
41-45,Perils 41–45 — WPISZ
|
|
||||||
46-50,Perils 46–50 — WPISZ
|
|
||||||
51-55,Perils 51–55 — WPISZ
|
|
||||||
56-60,Perils 56–60 — WPISZ
|
|
||||||
61-65,Perils 61–65 — WPISZ
|
|
||||||
66-70,Perils 66–70 — WPISZ
|
|
||||||
71-75,Perils 71–75 — WPISZ
|
|
||||||
76-80,Perils 76–80 — WPISZ
|
|
||||||
81-85,Perils 81–85 — WPISZ
|
|
||||||
86-90,Perils 86–90 — WPISZ
|
|
||||||
91-95,Perils 91–95 — WPISZ
|
|
||||||
96-100,Perils 96–100 — WPISZ
|
|
||||||
|
@@ -1,3 +0,0 @@
|
|||||||
name,type,discipline,action,test,range,sustained,effect,source,notes
|
|
||||||
Smite,power,Biomancy,Half,"WP Challenging (+0)","PR*10m",No,"1d10+PR E; Tearing","DH2 CRB","PRZYKŁAD – ZASTĄP"
|
|
||||||
Foreboding,power,Divination,Reaction,"Per Difficult (-10)","Self",No,"Use as Evasion; DoS rules","DH2 CRB","PRZYKŁAD – ZASTĄP"
|
|
||||||
|
-21
@@ -1,21 +0,0 @@
|
|||||||
Roll,Result
|
|
||||||
1-5,PP 1–5 — WPISZ
|
|
||||||
6-10,PP 6–10 — WPISZ
|
|
||||||
11-15,PP 11–15 — WPISZ
|
|
||||||
16-20,PP 16–20 — WPISZ
|
|
||||||
21-25,PP 21–25 — WPISZ
|
|
||||||
26-30,PP 26–30 — WPISZ
|
|
||||||
31-35,PP 31–35 — WPISZ
|
|
||||||
36-40,PP 36–40 — WPISZ
|
|
||||||
41-45,PP 41–45 — WPISZ
|
|
||||||
46-50,PP 46–50 — WPISZ
|
|
||||||
51-55,PP 51–55 — WPISZ
|
|
||||||
56-60,PP 56–60 — WPISZ
|
|
||||||
61-65,PP 61–65 — WPISZ
|
|
||||||
66-70,PP 66–70 — WPISZ
|
|
||||||
71-75,PP 71–75 — WPISZ
|
|
||||||
76-80,PP 76–80 — WPISZ
|
|
||||||
81-85,PP 81–85 — WPISZ
|
|
||||||
86-90,PP 86–90 — WPISZ
|
|
||||||
91-95,PP 91–95 — WPISZ
|
|
||||||
96-100,PP 96–100 — WPISZ
|
|
||||||
|
@@ -1,28 +0,0 @@
|
|||||||
// 🩹 Toggle conditions on selected tokens (Foundry v13)
|
|
||||||
const choices = [
|
|
||||||
{id:"fatigued", label:"Fatigued"},
|
|
||||||
{id:"stunned", label:"Stunned"},
|
|
||||||
{id:"prone", label:"Prone"},
|
|
||||||
{id:"frightened", label:"Frightened (Fear)"}
|
|
||||||
];
|
|
||||||
const opts = choices.map(c=>`<label><input type="checkbox" name="c" value="${c.id}"> ${c.label}</label>`).join("<br/>");
|
|
||||||
new Dialog({
|
|
||||||
title:"🩹 Conditions",
|
|
||||||
content:`<form>${opts}<div class="form-group"><label>Mode</label>
|
|
||||||
<select name="mode"><option value="toggle">Toggle</option><option value="on">Apply</option><option value="off">Remove</option></select></div></form>`,
|
|
||||||
buttons:{
|
|
||||||
go:{label:"Apply",callback: html=>{
|
|
||||||
const ids = Array.from(html.find('input[name="c"]:checked')).map(e=>e.value);
|
|
||||||
const mode = html.find('[name="mode"]').val();
|
|
||||||
const getEf = id => CONFIG.statusEffects.find(e=>e.id===id) ?? {id};
|
|
||||||
canvas.tokens.controlled.forEach(t=>{
|
|
||||||
ids.forEach(id=>{
|
|
||||||
if (mode==="toggle") t.toggleEffect(getEf(id));
|
|
||||||
else if (mode==="on") t.actor?.effects?.some(e=>e.getFlag("core","statusId")===id) ? null : t.toggleEffect(getEf(id));
|
|
||||||
else if (mode==="off") t.actor?.effects?.some(e=>e.getFlag("core","statusId")===id) ? t.toggleEffect(getEf(id)) : null;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
}).render(true);
|
|
||||||
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
// 💥 Quick Damage — supports Tearing, Proven(X), Primitive(X), flat mod
|
|
||||||
new Dialog({
|
|
||||||
title:"💥 Damage Roller",
|
|
||||||
content: `
|
|
||||||
<form>
|
|
||||||
<div class="form-group"><label>Flat modifier (e.g., +3)</label><input name="mod" type="number" value="0"/></div>
|
|
||||||
<div class="form-group"><label>Traits</label>
|
|
||||||
<label><input type="checkbox" name="tear"> Tearing</label>
|
|
||||||
<label><input type="checkbox" name="prov"> Proven</label>
|
|
||||||
<input name="provV" type="number" value="0" style="width:60px" placeholder="X"/>
|
|
||||||
<label><input type="checkbox" name="prim"> Primitive</label>
|
|
||||||
<input name="primV" type="number" value="0" style="width:60px" placeholder="X"/>
|
|
||||||
</div>
|
|
||||||
</form>`,
|
|
||||||
buttons:{
|
|
||||||
go:{label:"Roll", callback: async html=>{
|
|
||||||
const mod = Number(html.find('[name="mod"]').val());
|
|
||||||
const tearing = html.find('[name="tear"]')[0].checked;
|
|
||||||
const proven = html.find('[name="prov"]')[0].checked ? Number(html.find('[name="provV"]').val()) : 0;
|
|
||||||
const primitive = html.find('[name="prim"]')[0].checked ? Number(html.find('[name="primV"]').val()) : 0;
|
|
||||||
// base die (d10) with tearing (best of 2)
|
|
||||||
const r1 = await (new Roll("1d10")).roll({async:true});
|
|
||||||
const r2 = tearing ? await (new Roll("1d10")).roll({async:true}) : null;
|
|
||||||
let die = tearing ? Math.max(r1.total, r2.total) : r1.total;
|
|
||||||
// apply Proven/Primitive
|
|
||||||
if (proven>0) die = Math.max(die, proven);
|
|
||||||
if (primitive>0) die = Math.min(die, primitive);
|
|
||||||
const rf = (die===10); // potential Zealous Hatred trigger
|
|
||||||
const total = die + mod;
|
|
||||||
let flavor = `💥 <b>Damage</b><br/>Die: ${die}${tearing?` (Tearing ${r1.total}/${r2.total.total})`:''} + Mod ${mod} = <b>${total}</b>`;
|
|
||||||
if (proven>0) flavor += `<br/>Proven(${proven}) zastosowano`;
|
|
||||||
if (primitive>0) flavor += `<br/>Primitive(${primitive}) zastosowano`;
|
|
||||||
if (rf) flavor += `<br/><b>⚡ Natural 10</b> — rozważ Zealous Hatred.`;
|
|
||||||
ChatMessage.create({speaker: ChatMessage.getSpeaker(), content: flavor});
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
}).render(true);
|
|
||||||
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
// 📚 Create placeholder RollTables for Crits + Psychic Phenomena/Perils (with icons)
|
|
||||||
const icon = {Energy:"⚡", Impact:"🔨", Rending:"🗡️", Explosive:"💥"};
|
|
||||||
const dmgTypes = ["Energy","Impact","Rending","Explosive"];
|
|
||||||
const locs = ["Head","Body","Left Arm","Right Arm","Left Leg","Right Leg"];
|
|
||||||
async function makeCrit(dtype, loc){
|
|
||||||
const name = `Crit: ${dtype} - ${loc}`;
|
|
||||||
if (game.tables.getName(name)) return;
|
|
||||||
const results = [];
|
|
||||||
for (let i=1;i<=5;i++){
|
|
||||||
results.push({type:0, text:`${icon[dtype]||""} ${dtype}/${loc} — wpis ${i} (uzupełnij z PDF)`, weight:1, range:[i,i]});
|
|
||||||
}
|
|
||||||
await RollTable.implementation.create({name, formula:"1d5", replacement:true, displayRoll:false, results});
|
|
||||||
}
|
|
||||||
async function makeWide(name, emoji){
|
|
||||||
if (game.tables.getName(name)) return;
|
|
||||||
const results = [];
|
|
||||||
for (let i=0;i<20;i++){
|
|
||||||
const lo=i*5+1, hi=i*5+5;
|
|
||||||
results.push({type:0, text:`${emoji} ${name} ${lo}-${hi} — wpis (uzupełnij z PDF)`, weight:1, range:[lo,hi]});
|
|
||||||
}
|
|
||||||
await RollTable.implementation.create({name, formula:"1d100", replacement:true, displayRoll:false, results});
|
|
||||||
}
|
|
||||||
for (const d of dmgTypes) for (const l of locs) await makeCrit(d,l);
|
|
||||||
await makeWide("Psychic Phenomena","🌀");
|
|
||||||
await makeWide("Perils of the Warp","☠️");
|
|
||||||
ui.notifications.info("Utworzono puste tabele: Crits (4×6) + Phenomena + Perils.");
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
name,type,tier,aptitudes,prereq,effect,source,notes
|
|
||||||
Ambidextrous,talent,1,"Agility; Offence","Ag 30","-10 to off-hand penalty","DH2 CRB","PRZYKŁAD – ZASTĄP"
|
|
||||||
Aegis Discipline (GK),talent,2,"Willpower; Defence","Psyker","Reroll Perils 1×scene","HOUSE","DODAJ WŁASNY OPIS"
|
|
||||||
|
@@ -1,3 +0,0 @@
|
|||||||
name,type,subtype,damage,pen,range,rof,qualities,weight,availability,source,notes
|
|
||||||
Lasgun M36,weapon,Basic,"1d10+3",0,100m,"S/3/–","Reliable",4,Common,"DH2 CRB p.142","PRZYKŁAD – ZASTĄP"
|
|
||||||
Astartes Bolter,weapon,Basic,"1d10+9",4,90m,"S/2/–","Tearing; Unreliable",9,Rare,"DW CRB","PRZYKŁAD – ZASTĄP"
|
|
||||||
|
@@ -1,33 +0,0 @@
|
|||||||
// ⚡ Zealous Hatred helper (DH2) — roll 1d5 crit OR add +1d5 dmg
|
|
||||||
new Dialog({
|
|
||||||
title: "⚡ Zealous Hatred",
|
|
||||||
content: `
|
|
||||||
<form>
|
|
||||||
<div class="form-group"><label>Wound damage after Armour/TB?</label>
|
|
||||||
<select name="penetrated"><option value="yes">Yes → roll Critical (1d5)</option><option value="no">No → add +1d5 damage</option></select></div>
|
|
||||||
<div class="form-group"><label>Damage Type</label>
|
|
||||||
<select name="dtype"><option>Energy</option><option>Impact</option><option>Rending</option><option>Explosive</option></select></div>
|
|
||||||
<div class="form-group"><label>Hit Location</label>
|
|
||||||
<select name="loc"><option>Head</option><option>Body</option><option>Left Arm</option><option>Right Arm</option><option>Left Leg</option><option>Right Leg</option></select></div>
|
|
||||||
<div class="form-group"><label>Table name (optional override)</label><input name="tname" type="text" placeholder="Crit: Energy - Head"/></div>
|
|
||||||
</form>`,
|
|
||||||
buttons: {
|
|
||||||
go: { label: "Resolve", callback: async html => {
|
|
||||||
const pen = html.find('[name="penetrated"]').val();
|
|
||||||
if (pen === "yes") {
|
|
||||||
const dtype = html.find('[name="dtype"]').val();
|
|
||||||
const loc = html.find('[name="loc"]').val();
|
|
||||||
const override = html.find('[name="tname"]').val()?.trim();
|
|
||||||
const name = override || `Crit: ${dtype} - ${loc}`;
|
|
||||||
const r = await (new Roll("1d5")).roll({async:true});
|
|
||||||
const table = game.tables.getName(name);
|
|
||||||
if (table) await table.draw({displayResults:true, roll:r});
|
|
||||||
else r.toMessage({flavor:`⚡ <b>Zealous Hatred</b>: Critical ${r.total} — brak tabeli <b>${name}</b> (utwórz lub zmień nazwę).`});
|
|
||||||
} else {
|
|
||||||
const r = await (new Roll("1d5")).roll({async:true});
|
|
||||||
r.toMessage({flavor:"⚡ <b>Zealous Hatred</b>: Dodaj do obrażeń <b>+1d5</b> (atak nie przebił Soak)."});
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
}).render(true);
|
|
||||||
|
|
||||||
@@ -0,0 +1,296 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Buduje nową gałąź z "próbkowanych" commitów na podstawie tagów z bieżącej gałęzi,
|
||||||
|
a na końcu dodaje stan bieżącego HEAD (jeśli nie jest otagowany).
|
||||||
|
Autor: (drop-in)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from typing import List, Tuple, Optional
|
||||||
|
|
||||||
|
# --- Logging setup ---
|
||||||
|
logger = logging.getLogger("tag_branch_builder")
|
||||||
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
|
formatter = logging.Formatter("%(levelname)s: %(message)s")
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
class GitError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def run_git(args: List[str], cwd: Optional[str] = None, check: bool = True) -> str:
|
||||||
|
"""Run a git command and return stdout (stripped)."""
|
||||||
|
cmd = ["git"] + args
|
||||||
|
logger.debug("Running: %s", " ".join(cmd))
|
||||||
|
try:
|
||||||
|
proc = subprocess.run(
|
||||||
|
cmd,
|
||||||
|
cwd=cwd,
|
||||||
|
check=False,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception("Nie udało się uruchomić gita.") from e
|
||||||
|
raise
|
||||||
|
|
||||||
|
if check and proc.returncode != 0:
|
||||||
|
logger.error("Git error (%s): %s", " ".join(cmd), proc.stderr.strip())
|
||||||
|
raise GitError(proc.stderr.strip())
|
||||||
|
return proc.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_git_repo() -> None:
|
||||||
|
try:
|
||||||
|
run_git(["rev-parse", "--is-inside-work-tree"])
|
||||||
|
except Exception:
|
||||||
|
logger.exception("To nie wygląda na repozytorium git.")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_clean_worktree() -> None:
|
||||||
|
# Untracked + changes
|
||||||
|
status = run_git(["status", "--porcelain"])
|
||||||
|
if status.strip():
|
||||||
|
raise GitError(
|
||||||
|
"Drzewo robocze nie jest czyste. Zacommituj/stashuj zmiany i spróbuj ponownie."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def current_branch() -> str:
|
||||||
|
# Returns branch or 'HEAD' when detached
|
||||||
|
ref = run_git(["rev-parse", "--abbrev-ref", "HEAD"])
|
||||||
|
return ref
|
||||||
|
|
||||||
|
|
||||||
|
def head_sha() -> str:
|
||||||
|
return run_git(["rev-parse", "HEAD"])
|
||||||
|
|
||||||
|
|
||||||
|
def sha_has_tag(sha: str) -> List[str]:
|
||||||
|
# tags pointing at sha
|
||||||
|
tags = run_git(["tag", "--points-at", sha])
|
||||||
|
return [t for t in tags.splitlines() if t.strip()]
|
||||||
|
|
||||||
|
|
||||||
|
def tags_merged_into(branch: str) -> List[str]:
|
||||||
|
out = run_git(["tag", "--merged", branch])
|
||||||
|
return [t for t in out.splitlines() if t.strip()]
|
||||||
|
|
||||||
|
|
||||||
|
def tag_commit_sha(tag: str) -> str:
|
||||||
|
return run_git(["rev-list", "-n", "1", tag])
|
||||||
|
|
||||||
|
|
||||||
|
def commit_unix_time(sha: str) -> int:
|
||||||
|
return int(run_git(["show", "-s", "--format=%ct", sha]))
|
||||||
|
|
||||||
|
|
||||||
|
def sort_tags_by_commit_time(tags: List[str]) -> List[Tuple[str, str, int]]:
|
||||||
|
triples = []
|
||||||
|
for t in tags:
|
||||||
|
sha = tag_commit_sha(t)
|
||||||
|
ts = commit_unix_time(sha)
|
||||||
|
triples.append((t, sha, ts))
|
||||||
|
triples.sort(key=lambda x: x[2]) # oldest first
|
||||||
|
return triples
|
||||||
|
|
||||||
|
|
||||||
|
def list_intermediate_messages(old_sha: Optional[str], new_sha: str) -> List[str]:
|
||||||
|
"""
|
||||||
|
Zwróć listę komunikatów commitów POŚREDNICH (wyłącznie) od old_sha do new_sha.
|
||||||
|
Kolejność: od najstarszego do najnowszego.
|
||||||
|
"""
|
||||||
|
if old_sha is None:
|
||||||
|
# Nie ma commitów pośrednich przed pierwszym tagiem
|
||||||
|
return []
|
||||||
|
# ancestry-path: tylko ścieżka od old_sha do new_sha (jeśli wiele rodziców)
|
||||||
|
# Zakres old_sha..new_sha zawiera new_sha, dlatego pominiemy go w output.
|
||||||
|
rng = f"{old_sha}..{new_sha}"
|
||||||
|
try:
|
||||||
|
out = run_git(
|
||||||
|
["log", "--format=%s", "--reverse", "--ancestry-path", rng], check=True
|
||||||
|
)
|
||||||
|
except GitError:
|
||||||
|
# Brak ścieżki (np. tag nie jest potomkiem old_sha) – wtedy nie ma pośrednich
|
||||||
|
return []
|
||||||
|
msgs = [ln for ln in out.splitlines() if ln.strip()]
|
||||||
|
if msgs:
|
||||||
|
# Ostatnia pozycja może być new_sha (w praktyce git log na %s nie odróżnia, ale
|
||||||
|
# jeśli zakres zwróci message z new_sha na końcu, usuniemy go porównując SHA).
|
||||||
|
# Prostsze: usuń ostatni wpis, bo to najnowszy (new_sha).
|
||||||
|
msgs = msgs[:-1]
|
||||||
|
return msgs
|
||||||
|
|
||||||
|
|
||||||
|
def checkout_orphan_branch(new_branch: str) -> None:
|
||||||
|
run_git(["checkout", "--orphan", new_branch])
|
||||||
|
# Usuń wszystko z indeksu i roboczego (z wyjątkiem .git)
|
||||||
|
# Najpierw usuń śledzone:
|
||||||
|
run_git(["rm", "-r", "--quiet", "--cached", "--force", "."], check=False)
|
||||||
|
# Potem pliki robocze:
|
||||||
|
for root, dirs, files in os.walk(".", topdown=False):
|
||||||
|
# pomiń .git
|
||||||
|
if root.startswith("./.git") or root == ".git":
|
||||||
|
continue
|
||||||
|
for name in files:
|
||||||
|
try:
|
||||||
|
os.remove(os.path.join(root, name))
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
for name in dirs:
|
||||||
|
p = os.path.join(root, name)
|
||||||
|
if p == "./.git":
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
os.rmdir(p)
|
||||||
|
except OSError:
|
||||||
|
# Niepuste – to OK, wyczyścimy przy checkout
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def replace_worktree_with_commit(sha: str) -> None:
|
||||||
|
"""
|
||||||
|
Nadpisz zawartość roboczą drzewem z commit-a sha.
|
||||||
|
"""
|
||||||
|
# Najpierw usuń aktualne pliki (również nieśledzone), potem wczytaj tree wybranego commita:
|
||||||
|
# 1) git checkout <sha> -- . (zapisze pliki do working tree + index)
|
||||||
|
# 2) git add -A
|
||||||
|
# Aby dopilnować usunięć: zrób czyszczenie przez git rm -r ., potem checkout.
|
||||||
|
run_git(["rm", "-r", "--quiet", "--ignore-unmatch", "."], check=False)
|
||||||
|
# Przywróć pliki ze wskazanego commita:
|
||||||
|
# Uwaga: jeśli repo ma submoduły/large files – to wykracza poza zakres, ale zadziała dla standardowych plików.
|
||||||
|
run_git(["checkout", sha, "--", "."])
|
||||||
|
run_git(["add", "-A"])
|
||||||
|
|
||||||
|
|
||||||
|
def build_commit_message_for_tag(tag: str, intermediates: List[str]) -> str:
|
||||||
|
if not intermediates:
|
||||||
|
return f"Tag: {tag}"
|
||||||
|
lines = ["Tag: " + tag, "", "Intermediate commits (oldest → newest):"]
|
||||||
|
lines += [f"- {m}" for m in intermediates]
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def build_commit_message_for_head(since_tag: Optional[str], intermediates: List[str]) -> str:
|
||||||
|
title = "HEAD (unreleased)"
|
||||||
|
hdr = title if since_tag is None else f"{title} since tag {since_tag}"
|
||||||
|
if not intermediates:
|
||||||
|
return hdr
|
||||||
|
lines = [hdr, "", "Intermediate commits (oldest → newest):"]
|
||||||
|
lines += [f"- {m}" for m in intermediates]
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def commit_all(message: str) -> None:
|
||||||
|
# Zacommituj wszystko co w indeksie (po replace_worktree_with_commit daliśmy add -A)
|
||||||
|
run_git(["commit", "-m", message])
|
||||||
|
|
||||||
|
|
||||||
|
def push_new_branch(remote_url: str, branch: str) -> None:
|
||||||
|
# Dodaj zdalny 'newrepo' jeśli nie istnieje, ustaw URL i wypchnij
|
||||||
|
remotes = run_git(["remote"]).splitlines()
|
||||||
|
if "newrepo" not in remotes:
|
||||||
|
run_git(["remote", "add", "newrepo", remote_url])
|
||||||
|
else:
|
||||||
|
# podmień URL na wszelki wypadek
|
||||||
|
run_git(["remote", "set-url", "newrepo", remote_url])
|
||||||
|
run_git(["push", "-u", "newrepo", f"{branch}:{branch}"])
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Zbuduj nową gałąź na podstawie tagów z bieżącej gałęzi."
|
||||||
|
)
|
||||||
|
parser.add_argument("new_branch", help="Nazwa nowej gałęzi do utworzenia")
|
||||||
|
parser.add_argument(
|
||||||
|
"--new-repo-url",
|
||||||
|
dest="new_repo_url",
|
||||||
|
default=None,
|
||||||
|
help="(Opcjonalnie) adres URL nowego zdalnego repo – zostanie dodany jako 'newrepo' i wykonany push nowej gałęzi.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-v", "--verbose", action="store_true", help="Bardziej gadatliwe logi"
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.verbose:
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
try:
|
||||||
|
ensure_git_repo()
|
||||||
|
ensure_clean_worktree()
|
||||||
|
base_branch = current_branch()
|
||||||
|
base_head = head_sha()
|
||||||
|
logger.info("Bieżąca gałąź: %s", base_branch)
|
||||||
|
logger.info("HEAD: %s", base_head[:12])
|
||||||
|
|
||||||
|
# Zbierz tagi osiągalne z bieżącej gałęzi
|
||||||
|
merged_tags = tags_merged_into(base_branch)
|
||||||
|
if not merged_tags:
|
||||||
|
logger.warning(
|
||||||
|
"Nie znaleziono tagów osiągalnych z bieżącej gałęzi. Gałąź zostanie zbudowana tylko z HEAD."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Posortuj tagi po czasie commita
|
||||||
|
sorted_tags = sort_tags_by_commit_time(merged_tags)
|
||||||
|
|
||||||
|
# Sprawdź czy HEAD jest otagowany
|
||||||
|
head_tags = sha_has_tag(base_head)
|
||||||
|
head_is_tagged = bool(head_tags)
|
||||||
|
|
||||||
|
# Utwórz orphan branch
|
||||||
|
logger.info("Tworzę sierocą gałąź: %s", args.new_branch)
|
||||||
|
checkout_orphan_branch(args.new_branch)
|
||||||
|
|
||||||
|
prev_sha: Optional[str] = None
|
||||||
|
last_tag_name: Optional[str] = None
|
||||||
|
|
||||||
|
# Dla każdego tagu – commit z jego zawartości
|
||||||
|
for tag_name, tag_sha, _ts in sorted_tags:
|
||||||
|
logger.info("Przetwarzam tag: %s (%s)", tag_name, tag_sha[:12])
|
||||||
|
replace_worktree_with_commit(tag_sha)
|
||||||
|
intermediates = list_intermediate_messages(prev_sha, tag_sha)
|
||||||
|
msg = build_commit_message_for_tag(tag_name, intermediates)
|
||||||
|
commit_all(msg)
|
||||||
|
prev_sha = tag_sha
|
||||||
|
last_tag_name = tag_name
|
||||||
|
|
||||||
|
# Jeśli HEAD nie jest dokładnie ostatnim tagiem – dorzuć "unreleased"
|
||||||
|
if not head_is_tagged:
|
||||||
|
logger.info("Dodaję końcowy commit z bieżącego HEAD (unreleased).")
|
||||||
|
replace_worktree_with_commit(base_head)
|
||||||
|
inter = list_intermediate_messages(prev_sha, base_head)
|
||||||
|
msg = build_commit_message_for_head(last_tag_name, inter)
|
||||||
|
commit_all(msg)
|
||||||
|
else:
|
||||||
|
logger.info("HEAD jest oznaczony tagiem – kończę na ostatnim tagu.")
|
||||||
|
|
||||||
|
# Push do nowego repo jeśli podano
|
||||||
|
if args.new_repo_url:
|
||||||
|
logger.info("Wypycham nową gałąź do: %s", args.new_repo_url)
|
||||||
|
push_new_branch(args.new_repo_url, args.new_branch)
|
||||||
|
|
||||||
|
logger.info("Zakończono pomyślnie.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
except GitError as ge:
|
||||||
|
logger.error("Błąd gita: %s", ge)
|
||||||
|
return 2
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception("Nieoczekiwany błąd.") from e
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
+46
-26
@@ -2,22 +2,24 @@
|
|||||||
# trunk-ignore-all(bandit/B311)
|
# trunk-ignore-all(bandit/B311)
|
||||||
# pylint: disable=line-too-long
|
# pylint: disable=line-too-long
|
||||||
# pylint: disable=too-many-lines
|
# pylint: disable=too-many-lines
|
||||||
"""
|
"""Discord entrypoint for the Conjurer bot.
|
||||||
Module of a python bot named Conjurer - used to work on BDSM discord servers.
|
|
||||||
"""
|
|
||||||
import logging
|
|
||||||
|
|
||||||
# *=========================================== Standard Library Imports
|
The legacy bootstrap used threads and synchronous calls that made clean
|
||||||
|
shutdowns difficult. We now run everything from a single asyncio event loop
|
||||||
|
with cooperative shutdown signals so the bot can stop gracefully.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
import random
|
import random
|
||||||
import threading
|
import threading
|
||||||
from logging import handlers
|
from logging import handlers
|
||||||
|
|
||||||
# *==============Imported libraries
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
from communication_subroutine import comm_subroutine
|
from conjurer.backup_old_docker.communication_subroutine import comm_subroutine
|
||||||
from constants import ENCODING, LOGFILE, TOKEN
|
from conjurer.backup_old_docker.constants import ENCODING, LOGFILE, TOKEN
|
||||||
logger = logging.getLogger("discord")
|
logger = logging.getLogger("discord")
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
handler = handlers.RotatingFileHandler(
|
handler = handlers.RotatingFileHandler(
|
||||||
@@ -82,22 +84,40 @@ async def on_ready():
|
|||||||
logger.info("All systems: operational")
|
logger.info("All systems: operational")
|
||||||
|
|
||||||
|
|
||||||
# *================================== Run
|
async def _run_comm_subroutine(stop_event: threading.Event) -> None:
|
||||||
|
await asyncio.to_thread(comm_subroutine, stop_event)
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_bot(token: str, shutdown_event: asyncio.Event) -> None:
|
||||||
|
try:
|
||||||
|
await client.start(token, log_handler=None)
|
||||||
|
finally:
|
||||||
|
shutdown_event.set()
|
||||||
|
|
||||||
|
|
||||||
|
async def main() -> None:
|
||||||
|
if not TOKEN:
|
||||||
|
logger.error("Discord token missing – set DISCORD_TOKEN or configure netrc")
|
||||||
|
return
|
||||||
|
|
||||||
|
shutdown_event = asyncio.Event()
|
||||||
|
comm_stop_event = threading.Event()
|
||||||
|
|
||||||
|
comm_task = asyncio.create_task(_run_comm_subroutine(comm_stop_event))
|
||||||
|
bot_task = asyncio.create_task(_run_bot(TOKEN, shutdown_event))
|
||||||
|
|
||||||
|
try:
|
||||||
|
await shutdown_event.wait()
|
||||||
|
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||||
|
logger.info("Shutdown signal received")
|
||||||
|
comm_stop_event.set()
|
||||||
|
await client.close()
|
||||||
|
finally:
|
||||||
|
comm_stop_event.set()
|
||||||
|
if not client.is_closed():
|
||||||
|
await client.close()
|
||||||
|
await asyncio.gather(bot_task, comm_task, return_exceptions=True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
logger.info("Starting discord bot")
|
asyncio.run(main())
|
||||||
threads = []
|
|
||||||
logger.info("Starting discord bot: Creating threads")
|
|
||||||
threads.append(threading.Thread(target=client.run, args=(TOKEN,),kwargs={"log_handler":None}))
|
|
||||||
threads.append(threading.Thread(target=comm_subroutine))
|
|
||||||
logger.info("Starting discord bot: Starting threads")
|
|
||||||
WRK_CNT = 0
|
|
||||||
for worker in threads:
|
|
||||||
WRK_CNT += 1
|
|
||||||
logger.info("Starting discord bot: Starting thread %s", WRK_CNT)
|
|
||||||
worker.start()
|
|
||||||
logger.info("Starting discord bot: Joining threads")
|
|
||||||
WRK_CNT = 0
|
|
||||||
for worker in threads:
|
|
||||||
WRK_CNT += 1
|
|
||||||
logger.info("Starting discord bot: Joining thread %s", WRK_CNT)
|
|
||||||
worker.join()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user