RcGcDb/src/irc_feed.py

77 lines
2.5 KiB
Python
Raw Normal View History

2021-01-20 15:45:17 +00:00
import irc.client_aio
import json
2021-02-20 15:33:44 +00:00
import logging
2021-01-20 15:45:17 +00:00
from urllib.parse import urlparse, quote
2021-02-20 15:33:44 +00:00
logger = logging.getLogger("rcgcdw.irc_feed")
2021-01-20 15:45:17 +00:00
class AioIRCCat(irc.client_aio.AioSimpleIRCClient):
def connect(self, *args, **kwargs):
super().connect(*args, **kwargs)
self.connection_details = (args, kwargs)
2021-01-20 15:45:17 +00:00
def __init__(self, targets, all_wikis):
2021-02-20 15:33:44 +00:00
irc.client_aio.SimpleIRCClient.__init__(self)
2021-01-20 15:45:17 +00:00
self.targets = targets
2021-02-20 15:33:44 +00:00
self.updated = set() # Storage for edited wikis
self.updated_discussions = set()
2021-01-20 15:45:17 +00:00
self.wikis = all_wikis
self.connection.buffer_class.errors = "replace" # Ignore encoding errors
self.connection_details = None
2021-01-20 15:45:17 +00:00
def on_welcome(self, connection, event): # Join IRC channels
for channel in self.targets.values():
connection.join(channel)
2021-02-20 15:33:44 +00:00
def on_pubmsg(self, connection, event):
if event.target == self.targets["rc"]:
2021-01-21 13:40:55 +00:00
self.parse_fandom_message(' '.join(event.arguments))
2021-02-20 15:33:44 +00:00
elif event.target == self.targets["discussion"]:
2021-01-21 13:40:55 +00:00
self.parse_fandom_discussion(' '.join(event.arguments))
2021-01-20 15:45:17 +00:00
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_disconnect(self, connection, event):
self.connect(*self.connection_details[0], **self.connection_details[1]) # attempt to reconnect
2021-03-16 21:38:57 +00:00
def parse_fandom_message(self, message: str):
2021-01-20 15:45:17 +00:00
message = message.split("\x035*\x03")
# print(asyncio.all_tasks())
half = message[0].find("\x0302http")
if half == -1:
return
message = message[0][half + 3:].strip()
# print(message)
url = urlparse(message)
2021-02-20 15:33:44 +00:00
full_url = "https://"+url.netloc + recognize_langs(url.path)
if full_url in self.wikis and self.wikis[full_url].rc_active != -1:
self.updated.add(full_url)
logger.debug("New website appended to the list! {}".format(full_url))
2021-01-20 15:45:17 +00:00
2021-03-16 21:38:57 +00:00
def parse_fandom_discussion(self, message: str):
try:
post = json.loads(message)
except json.JSONDecodeError:
logger.warning("Seems like we have invalid JSON in Discussions part, message: {}".format(message))
return
if post.get('action', 'unknown') != "deleted": # ignore deletion events
url = urlparse(post.get('url'))
2021-02-20 15:33:44 +00:00
full_url ="https://"+ url.netloc + recognize_langs(url.path)
if full_url in self.wikis: # POSSIBLE MEMORY LEAK AS WE DON'T HAVE A WAY TO CHECK IF WIKI IS LOOKING FOR DISCUSSIONS OR NOT
self.updated_discussions.add("https://"+full_url)
logger.debug("New website appended to the list (discussions)! {}".format(full_url))
2021-01-20 15:45:17 +00:00
def recognize_langs(path):
lang = ""
new_path = path.split("/")
if len(new_path)>2:
2021-02-20 15:33:44 +00:00
if new_path[1] not in ("wiki", "f"):
2021-01-20 15:45:17 +00:00
lang = "/"+new_path[1]
2021-02-20 15:33:44 +00:00
return lang+"/"
2021-01-20 15:45:17 +00:00