🏡 index : ~doyle/dave.git

author Jordan Doyle <jordan@doyle.wf> 2017-07-22 23:42:04.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-07-22 23:42:04.0 +00:00:00
commit
5f6ab65e4c681a8d8bc90a67b1cbfebe22d51783 [patch]
tree
e62981dab83f351c0c4846d490282f4c27637ee5
parent
e58b2cb338432d3d5f9e9b2927c6251c84d1028c
download
5f6ab65e4c681a8d8bc90a67b1cbfebe22d51783.tar.gz

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(-)

diff --git a/dave/config.py b/dave/config.py
index b1e7a6c..552863a 100644
--- a/dave/config.py
+++ b/dave/config.py
@@ -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

# read the config file
basepath = os.path.dirname(__file__)
diff --git a/dave/dave.py b/dave/dave.py
index 96279b2..5c1f682 100644
--- a/dave/dave.py
+++ b/dave/dave.py
@@ -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()
diff --git a/dave/module.py b/dave/module.py
index 34518fc..905e54c 100644
--- a/dave/module.py
+++ b/dave/module.py
@@ -2,8 +2,6 @@
"""Provide various decorators for dave modules."""
import re
from enum import Enum
import socket
import config


def match(value):
diff --git a/dave/modules/cryptocurrency.py b/dave/modules/cryptocurrency.py
index b7477b7..44cadc4 100644
--- a/dave/modules/cryptocurrency.py
+++ b/dave/modules/cryptocurrency.py
@@ -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):
    """Contact the preev api and get the latest prices and cache for 10 seconds"""
@@ -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:
diff --git a/dave/modules/help.py b/dave/modules/help.py
index bc131c4..6dbf41b 100644
--- a/dave/modules/help.py
+++ b/dave/modules/help.py
@@ -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"]:
diff --git a/dave/modules/nice.py b/dave/modules/nice.py
index ce4f967..3a40deb 100644
--- a/dave/modules/nice.py
+++ b/dave/modules/nice.py
@@ -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")
diff --git a/dave/modules/pollen.py b/dave/modules/pollen.py
index 6c20de2..12aff4f 100644
--- a/dave/modules/pollen.py
+++ b/dave/modules/pollen.py
@@ -2,9 +2,8 @@
"""Get the pollen count for a UK postcode."""
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:
diff --git a/dave/modules/sed.py b/dave/modules/sed.py
index 6de4092..70b636e 100644
--- a/dave/modules/sed.py
+++ b/dave/modules/sed.py
@@ -2,7 +2,6 @@
"""Pass any messages beginning with 'sed' to GNU sed."""
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:
            # bold replacements
            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


diff --git a/dave/modules/seen.py b/dave/modules/seen.py
index f7002bb..ba50f4b 100644
--- a/dave/modules/seen.py
+++ b/dave/modules/seen.py
@@ -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

diff --git a/dave/modules/title.py b/dave/modules/title.py
index 0b5ac54..904ba90 100644
--- a/dave/modules/title.py
+++ b/dave/modules/title.py
@@ -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]))
diff --git a/dave/modules/urbandictionary.py b/dave/modules/urbandictionary.py
index 362999a..9666aa1 100644
--- a/dave/modules/urbandictionary.py
+++ b/dave/modules/urbandictionary.py
@@ -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)):
diff --git a/dave/modules/weather.py b/dave/modules/weather.py
index 52868d8..7b0bb2e 100644
--- a/dave/modules/weather.py
+++ b/dave/modules/weather.py
@@ -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
diff --git a/dave/modules/wolfram.py b/dave/modules/wolfram.py
index b219820..c9a0070 100644
--- a/dave/modules/wolfram.py
+++ b/dave/modules/wolfram.py
@@ -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)
diff --git a/requirements.txt b/requirements.txt
index 98e962c..45637eb 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -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