add everything thus far, reddit scaping, storing to db, basic retrieval

main
lza_menace 4 years ago
parent 3679137e6c
commit d4dec84576

@ -1,5 +1,4 @@
praw
peewee
pure-sasl
pydle
timeloop
arrow

@ -1,13 +1,46 @@
import sys, os
import pydle
import asyncio
import arrow
from supportbot.reddit import Reddit
from supportbot.db import SupportRequest, IRCSupportOperator
from supportbot import config
from supportbot import db
class IRCBot(pydle.Client):
async def on_connect(self):
for room in config.ROOMS:
await self.join(room)
while True:
print("Checking reddit for new posts")
r = Reddit()
new_requests = []
subreddit = r.reddit.subreddit(r.subreddit)
for submission in subreddit.new():
if not SupportRequest.select().where(SupportRequest.post_id == submission.id):
s = SupportRequest(
post_id=submission.id,
author=submission.author,
title=submission.title,
permalink=submission.permalink,
timestamp=submission.created_utc
)
s.save()
print(f"Added Reddit post {submission.id} as record #{s.id}")
new_requests.append(s)
if new_requests:
await self.message(room, f"Found {len(new_requests)} new Reddit posts in /r/monerosupport. Use `!l` or `!list` to see them.")
await asyncio.sleep(30)
# solved = BooleanField(default=False)
# assigned = BooleanField(default=False)
# assignee = ForeignKey(IRCSupportOperator, backref='assignee')
#
# class IRCSupportOperator(BaseModel):
# irc_nick = CharField()
# is_a_regular = BooleanField()
# is_support_admin = BooleanField()
async def on_message(self, target, source, message):
if source == self.nickname:
@ -18,6 +51,42 @@ class IRCBot(pydle.Client):
if self.nickname in message:
await self.message(target, f"Sup. I'm not very helpful yet, but getting there.")
if message in ["!list"]:
s = []
reqs = SupportRequest.select().where(
SupportRequest.solved==False
).order_by(SupportRequest.timestamp.desc())
for req in reqs:
s.append(f"{req.id} - {req.author} - {arrow.get(req.timestamp).humanize()}")
await self.message(target, ", ".join(s[0:9]))
if message.startswith("!request ") or message in ["!request"]:
msg = message.split()
if not len(msg) > 1:
await self.message(target, "Invalid arguments")
return
try:
post_id = int(msg[1])
except:
await self.message(target, "Invalid arguments")
return
req = SupportRequest.select().where(
SupportRequest.id==post_id
).first()
if req:
if req.assigned:
_a = "ASSIGNED"
else:
_a = "UNASSIGNED"
await self.message(target, f"{req.id} - {req.title} - https://reddit.com/{req.permalink} - {_a}")
else:
await self.message(target, "No record with that ID")
if message in ["!help"]:
await self.message(target, "not ready")
async def is_admin(self, nickname):
admin = False
if nickname in config.ADMIN_NICKNAMES:

@ -1,4 +1,4 @@
import datetime
from datetime import datetime
from peewee import *
from supportbot import config
@ -9,7 +9,19 @@ class BaseModel(Model):
class Meta:
database = db
class IRCSupportOperator(BaseModel):
irc_nick = CharField()
is_a_regular = BooleanField()
is_support_admin = BooleanField()
class SupportRequest(BaseModel):
reddit_user = CharField()
post_id = CharField()
timestamp = DateTimeField(default=datetime.now)
author = CharField()
title = CharField()
permalink = CharField()
solved = BooleanField(default=False)
# todo...
assigned = BooleanField(default=False)
assignee = ForeignKeyField(IRCSupportOperator, backref='assignee', null=True)
db.create_tables([SupportRequest, IRCSupportOperator])

Loading…
Cancel
Save