RcGcDw/src/discussions.py

152 lines
6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw).
# RcGcDw is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# RcGcDw is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
2020-08-08 00:21:20 +00:00
import logging, schedule, requests
2020-08-23 13:32:12 +00:00
from typing import Dict, Any
2020-07-07 11:21:49 +00:00
from src.configloader import settings
2021-05-07 18:30:13 +00:00
#from src.discussion_formatters import embed_formatter, compact_formatter
2021-05-14 12:30:52 +00:00
from src.misc import datafile, prepare_paths, run_hooks
2021-05-07 18:30:13 +00:00
from src.discord.queue import messagequeue, send_to_discord
from src.discord.message import DiscordMessageMetadata
2020-07-07 11:21:49 +00:00
from src.session import session
2020-08-23 13:32:12 +00:00
from src.exceptions import ArticleCommentError
2021-05-07 18:30:13 +00:00
from src.api.util import default_message
from src.api.context import Context
from src.api.hooks import formatter_hooks, pre_hooks, post_hooks
# Create a custom logger
discussion_logger = logging.getLogger("rcgcdw.disc")
2020-04-05 00:07:56 +00:00
# Create a variable in datafile if it doesn't exist yet (in files <1.10)
if "discussion_id" not in datafile.data:
datafile["discussion_id"] = 0
2020-04-05 00:07:56 +00:00
datafile.save_datafile()
storage = datafile
2020-04-05 00:07:56 +00:00
2021-05-07 18:30:13 +00:00
global client
# setup a few things first so we don't have to do expensive nested get operations every time
fetch_url = "{wiki}wikia.php?controller=DiscussionPost&method=getPosts&sortDirection=descending&sortKey=creation_date&limit={limit}&includeCounters=false".format(wiki=settings["fandom_discussions"]["wiki_url"], limit=settings["fandom_discussions"]["limit"])
2020-08-23 13:32:12 +00:00
domain = prepare_paths(settings["fandom_discussions"]["wiki_url"], dry=True) # Shutdown if the path for discussions is wrong
2021-05-07 18:30:13 +00:00
display_mode = settings.get("fandom_discussions", {}).get("appearance", {}).get("mode", "embed")
2021-05-09 19:16:24 +00:00
webhook_url = settings.get("fandom_discussions", {}).get("webhookURL", settings.get("webhookURL"))
2021-05-07 18:30:13 +00:00
2020-04-05 00:07:56 +00:00
def inject_client(client_obj):
"""Function to avoid circular import issues"""
global client
client = client_obj
def fetch_discussions():
2020-05-06 09:13:51 +00:00
messagequeue.resend_msgs()
2020-04-05 00:07:56 +00:00
request = safe_request(fetch_url)
if request:
try:
request_json = request.json()["_embedded"]["doc:posts"]
request_json.reverse()
except ValueError:
discussion_logger.warning("ValueError in fetching discussions")
return None
except KeyError:
discussion_logger.warning("Wiki returned %s" % (request.json()))
2020-04-05 00:07:56 +00:00
return None
else:
if request_json:
2020-08-23 13:32:12 +00:00
comment_pages: dict = {}
comment_events: list = [post["forumId"] for post in request_json if post["_embedded"]["thread"][0]["containerType"] == "ARTICLE_COMMENT" and int(post["id"]) > storage["discussion_id"]]
if comment_events:
comment_pages = safe_request(
"{wiki}wikia.php?controller=FeedsAndPosts&method=getArticleNamesAndUsernames&stablePageIds={pages}&format=json".format(
wiki=settings["fandom_discussions"]["wiki_url"], pages=",".join(comment_events)
))
if comment_pages:
try:
comment_pages = comment_pages.json()["articleNames"]
except ValueError:
discussion_logger.warning("ValueError in fetching discussions")
return None
except KeyError:
discussion_logger.warning("Wiki returned %s" % (request_json.json()))
return None
else:
return None
2020-04-05 00:07:56 +00:00
for post in request_json:
2020-04-05 21:50:36 +00:00
if int(post["id"]) > storage["discussion_id"]:
2020-08-23 13:32:12 +00:00
try:
2021-05-03 11:37:42 +00:00
discussion_logger.debug(f"Sending discussion post with ID {post['id']}")
2020-08-23 13:32:12 +00:00
parse_discussion_post(post, comment_pages)
except ArticleCommentError:
return None
2020-04-05 21:50:36 +00:00
if int(post["id"]) > storage["discussion_id"]:
storage["discussion_id"] = int(post["id"])
2020-04-05 00:07:56 +00:00
datafile.save_datafile()
2021-05-07 18:30:13 +00:00
2020-08-23 13:32:12 +00:00
def parse_discussion_post(post, comment_pages):
2020-04-25 14:06:15 +00:00
"""Initial post recognition & handling"""
2021-05-07 18:30:13 +00:00
global client
2020-08-05 00:08:38 +00:00
post_type = post["_embedded"]["thread"][0]["containerType"]
2021-05-07 18:30:13 +00:00
context = Context(display_mode, webhook_url, client)
2020-08-16 13:01:16 +00:00
# Filter posts by forum
if post_type == "FORUM" and settings["fandom_discussions"].get("show_forums", []):
if not post["forumName"] in settings["fandom_discussions"]["show_forums"]:
discussion_logger.debug(f"Ignoring post as it's from {post['forumName']}.")
return
2020-08-23 13:32:12 +00:00
comment_page = None
if post_type == "ARTICLE_COMMENT":
try:
comment_page = {**comment_pages[post["forumId"]], "fullUrl": domain + comment_pages[post["forumId"]]["relativeUrl"]}
except KeyError:
discussion_logger.error("Could not parse paths for article comment, here is the content of comment_pages: {}, ignoring...".format(comment_pages))
raise ArticleCommentError
2021-05-09 19:16:24 +00:00
event_type = f"discussion/{post_type.lower()}"
context.set_comment_page(comment_page)
2021-05-14 12:30:52 +00:00
run_hooks(pre_hooks, context, post)
discord_message = default_message(event_type, formatter_hooks)(context, post)
metadata = DiscordMessageMetadata("POST")
run_hooks(post_hooks, discord_message, metadata, context, post)
2021-05-14 12:30:52 +00:00
send_to_discord(discord_message, metadata)
2020-04-25 14:06:15 +00:00
2020-04-05 00:07:56 +00:00
def safe_request(url):
"""Function to assure safety of request, and do not crash the script on exceptions,"""
try:
2020-04-05 21:50:36 +00:00
request = session.get(url, timeout=10, allow_redirects=False, headers={"Accept": "application/hal+json"})
2020-04-05 00:07:56 +00:00
except requests.exceptions.Timeout:
discussion_logger.warning("Reached timeout error for request on link {url}".format(url=url))
return None
except requests.exceptions.ConnectionError:
discussion_logger.warning("Reached connection error for request on link {url}".format(url=url))
return None
except requests.exceptions.ChunkedEncodingError:
discussion_logger.warning("Detected faulty response from the web server for request on link {url}".format(url=url))
return None
else:
if 499 < request.status_code < 600:
return None
return request
2021-05-07 18:30:13 +00:00
schedule.every(settings["fandom_discussions"]["cooldown"]).seconds.do(fetch_discussions)