Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
ea1814e166 | |||
77030f9092 |
231
plugin.py
231
plugin.py
@ -31,7 +31,10 @@
|
|||||||
from supybot import utils, plugins, ircutils, callbacks, world, conf, log
|
from supybot import utils, plugins, ircutils, callbacks, world, conf, log
|
||||||
from supybot.commands import *
|
from supybot.commands import *
|
||||||
|
|
||||||
|
# HTTP Imports
|
||||||
|
from supybot import httpserver
|
||||||
|
|
||||||
|
# Misc
|
||||||
from num2words import num2words
|
from num2words import num2words
|
||||||
import pickle
|
import pickle
|
||||||
import datetime
|
import datetime
|
||||||
@ -63,6 +66,110 @@ METHODS = {
|
|||||||
"smoked": ["Smoked"]
|
"smoked": ["Smoked"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TripsitServerCallback(httpserver.SupyHTTPServerCallback):
|
||||||
|
name = 'Tripsit'
|
||||||
|
defaultResponse = """
|
||||||
|
This plugin handles only GET request, please don't use other requests."""
|
||||||
|
|
||||||
|
def __init__(self, plugin):
|
||||||
|
self.plugin = plugin # to access db
|
||||||
|
|
||||||
|
def doGet(self, handler, path):
|
||||||
|
if path == '/doses':
|
||||||
|
# Collect all dose logs from self.db
|
||||||
|
dose_logs = []
|
||||||
|
for nick, data in self.plugin.db.items():
|
||||||
|
for dose in data.get('doses', []):
|
||||||
|
dose_logs.append({
|
||||||
|
'nick': nick,
|
||||||
|
'time': dose['time'],
|
||||||
|
'dose': dose['dose'],
|
||||||
|
'drug': dose['drug'],
|
||||||
|
'method': dose['method'],
|
||||||
|
})
|
||||||
|
|
||||||
|
# Create HTML response
|
||||||
|
html_response = """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Dose Logs</title>
|
||||||
|
<style>
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
table, th, td {
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
#filter {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 5px;
|
||||||
|
width: 300px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Dose Logs</h1>
|
||||||
|
<input type="text" id="filter" placeholder="Search for nicknames, drugs, etc.">
|
||||||
|
<table id="doseTable">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Nick</th>
|
||||||
|
<th>Time</th>
|
||||||
|
<th>Dose</th>
|
||||||
|
<th>Drug</th>
|
||||||
|
<th>Method</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
"""
|
||||||
|
# Add rows for each dose log
|
||||||
|
for log in dose_logs:
|
||||||
|
html_response += f"""
|
||||||
|
<tr>
|
||||||
|
<td>{log['nick']}</td>
|
||||||
|
<td>{log['time']}</td>
|
||||||
|
<td>{log['dose']}</td>
|
||||||
|
<td>{log['drug']}</td>
|
||||||
|
<td>{log['method']}</td>
|
||||||
|
</tr>
|
||||||
|
"""
|
||||||
|
|
||||||
|
html_response += """
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<script>
|
||||||
|
// Filter table rows based on input
|
||||||
|
document.getElementById('filter').addEventListener('input', function() {
|
||||||
|
const filter = this.value.toLowerCase();
|
||||||
|
const rows = document.querySelectorAll('#doseTable tbody tr');
|
||||||
|
rows.forEach(row => {
|
||||||
|
const text = row.innerText.toLowerCase();
|
||||||
|
row.style.display = text.includes(filter) ? '' : 'none';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Send the HTML response
|
||||||
|
handler.send_response(200)
|
||||||
|
handler.send_header('Content-type', 'text/html')
|
||||||
|
handler.end_headers()
|
||||||
|
handler.wfile.write(html_response.encode('utf-8'))
|
||||||
|
else:
|
||||||
|
# 404 response for unknown paths
|
||||||
|
handler.send_response(404)
|
||||||
|
handler.send_header('Content-type', 'text/html')
|
||||||
|
handler.end_headers()
|
||||||
|
handler.wfile.write(b"<h1>404 Not Found</h1>")
|
||||||
|
|
||||||
class Tripsit(callbacks.Plugin):
|
class Tripsit(callbacks.Plugin):
|
||||||
"""Harm-Reduction tools from tripsit's tripbot and the tripsitwiki"""
|
"""Harm-Reduction tools from tripsit's tripbot and the tripsitwiki"""
|
||||||
@ -74,6 +181,7 @@ class Tripsit(callbacks.Plugin):
|
|||||||
self.db = {}
|
self.db = {}
|
||||||
self._loadDb()
|
self._loadDb()
|
||||||
world.flushers.append(self._flushDb)
|
world.flushers.append(self._flushDb)
|
||||||
|
httpserver.hook('tripsit', TripsitServerCallback(self)) # register the callback at `/tripsit`
|
||||||
|
|
||||||
def _loadDb(self):
|
def _loadDb(self):
|
||||||
"""Loads the (flatfile) database mapping nicks to doses."""
|
"""Loads the (flatfile) database mapping nicks to doses."""
|
||||||
@ -95,6 +203,7 @@ class Tripsit(callbacks.Plugin):
|
|||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
self._flushDb()
|
self._flushDb()
|
||||||
|
httpserver.unhook('tripsit')
|
||||||
world.flushers.remove(self._flushDb)
|
world.flushers.remove(self._flushDb)
|
||||||
self.__parent.die()
|
self.__parent.die()
|
||||||
|
|
||||||
@ -211,7 +320,102 @@ class Tripsit(callbacks.Plugin):
|
|||||||
This command takes no arguments.
|
This command takes no arguments.
|
||||||
|
|
||||||
Retrieves the number of doses logged for a given nick
|
Retrieves the number of doses logged for a given nick
|
||||||
"""
|
""" def doGet(self, handler, path):
|
||||||
|
if path == '/doses':
|
||||||
|
# Collect all dose logs from self.db
|
||||||
|
dose_logs = []
|
||||||
|
for nick, data in self.plugin.db.items():
|
||||||
|
for dose in data.get('doses', []):
|
||||||
|
dose_logs.append({
|
||||||
|
'nick': nick,
|
||||||
|
'time': dose['time'],
|
||||||
|
'dose': dose['dose'],
|
||||||
|
'drug': dose['drug'],
|
||||||
|
'method': dose['method'],
|
||||||
|
})
|
||||||
|
|
||||||
|
# Create HTML response
|
||||||
|
html_response = """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Dose Logs</title>
|
||||||
|
<style>
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
table, th, td {
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
#filter {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 5px;
|
||||||
|
width: 300px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Dose Logs</h1>
|
||||||
|
<input type="text" id="filter" placeholder="Search for nicknames, drugs, etc.">
|
||||||
|
<table id="doseTable">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Nick</th>
|
||||||
|
<th>Time</th>
|
||||||
|
<th>Dose</th>
|
||||||
|
<th>Drug</th>
|
||||||
|
<th>Method</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
"""
|
||||||
|
# Add rows for each dose log
|
||||||
|
for log in dose_logs:
|
||||||
|
html_response += f"""
|
||||||
|
<tr>
|
||||||
|
<td>{log['nick']}</td>
|
||||||
|
<td>{log['time']}</td>
|
||||||
|
<td>{log['dose']}</td>
|
||||||
|
<td>{log['drug']}</td>
|
||||||
|
<td>{log['method']}</td>
|
||||||
|
</tr>
|
||||||
|
"""
|
||||||
|
|
||||||
|
html_response += """
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<script>
|
||||||
|
// Filter table rows based on input
|
||||||
|
document.getElementById('filter').addEventListener('input', function() {
|
||||||
|
const filter = this.value.toLowerCase();
|
||||||
|
const rows = document.querySelectorAll('#doseTable tbody tr');
|
||||||
|
rows.forEach(row => {
|
||||||
|
const text = row.innerText.toLowerCase();
|
||||||
|
row.style.display = text.includes(filter) ? '' : 'none';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Send the HTML response
|
||||||
|
handler.send_response(200)
|
||||||
|
handler.send_header('Content-type', 'text/html')
|
||||||
|
handler.end_headers()
|
||||||
|
handler.wfile.write(html_response.encode('utf-8'))
|
||||||
|
else:
|
||||||
|
# 404 response for unknown paths
|
||||||
|
handler.send_response(404)
|
||||||
|
handler.send_header('Content-type', 'text/html')
|
||||||
|
handler.end_headers()
|
||||||
|
handler.wfile.write(b"<h1>404 Not Found</h1>")
|
||||||
nick = msg.nick
|
nick = msg.nick
|
||||||
if nick in self.db:
|
if nick in self.db:
|
||||||
try:
|
try:
|
||||||
@ -278,16 +482,15 @@ class Tripsit(callbacks.Plugin):
|
|||||||
doses = self.db[nick]['doses']
|
doses = self.db[nick]['doses']
|
||||||
if drug_filter:
|
if drug_filter:
|
||||||
doses = [dose for dose in doses if dose['drug'].lower() == drug_filter.lower()]
|
doses = [dose for dose in doses if dose['drug'].lower() == drug_filter.lower()]
|
||||||
|
|
||||||
if len(doses) == 0:
|
if len(doses) == 0:
|
||||||
irc.error(f"No doses found for drug '{drug_filter}'.")
|
irc.error(f"No doses found for drug '{drug_filter}'.")
|
||||||
return
|
return
|
||||||
if drug_filter:
|
|
||||||
irc.reply(f"Here are your last {history} dose(s) for drug '{drug_filter}':", private=True)
|
|
||||||
else:
|
|
||||||
irc.reply(f"Here are your last {history} dose(s):", private=True)
|
|
||||||
try:
|
try:
|
||||||
for number in range(0, history):
|
irc.reply(f"Your last {history} dose(s) are:", private=True)
|
||||||
lastdose = doses[-(number + 1)]
|
for number in range(history, 0, -1):
|
||||||
|
lastdose = doses[-number]
|
||||||
dose = lastdose['dose']
|
dose = lastdose['dose']
|
||||||
drug = lastdose['drug']
|
drug = lastdose['drug']
|
||||||
method = lastdose['method']
|
method = lastdose['method']
|
||||||
@ -297,13 +500,15 @@ class Tripsit(callbacks.Plugin):
|
|||||||
time = datetime.datetime.now(tz=tz)
|
time = datetime.datetime.now(tz=tz)
|
||||||
since_dose = time - dose_time
|
since_dose = time - dose_time
|
||||||
since_dose_seconds = since_dose.total_seconds()
|
since_dose_seconds = since_dose.total_seconds()
|
||||||
hours, remainder = divmod(since_dose_seconds, 3600)
|
|
||||||
minutes, seconds = divmod(remainder, 60)
|
if number == 1:
|
||||||
timedelta_str = f"{int(hours)} hours, {int(minutes)} minutes" if hours > 0 else f"{int(minutes)} minutes, {int(seconds)} seconds"
|
number = "The"
|
||||||
number_str = "The" if number == 0 else num2words(number + 1, to='ordinal')
|
else:
|
||||||
|
number = num2words(number, to='ordinal')
|
||||||
|
|
||||||
re = utils.str.format(
|
re = utils.str.format(
|
||||||
"✧ %s last dose: \x02Amount:\x0F %s of \x02%s\x0F \x02via\x0F %s | \x02Datetime:\x0F %s %s | \x02Time Since Last Dose:\x0F %s",
|
"::> %s last dose: Amount: %s of \x02%s\x0F via %s | datetime: %s %s | timedelta %T",
|
||||||
number_str, dose, drug, method, dose_time.strftime("%Y-%m-%d %H:%M:%S"), timezone, timedelta_str
|
number, dose, drug, method, dose_time.strftime("%c"), timezone, since_dose_seconds
|
||||||
)
|
)
|
||||||
irc.reply(re, private=True)
|
irc.reply(re, private=True)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
Loading…
Reference in New Issue
Block a user