2020-09-01 18:04:31 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2020, mogad0n
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
###
|
|
|
|
|
2020-09-05 10:32:51 +02:00
|
|
|
from supybot import utils, plugins, ircutils, callbacks, world, conf, log
|
2020-09-01 18:04:31 +02:00
|
|
|
from supybot.commands import *
|
2020-09-05 05:51:17 +02:00
|
|
|
import dateutil.parser
|
2020-09-01 18:04:31 +02:00
|
|
|
import json
|
|
|
|
import requests
|
2020-09-05 10:32:51 +02:00
|
|
|
import pickle
|
|
|
|
import sys
|
2020-09-17 09:45:49 +02:00
|
|
|
import datetime
|
2020-09-18 07:17:25 +02:00
|
|
|
import pytz
|
2020-09-18 09:16:42 +02:00
|
|
|
import time
|
2020-09-18 07:17:25 +02:00
|
|
|
|
2020-09-01 18:04:31 +02:00
|
|
|
try:
|
|
|
|
from supybot.i18n import PluginInternationalization
|
|
|
|
_ = PluginInternationalization('Tripsit')
|
|
|
|
except ImportError:
|
|
|
|
# Placeholder that allows to run the plugin on a bot
|
|
|
|
# without the i18n module
|
|
|
|
_ = lambda x: x
|
|
|
|
|
2020-09-05 10:32:51 +02:00
|
|
|
filename = conf.supybot.directories.data.dirize("Tripsit.db")
|
2020-09-01 18:04:31 +02:00
|
|
|
|
2020-09-05 10:32:51 +02:00
|
|
|
|
|
|
|
url_drug = "http://tripbot.tripsit.me/api/tripsit/getDrug"
|
|
|
|
url_combo = "http://tripbot.tripsit.me/api/tripsit/getInteraction"
|
|
|
|
|
|
|
|
insufflated = ["Insufflated", "Insufflated-IR", "Insufflated-XR"]
|
2020-09-01 18:04:31 +02:00
|
|
|
|
|
|
|
METHODS = {
|
|
|
|
"iv": ["IV"],
|
|
|
|
"shot": ["IV"],
|
|
|
|
|
|
|
|
"im": ["IM"],
|
|
|
|
|
|
|
|
"oral": ["Oral", "Oral-IR", "Oral-XR"],
|
|
|
|
|
2020-09-05 10:32:51 +02:00
|
|
|
"insufflated": insufflated,
|
|
|
|
"snorted": insufflated,
|
2020-09-01 18:04:31 +02:00
|
|
|
|
|
|
|
"smoked": ["Smoked"]
|
|
|
|
}
|
|
|
|
|
|
|
|
class Tripsit(callbacks.Plugin):
|
|
|
|
"""Harm-Reduction tools from tripsit's tripbot and the tripsitwiki"""
|
|
|
|
threaded = True
|
2020-09-05 10:32:51 +02:00
|
|
|
|
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(Tripsit, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
self.db = {}
|
|
|
|
self._loadDb()
|
|
|
|
world.flushers.append(self._flushDb)
|
|
|
|
|
|
|
|
def _loadDb(self):
|
|
|
|
"""Loads the (flatfile) database mapping nicks to doses."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
self.db = pickle.load(f)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.debug("Tripsit: Unable to load pickled database: %s", e)
|
|
|
|
|
|
|
|
def _flushDb(self):
|
|
|
|
"""Flushes the (flatfile) database mapping nicks to doses."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(filename, "wb") as f:
|
|
|
|
pickle.dump(self.db, f, 2)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.warning("Tripsit: Unable to write pickled database: %s", e)
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self._flushDb()
|
|
|
|
world.flushers.remove(self._flushDb)
|
|
|
|
self.__parent.die()
|
|
|
|
|
2020-09-01 18:04:31 +02:00
|
|
|
@wrap(['something', optional('something')])
|
|
|
|
def drug(self, irc, msg, args, name, category):
|
|
|
|
"""<drug> [<category>]
|
2020-09-17 07:55:24 +02:00
|
|
|
|
2020-09-01 18:04:31 +02:00
|
|
|
fetches data on drug from tripsit wiki
|
|
|
|
"""
|
|
|
|
category_list = []
|
2020-09-05 10:32:51 +02:00
|
|
|
r = requests.get(url_drug, params={"name": name}).json()
|
2020-09-01 18:04:31 +02:00
|
|
|
if not r['err']:
|
|
|
|
drug = r["data"][0]["pretty_name"]
|
|
|
|
properties = r["data"][0]["properties"]
|
2020-09-05 10:32:51 +02:00
|
|
|
for key in properties:
|
|
|
|
category_list.append(key)
|
2020-09-01 18:04:31 +02:00
|
|
|
if category == None:
|
2020-09-05 10:32:51 +02:00
|
|
|
re = drug + " Available categories are: " + ", ".join(category_list)
|
2020-09-01 18:04:31 +02:00
|
|
|
irc.reply(re)
|
|
|
|
else:
|
|
|
|
if category in properties.keys():
|
2020-09-05 10:32:51 +02:00
|
|
|
re = drug + " " + properties[category]
|
2020-09-01 18:04:31 +02:00
|
|
|
irc.reply(re)
|
|
|
|
else:
|
2020-09-05 10:32:51 +02:00
|
|
|
irc.error(f"Unknown category {drug} Available categories are: " + ", ".join(category_list))
|
2020-09-01 18:04:31 +02:00
|
|
|
else:
|
|
|
|
irc.error("unknown drug")
|
|
|
|
|
|
|
|
def combo(self, irc, msg, args, drugA, drugB):
|
|
|
|
"""<drugA> <drugB>
|
2020-09-17 07:55:24 +02:00
|
|
|
|
2020-09-05 10:32:51 +02:00
|
|
|
fetches known interactions between the substances provided.
|
2020-09-01 18:04:31 +02:00
|
|
|
"""
|
2020-09-05 10:32:51 +02:00
|
|
|
r = requests.get(url_combo, params={f"drugA": drugA, f"drugB": drugB}).json()
|
2020-09-01 18:04:31 +02:00
|
|
|
if not r["err"] and r["data"][0]:
|
|
|
|
interaction = r["data"][0]
|
|
|
|
drug_a = interaction["interactionCategoryA"]
|
|
|
|
drug_b = interaction["interactionCategoryB"]
|
|
|
|
interaction_status = interaction["status"]
|
2020-09-05 10:32:51 +02:00
|
|
|
re = f"{drug_a} and {drug_b}: {interaction_status}"
|
|
|
|
if 'note' in interaction:
|
|
|
|
note = interaction["note"]
|
|
|
|
re += f'. Note: {note}'
|
|
|
|
irc.reply(re)
|
|
|
|
else:
|
|
|
|
irc.reply(re)
|
2020-09-01 18:04:31 +02:00
|
|
|
else:
|
2020-09-05 10:32:51 +02:00
|
|
|
irc.reply("Unknown combo (that doesn't mean it's safe). Known combos: lsd, mushrooms, dmt, mescaline, dox, nbomes, 2c-x, 2c-t-x, amt, 5-meo-xxt, cannabis, ketamine, mxe, dxm, pcp, nitrous, amphetamines, mdma, cocaine, caffeine, alcohol, ghb/gbl, opioids, tramadol, benzodiazepines, maois, ssris.")
|
|
|
|
|
2020-09-01 18:04:31 +02:00
|
|
|
combo = wrap(combo, [("something"), ("something")])
|
|
|
|
|
2020-09-18 07:17:25 +02:00
|
|
|
def set(self, irc, msg, args, timezone):
|
|
|
|
"""<timezone>
|
|
|
|
|
|
|
|
Sets location for your current ident@host to <timezone>
|
|
|
|
for eg. America/Chicago
|
|
|
|
"""
|
|
|
|
|
|
|
|
ih = msg.prefix.split("!")[1]
|
|
|
|
self.db[ih] = timezone
|
|
|
|
irc.replySuccess()
|
|
|
|
set = wrap(set, ["text"])
|
|
|
|
|
2020-09-17 19:07:22 +02:00
|
|
|
@wrap(["something", "something", optional("something"), optional("something")])
|
2020-09-17 13:35:44 +02:00
|
|
|
def idose(self, irc, msg, args, dose, name, method, ago):
|
|
|
|
"""<amount> <drug> [<method>] [<ago>]
|
2020-09-17 07:55:24 +02:00
|
|
|
|
|
|
|
<ago> is in the format HHMM
|
|
|
|
logs a dose for you, use 'lastdose' command to retrieve
|
2020-09-05 05:51:17 +02:00
|
|
|
"""
|
2020-09-05 10:32:51 +02:00
|
|
|
r = requests.get(url_drug, params={"name": name}).json()
|
2020-09-05 05:51:17 +02:00
|
|
|
found_method = False
|
|
|
|
onset = None
|
|
|
|
if not r['err']:
|
|
|
|
drug = r['data'][0]
|
2020-09-17 19:07:22 +02:00
|
|
|
drug_name = drug['pretty_name']
|
2020-09-05 05:51:17 +02:00
|
|
|
method_keys = ['value']
|
|
|
|
methods = []
|
|
|
|
if method:
|
|
|
|
methods = [method.lower()]
|
|
|
|
methods = METHODS.get(methods[0], methods)
|
|
|
|
method_keys += methods
|
|
|
|
|
|
|
|
if 'formatted_onset' in drug:
|
|
|
|
match = list(set(method_keys)&
|
|
|
|
set(drug["formatted_onset"].keys()))
|
|
|
|
if match:
|
|
|
|
onset = drug["formatted_onset"][match[0]]
|
|
|
|
found_method = True
|
|
|
|
if match[0] in methods:
|
|
|
|
method = (match or [method])[0]
|
|
|
|
|
|
|
|
if onset and "_unit" in drug["formatted_onset"]:
|
|
|
|
onset = "%s %s" % (
|
|
|
|
onset, drug["formatted_onset"]["_unit"])
|
2020-09-17 19:18:05 +02:00
|
|
|
drug_and_method = name
|
2020-09-05 05:51:17 +02:00
|
|
|
if method:
|
|
|
|
if not found_method:
|
|
|
|
method = method.title()
|
|
|
|
drug_and_method = "%s via %s" % (drug_and_method, method)
|
2020-09-17 19:13:55 +02:00
|
|
|
else:
|
2020-09-18 07:17:25 +02:00
|
|
|
method = 'Undefined'
|
2020-09-17 19:07:22 +02:00
|
|
|
|
2020-09-18 07:17:25 +02:00
|
|
|
ih = msg.prefix.split("!")[1]
|
|
|
|
if self.db[ih] is not None:
|
|
|
|
timezone = self.db[ih]
|
2020-09-18 07:28:06 +02:00
|
|
|
timez = pytz.timezone(timezone)
|
|
|
|
time = datetime.datetime.now(tz=timez)
|
2020-09-17 07:55:24 +02:00
|
|
|
else:
|
2020-09-18 07:17:25 +02:00
|
|
|
timezone = 'UTC'
|
|
|
|
time = datetime.datetime.utcnow()
|
2020-09-18 09:16:42 +02:00
|
|
|
dose_td = 0
|
|
|
|
if ago is not None and len(ago) == 4:
|
2020-09-17 19:18:05 +02:00
|
|
|
hours = int(ago[0:2])
|
|
|
|
minutes = int(ago[2:4])
|
2020-09-17 07:55:24 +02:00
|
|
|
dose_td = datetime.timedelta(hours=hours, minutes=minutes)
|
2020-09-18 07:17:25 +02:00
|
|
|
dose_td_s = dose_td.total_seconds()
|
|
|
|
time = time - dose_td
|
|
|
|
|
|
|
|
|
|
|
|
log_dict = {'time': time, 'timezone': timezone, 'dose': dose, 'drug': name, 'method': method }
|
|
|
|
|
|
|
|
nickdb = self.db[msg.nick]
|
|
|
|
if nickdb is not None and len(nickdb) < 10:
|
|
|
|
# if dose db exists and there are less that 10
|
|
|
|
nickdb.append(log_dict)
|
|
|
|
elif len(nickdb) == 10:
|
|
|
|
# how can this be greater than it has to be equal to 10
|
|
|
|
nickdb.pop(0)
|
|
|
|
elif nickdb is None:
|
|
|
|
# dose db doesnt exist. Initialise list
|
|
|
|
nickdb = [log_dict]
|
|
|
|
|
2020-09-18 09:16:42 +02:00
|
|
|
if dose_td == 0:
|
|
|
|
re = utils.str.format("You dosed %s of %s at %t, %s", dose, drug_and_method, time.timetuple(), timezone)
|
2020-09-18 07:17:25 +02:00
|
|
|
if onset is not None:
|
|
|
|
re += utils.str.format(". You should start feeling effects %s from now", onset)
|
|
|
|
else:
|
2020-09-18 09:16:42 +02:00
|
|
|
re = utils.str.format("You dosed %s of %s at %t, %s %T", dose, drug_and_method, time.timetuple(), timezone, dose_td.total_seconds())
|
2020-09-18 07:17:25 +02:00
|
|
|
if onset is not None:
|
|
|
|
re += utils.str.format(". You should have/will start feeling effects %s from/after dosing", onset)
|
2020-09-05 05:51:17 +02:00
|
|
|
irc.reply(re)
|
2020-09-17 07:55:24 +02:00
|
|
|
|
2020-09-18 07:17:25 +02:00
|
|
|
@wrap([optional('positiveInt')])
|
|
|
|
def lastdose(self, irc, msg, args, history):
|
|
|
|
"""<history> are digits upto but not including 10
|
2020-09-17 07:55:24 +02:00
|
|
|
|
|
|
|
retrieves your last logged dose
|
2020-09-05 05:51:17 +02:00
|
|
|
"""
|
2020-09-05 10:32:51 +02:00
|
|
|
if msg.nick in self.db:
|
2020-09-18 07:17:25 +02:00
|
|
|
if history:
|
2020-09-18 09:16:42 +02:00
|
|
|
lastdose = self.db[msg.nick][int(history)]
|
2020-09-18 07:17:25 +02:00
|
|
|
else:
|
|
|
|
history = 0
|
2020-09-18 09:16:42 +02:00
|
|
|
lastdose = self.db[msg.nick][0]
|
2020-09-18 07:17:25 +02:00
|
|
|
dose = lastdose['dose']
|
|
|
|
drug = lastdose['drug']
|
|
|
|
timezone = lastdose['timezone']
|
|
|
|
if timezone == 'UTC':
|
|
|
|
time = datetime.datetime.utcnow()
|
|
|
|
else:
|
2020-09-18 07:28:06 +02:00
|
|
|
timez = pytz.timezone(timezone)
|
|
|
|
time = datetime.datetime.now(tz=timez)
|
2020-09-18 09:16:42 +02:00
|
|
|
dose_time = lastdose['time']
|
2020-09-05 10:32:51 +02:00
|
|
|
since_dose = time - dose_time
|
|
|
|
since_dose_seconds = since_dose.total_seconds()
|
2020-09-18 09:16:42 +02:00
|
|
|
re = utils.str.format("Your #%i last dose was %s of %s at %t %s, %T ago", history, dose, drug, dose_time.timetuple(), timezone, since_dose_seconds)
|
2020-09-05 05:51:17 +02:00
|
|
|
irc.reply(re)
|
|
|
|
else:
|
2020-09-05 10:32:51 +02:00
|
|
|
irc.error(f'No last dose saved for {msg.nick}')
|
2020-09-05 05:51:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-01 18:04:31 +02:00
|
|
|
Class = Tripsit
|
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|