Port to python3
Diff
dave/config.py | 4 ----
dave/dave.py | 14 ++++++--------
dave/module.py | 2 --
dave/modules/cryptocurrency.py | 15 +++++++++------
dave/modules/help.py | 4 ++--
dave/modules/nice.py | 2 +-
dave/modules/pollen.py | 10 +++-------
dave/modules/sed.py | 14 +++++++-------
dave/modules/seen.py | 1 -
dave/modules/title.py | 4 ++--
dave/modules/urbandictionary.py | 4 ++--
dave/modules/weather.py | 2 +-
dave/modules/wolfram.py | 17 +++++++++--------
requirements.txt | 21 ++++++++++-----------
14 files changed, 52 insertions(+), 62 deletions(-)
@@ -3,10 +3,6 @@ import json
import os.path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import socket
import markovify
import nltk
import re
basepath = os.path.dirname(__file__)
@@ -5,16 +5,15 @@ from twisted.words.protocols import irc
from twisted.python import log
import time
import pkgutil
import modules
import dave.modules as modules
import re
import config
import datetime
import dave.config as config
from twisted.internet import reactor
from twisted.internet.threads import deferToThread
class Dave(irc.IRCClient):
nickname = bytes(config.config["irc"]["nick"])
nickname = config.config["irc"]["nick"]
def connectionMade(self):
irc.IRCClient.connectionMade(self)
@@ -31,7 +30,7 @@ class Dave(irc.IRCClient):
self.mode(self.nickname, True, "B")
for channel in config.config["irc"]["channels"]:
self.join(bytes(channel))
self.join(channel)
if config.config["irc"]["nickserv_password"]:
self.msg("nickserv", "identify {}".format(config.config["irc"]["nickserv_password"]))
@@ -53,7 +52,7 @@ class Dave(irc.IRCClient):
for importer, modname, ispkg in pkgutil.iter_modules(path, prefix):
m = importer.find_module(modname).load_module(modname)
for name, val in m.__dict__.iteritems():
for name, val in m.__dict__.items():
if callable(val) and hasattr(val, "rule"):
priority = val.priority.value if hasattr(val, "priority") else 0
@@ -88,7 +87,7 @@ class Dave(irc.IRCClient):
self.join(params[1])
def reply(self, source, sender, msg):
self.msg(source, "{}: {}".format(sender, msg.encode("utf-8")))
self.msg(source, "{}: {}".format(sender, msg))
def main():
@@ -98,5 +97,4 @@ def main():
factory.protocol = Dave
reactor.connectSSL(config.config["irc"]["host"], config.config["irc"]["port"],
factory, ssl.ClientContextFactory())
reactor.suggestThreadPoolSize(30)
reactor.run()
@@ -2,8 +2,6 @@
import re
from enum import Enum
import socket
import config
def match(value):
@@ -49,12 +49,12 @@ def crypto(bot, args, sender, source):
multiplier = decimal.Decimal(1)
total_volume = sum(
float(market['volume']) for market in p[cryptoKey][currencyKey].itervalues()
float(market['volume']) for market in p[cryptoKey][currencyKey].values()
)
avg = sum(
float(market['last']) * float(market['volume']) / total_volume
for market in p[cryptoKey][currencyKey].itervalues()
for market in p[cryptoKey][currencyKey].values()
)
prices = assembleFormattedText(A.normal[
@@ -65,16 +65,16 @@ def crypto(bot, args, sender, source):
market,
babel.numbers.format_currency(decimal.Decimal(data['last']) * multiplier,
currency)
).encode('utf-8') for market, data in p[cryptoKey][currencyKey].iteritems()
) for market, data in p[cryptoKey][currencyKey].items()
])
])
prices += u'. average: {}'.format(
babel.numbers.format_currency(decimal.Decimal(avg) * multiplier,
currency)
).encode('utf-8')
)
bot.reply(source, sender, prices.decode('utf-8'))
bot.reply(source, sender, prices)
def preev(cryptocurrency, currency):
@@ -87,7 +87,10 @@ def preev(cryptocurrency, currency):
currency
))
json = r.json()
try:
json = r.json()
except:
return r.text
dave.config.redis.setex(key, 10, pickle.dumps(json))
else:
@@ -16,7 +16,7 @@ def list_modules(bot, args, sender, source):
for importer, modname, ispkg in pkgutil.iter_modules(path, prefix):
m = importer.find_module(modname).load_module(modname)
for name, val in m.__dict__.iteritems():
for name, val in m.__dict__.items():
if callable(val) and hasattr(val, "help"):
reply.append(val.help["name"])
@@ -34,7 +34,7 @@ def help(bot, args, sender, source):
for importer, modname, ispkg in pkgutil.iter_modules(path, prefix):
m = importer.find_module(modname).load_module(modname)
for name, val in m.__dict__.iteritems():
for name, val in m.__dict__.items():
if callable(val) and hasattr(val, "rule") and hasattr(val, "help"):
for rule in val.rule:
if "commands" in rule and args[0] in rule["commands"]:
@@ -6,5 +6,5 @@ import dave.module
@dave.module.help("Nice.", name="nice")
@dave.module.match(r"^nice\.?$")
@dave.module.priority(dave.module.Priority.LOW)
def donk(bot, args, sender, source):
def nice(bot, args, sender, source):
bot.msg(source, "nice")
@@ -2,9 +2,8 @@
import dave.module
from bs4 import BeautifulSoup
from mechanize import Browser
from requests import get
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
import socket
import dave.config
@@ -13,16 +12,13 @@ import dave.config
@dave.module.priority(dave.module.Priority.HIGHEST)
def pollen(bot, args, sender, source):
postcode = args[0].lower()
br = Browser()
br.set_handle_robots(False)
text = None
if not dave.config.redis.exists("pollen:{}".format(postcode)):
res = br.open("https://www.bbc.co.uk/weather/{}".format(postcode))
data = res.get_data()
res = get("https://www.bbc.co.uk/weather/{}".format(postcode))
soup = BeautifulSoup(data, "html.parser")
soup = BeautifulSoup(res.text, "html.parser")
element = soup.find_all("div", class_="environmental-index pollen-index")
if element:
@@ -2,7 +2,6 @@
import dave.module
import dave.config
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
import re
@dave.module.help("Syntax: s/find/replace/flags", "sed")
@@ -20,17 +19,18 @@ def sed(bot, args, sender, source):
try:
replace = re.sub(args[3], "\x02{}\x0F".format(args[5]),
msg, count=0 if 'g' in flags else 1, flags=f)
toDisplay = re.sub(args[3], "\x02{}\x0F".format(args[5]),
msg.decode('utf-8'), count=0 if 'g' in flags else 1, flags=f)
toSave = re.sub(args[3], args[5],
msg.decode('utf-8'), count=0 if 'g' in flags else 1, flags=f)
except Exception as e:
bot.reply(source, sender,
"There was a problem with your sed command: {}".format(str(e)))
return
if replace.strip() != msg:
msg = replace.strip()
bot.msg(source, "<{}> {}".format(sender, msg))
dave.config.redis.lset(key, i, msg)
if toDisplay.strip() != msg:
bot.msg(source, "<{}> {}".format(sender, toDisplay.strip()))
dave.config.redis.lset(key, i, toSave.strip())
return
@@ -2,7 +2,6 @@ import pickle
import dave.module
import dave.config
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
from humanize import naturaltime
from datetime import datetime
@@ -26,11 +26,11 @@ def link_parse(bot, args, sender, source):
if title is not None:
title = re.sub(r"(\r?\n|\r| )+",
" ",
title.string.encode("utf-8").strip())
title.string.strip())
title = title[:140] + (title[140:] and '...')
dave.config.redis.setex("site:{}".format(match), 300, title)
else:
title = dave.config.redis.get("site:{}".format(match))
title = dave.config.redis.get("site:{}".format(match)).decode('utf-8')
if title is not None:
titles.append(assembleFormattedText(A.bold[title]))
@@ -5,7 +5,7 @@ import dave.config
import requests
import pickle
import re
from urllib import quote_plus
from urllib.parse import quote_plus
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
@@ -19,7 +19,7 @@ def urbandictionary(bot, args, sender, source):
key = "urban:{}:{}".format(query, result)
if dave.config.redis.exists(key):
bot.reply(source, sender, dave.config.redis.get(key))
bot.reply(source, sender, dave.config.redis.get(key).decode('utf-8'))
return
if not dave.config.redis.exists("urban_query:{}".format(query)):
@@ -5,7 +5,7 @@ import dave.modules
import requests
import dave.config
import pickle
from urllib import quote_plus
from urllib.parse import quote_plus
import arrow
from dave.models import Location
import socket
@@ -3,7 +3,6 @@
import dave.module
import dave.config
import wolframalpha
import socket
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
@@ -16,7 +15,7 @@ def wolfram(bot, args, sender, source):
key = "wolfram:{}".format(query.lower())
if dave.config.redis.exists(key):
bot.reply(source, sender, dave.config.redis.get(key))
bot.reply(source, sender, dave.config.redis.get(key).decode('utf-8'))
dave.config.redis.setex(key, 60, dave.config.redis.get(key))
return
@@ -24,17 +23,19 @@ def wolfram(bot, args, sender, source):
client = wolframalpha.Client(dave.config.config["api_keys"]["wolfram"])
res = client.query(query)
if len(res.pods) > 0:
pods = list(res.pods)
if len(pods) > 0:
resultpod = next(res.results)
result = resultpod.text.encode('utf-8')
result = resultpod.text
if "image" in res.pods[0].text:
if "image" in pods[0].text:
result = resultpod.img
if len(result) > 500:
result = result[:497] + "..."
res = assembleFormattedText(A.normal[A.bold[str(res.pods[0].text.encode('utf-8'))],
res = assembleFormattedText(A.normal[A.bold[pods[0].text],
": {}".format(result)])
dave.config.redis.setex(key, 60, res.decode('utf-8'))
bot.reply(source, sender, res.decode('utf-8'))
dave.config.redis.setex(key, 60, res)
bot.reply(source, sender, res)
@@ -1,18 +1,17 @@
twisted==17.5.0
pyopenssl==16.2.0
service_identity==16.0.0
service_identity==17.0.0
enum34==1.1.6
beautifulsoup4==4.4.1
mechanize==0.2.5
beautifulsoup4==4.6.0
requests==2.18.1
hiredis==0.2.0
redis==2.10.5
arrow==0.7.0
sqlalchemy==1.0.13
alembic==0.8.6
psycopg2==2.6.1
lxml==3.6.0
pysocks==1.5.7
wolframalpha==2.4
babel==2.3.4
arrow==0.10.0
sqlalchemy==1.1.11
alembic==0.9.3
psycopg2==2.7.2
lxml==3.8.0
pysocks==1.6.7
wolframalpha==3.0
babel==2.4.0
humanize==0.5.1
\ No newline at end of file