Remove markov & socks proxying
Diff
alembic.ini | 3 +--
requirements.txt | 4 ++--
dave/config.py | 33 +--------------------------------
dave/dave.py | 16 ----------------
dave/models.py | 10 ----------
dave/module.py | 2 --
dave/modules/btc.py | 3 +--
dave/modules/markov.py | 38 --------------------------------------
dave/modules/nice.py | 2 +-
dave/modules/pollen.py | 2 --
dave/modules/sed.py | 10 ++++++----
dave/modules/title.py | 2 --
dave/modules/urbandictionary.py | 2 --
dave/modules/weather.py | 6 ------
dave/modules/wolfram.py | 2 --
migrations/versions/7836665787cd_add_messages_table.py | 38 --------------------------------------
16 files changed, 12 insertions(+), 161 deletions(-)
@@ -29,8 +29,7 @@
sqlalchemy.url = postgresql://homestead:secret@192.168.10.10/dave
sqlalchemy.url = postgresql://dave:dave@127.0.0.1/dave
[loggers]
@@ -1,5 +1,5 @@
twisted==16.2.0
pyopenssl==16.0.0
twisted==17.5.0
pyopenssl==16.2.0
service_identity==16.0.0
enum34==1.1.6
beautifulsoup4==4.4.1
@@ -1,13 +1,11 @@
import redis
import json
import os.path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import socket
import socks
import markovify
import nltk
from models import Message
import re
@@ -20,36 +18,7 @@
redis = redis.StrictRedis(host=config["redis"]["host"], port=config["redis"]["port"],
db=config["redis"]["db"])
proxy = config['socks'].split(":")
default_socket = socket.socket
if proxy[0]:
socks.set_default_proxy(socks.SOCKS5, proxy[0], int(proxy[1]))
proxied_socket = socks.socksocket
else:
proxied_socket = socket.socket
db = create_engine(config["database"])
Session = sessionmaker(bind=db)
session = Session()
class PText(markovify.NewlineText):
def word_split(self, sentence):
words = re.split(self.word_split_pattern, sentence)
words = [ "::".join(tag) for tag in nltk.pos_tag(words) ]
return words
def word_join(self, words):
sentence = " ".join(word.split("::")[0] for word in words)
return sentence
if not redis.exists("markov:chain"):
markov = PText("\n".join([m[0] for m in session.query(Message.message).all()]), state_size=2)
redis.setex("markov:chain", 500, markov.chain.to_json())
else:
markov = PText("\n".join([m[0] for m in session.query(Message.message).all()]),
state_size=2, chain=markovify.Chain.from_json(redis.get("markov:chain")))
session = Session()
@@ -9,7 +9,6 @@
import re
import config
import datetime
from models import Message
from twisted.internet.threads import deferToThread
@@ -73,21 +72,6 @@
run.append((val, match.groups()))
else:
method = (priority, val, match.groups())
if not msg.startswith(self.nickname) and channel.startswith("#"):
try:
db_msg = Message(
nick=nick,
channel=channel,
message=msg.decode('utf-8', 'ignore').encode('utf-8'),
userhost=user.split("!", 1)[1],
created_at=datetime.datetime.now()
)
config.session.add(db_msg)
config.session.commit()
except:
log.err()
if method[1] is not None:
@@ -9,13 +9,3 @@
id = Column(Integer, primary_key=True)
nick = Column(String)
location = Column(String)
class Message(Base):
__tablename__ = "messages"
id = Column(Integer, primary_key=True)
nick = Column(String)
channel = Column(String)
message = Column(String)
userhost = Column(String)
created_at = Column(DateTime)
@@ -88,9 +88,7 @@
"""Decorate a function to always proxy data flowing from it."""
def proxifier(function):
def wrapper(*args, **kw):
socket.socket = config.proxied_socket
output = function(*args, **kw)
socket.socket = config.default_socket
return output
return wrapper
@@ -48,8 +48,7 @@
market,
babel.numbers.format_currency(decimal.Decimal(data['last']) * multiplier,
currency)
).encode('utf-8')
for market, data in p['btc'][currencyKey].iteritems()
).encode('utf-8') for market, data in p['btc'][currencyKey].iteritems()
])
prices += u'. average: {}'.format(
@@ -1,38 +1,0 @@
"""Chatter bot using Markov chains."""
import dave.module
import dave.config
import random
import re
from dave.models import Message
FACTOR = 0.05
@dave.module.help("Syntax: babble [optional seeds].")
@dave.module.command(["babble"], "?(.*)")
@dave.module.priority(dave.module.Priority.HIGHEST)
def babble(bot, args, sender, source):
msg = args[0].strip()
if msg:
args = msg.split(" ")
if len(args) == 2:
resp = dave.config.markov.make_sentence(tuple(dave.config.markov.word_split(" ".join(args))), tries=10000)
else:
resp = None
else:
resp = dave.config.markov.make_sentence(tries=10000)
if not resp is None:
bot.msg(source, resp.encode("utf-8"))
@@ -1,9 +1,9 @@
"""Nice."""
import dave.module
@dave.module.help("Nice.", name="donk")
@dave.module.help("Nice.", name="nice")
@dave.module.match(r"^nice\.?$")
@dave.module.priority(dave.module.Priority.LOW)
def donk(bot, args, sender, source):
@@ -19,10 +19,8 @@
text = None
if not dave.config.redis.exists("pollen:{}".format(postcode)):
socket.socket = dave.config.proxied_socket
res = br.open("https://www.bbc.co.uk/weather/{}".format(postcode))
data = res.get_data()
socket.socket = dave.config.default_socket
soup = BeautifulSoup(data, "html.parser")
element = soup.find_all("div", class_="environmental-index pollen-index")
@@ -1,8 +1,8 @@
"""Pass any messages beginning with 'sed' to GNU sed."""
import subprocess
import dave.module
import dave.config
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
import re
@@ -20,9 +20,11 @@
f = f | re.IGNORECASE
try:
replace = re.sub(args[3], args[5], msg, count=0 if 'g' in flags else 1,
replace = re.sub(args[3],
assembleFormattedText(A.bold[args[5]]),
msg,
count=0 if 'g' in flags else 1,
flags=f)
print(replace)
except Exception as e:
bot.reply(source, sender,
"There was a problem with your sed command: {}".format(str(e)))
@@ -30,7 +32,7 @@
if replace.strip() != msg:
msg = replace.strip()
bot.msg(source, "{} meant: {}".format(sender, msg))
bot.msg(source, "<{}> {}".format(sender, msg))
dave.config.redis.lset(key, i, msg)
return
@@ -23,10 +23,8 @@
for match in matches:
if not dave.config.redis.exists("site:{}".format(match)):
socket.socket = dave.config.proxied_socket
res = br.open(match)
data = res.get_data()
socket.socket = dave.config.default_socket
soup = BeautifulSoup(data, "html.parser")
title = soup.title
@@ -24,7 +24,6 @@
return
if not dave.config.redis.exists("urban_query:{}".format(query)):
socket.socket = dave.config.proxied_socket
url = "https://mashape-community-urban-dictionary.p.mashape.com/define?term={}".format(quote_plus(query))
r = requests.get(url, headers={
"X-Mashape-Key": dave.config.config["api_keys"]["mashape"],
@@ -32,7 +31,6 @@
})
resp = r.json()
socket.socket = dave.config.default_socket
dave.config.redis.setex("urban_query:{}".format(query), 86400, pickle.dumps(resp))
else:
resp = pickle.loads(dave.config.redis.get("urban_query:{}".format(query)))
@@ -65,14 +65,12 @@
key = "location:{}".format(location.lower())
if not dave.config.redis.exists(key):
socket.socket = dave.config.proxied_socket
r = requests.get("https://maps.googleapis.com/maps/api/geocode/json?address={}&key={}".format(
quote_plus(location),
dave.config.config["api_keys"]["google_maps"]
))
geocode = r.json()
socket.socket = dave.config.default_socket
dave.config.redis.set(key, pickle.dumps(geocode))
else:
@@ -112,7 +110,6 @@
geocode["results"][0]["geometry"]["location"]["lng"])
if not dave.config.redis.exists(key):
socket.socket = dave.config.proxied_socket
r = requests.get("http://api.timezonedb.com/?key={}&lat={}&lng={}&format=json".format(
dave.config.config["api_keys"]["timezonedb"],
geocode["results"][0]["geometry"]["location"]["lat"],
@@ -120,7 +117,6 @@
))
timezone = r.json()
socket.socket = dave.config.default_socket
dave.config.redis.set(key, pickle.dumps(timezone))
else:
@@ -135,7 +131,6 @@
geocode["results"][0]["geometry"]["location"]["lng"])
if not dave.config.redis.exists(key):
socket.socket = dave.config.proxied_socket
r = requests.get("https://api.forecast.io/forecast/{}/{},{}?units=uk2".format(
dave.config.config["api_keys"]["forecast.io"],
geocode["results"][0]["geometry"]["location"]["lat"],
@@ -143,7 +138,6 @@
))
json = r.json()
socket.socket = dave.config.default_socket
dave.config.redis.setex(key, 300, pickle.dumps(json))
else:
@@ -21,10 +21,8 @@
return
if query:
socket.socket = dave.config.proxied_socket
client = wolframalpha.Client(dave.config.config["api_keys"]["wolfram"])
res = client.query(query)
socket.socket = dave.config.default_socket
if len(res.pods) > 0:
resultpod = next(res.results)
@@ -1,38 +1,0 @@
"""Add messages table
Revision ID: 7836665787cd
Revises: 0689a529bce0
Create Date: 2016-11-12 08:20:05.648069
"""
revision = '7836665787cd'
down_revision = '0689a529bce0'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
op.create_table('messages',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nick', sa.String(), nullable=True),
sa.Column('channel', sa.String(), nullable=True),
sa.Column('message', sa.String(), nullable=True),
sa.Column('userhost', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.drop_constraint(u'locations_nick_key', 'locations', type_='unique')
def downgrade():
op.create_unique_constraint(u'locations_nick_key', 'locations', ['nick'])
op.drop_table('messages')