diff --git a/discussions.py b/discussions.py
index dfab679..eb3209a 100644
--- a/discussions.py
+++ b/discussions.py
@@ -16,9 +16,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
-import logging, gettext
+import logging, gettext, schedule, requests
from configloader import settings
from misc import datafile, WIKI_SCRIPT_PATH
+from session import session
# Initialize translation
@@ -29,7 +30,70 @@ _ = t.gettext
discussion_logger = logging.getLogger("rcgcdw.disc")
+# Create a variable in datafile if it doesn't exist yet (in files <1.10)
+
+if "discussion_id" not in datafile.data:
+ datafile.data["discussion_id"] = 0
+ datafile.save_datafile()
+
+storage = datafile.data
+
fetch_url = "https://services.fandom.com/discussion/{wikiid}/posts?sortDirection=descending&sortKey=creation_date&limit={limit}".format(wikiid=settings["fandom_discussions"]["wiki_id"], limit=settings["fandom_discussions"]["limit"])
+
+def embed_formatter(post):
+ """Embed formatter for Fandom discussions."""
+ pass
+
+
+def compact_formatter(post):
+ """Compact formatter for Fandom discussions."""
+ message = None
+ if post["isReply"]:
+ pass
+ else:
+ pass
+
+
def fetch_discussions():
- pass
\ No newline at end of file
+ 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.json()))
+ return None
+ else:
+ if request_json:
+ for post in request_json:
+ if post["id"] > storage["discussion_id"]:
+ formatter(post)
+ if post["id"] > storage["discussion_id"]:
+ storage["discussion_id"] = post["id"]
+ datafile.save_datafile()
+
+def safe_request(url):
+ """Function to assure safety of request, and do not crash the script on exceptions,"""
+ try:
+ request = session.get(url, timeout=10, allow_redirects=False, header={"Accept": "application/hal+json"})
+ 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
+
+formatter = embed_formatter if settings["fandom_discussions"]["appearance"]["mode"] == "embed" else compact_formatter
+
+schedule.every(settings["fandom_discussions"]["cooldown"]).seconds.do(fetch_discussions)
\ No newline at end of file
diff --git a/misc.py b/misc.py
index 8bf1f9f..abe6c89 100644
--- a/misc.py
+++ b/misc.py
@@ -245,4 +245,10 @@ def prepare_paths():
misc_logger.critical("Could not verify wikis paths. Please make sure you have given the proper wiki URL in settings.json and your Internet connection is working.")
sys.exit(1)
-prepare_paths()
\ No newline at end of file
+
+prepare_paths()
+
+
+def create_article_path(article: str) -> str:
+ """Takes the string and creates an URL with it as the article name"""
+ return WIKI_ARTICLE_PATH.replace("$1", article)
diff --git a/rcgcdw.py b/rcgcdw.py
index 0d024ca..6f313aa 100644
--- a/rcgcdw.py
+++ b/rcgcdw.py
@@ -29,10 +29,11 @@ from collections import defaultdict, Counter
from urllib.parse import quote_plus
from configloader import settings
from misc import link_formatter, ContentParser, safe_read, handle_discord_http, add_to_dict, datafile, \
- WIKI_API_PATH, WIKI_ARTICLE_PATH, WIKI_SCRIPT_PATH, WIKI_JUST_DOMAIN
+ WIKI_API_PATH, WIKI_SCRIPT_PATH, WIKI_JUST_DOMAIN, create_article_path
+from session import session
if settings["fandom_discussions"]["enabled"]:
- pass
+ import discussions
if __name__ != "__main__": # return if called as a module
logging.critical("The file is being executed as a module. Please execute the script using the console.")
@@ -113,10 +114,6 @@ class MWError(Exception):
pass
-def create_article_path(article: str) -> str:
- """Takes the string and creates an URL with it as the article name"""
- return WIKI_ARTICLE_PATH.replace("$1", article)
-
def send(message, name, avatar):
dictionary_creator = {"content": message}
if name:
@@ -188,7 +185,7 @@ def pull_comment(comment_id):
def compact_formatter(action, change, parsed_comment, categories):
if action != "suppressed":
- author_url = link_formatter(create_article_path("User:{user}".format( user=change["user"])))
+ author_url = link_formatter(create_article_path("User:{user}".format(user=change["user"])))
author = change["user"]
parsed_comment = "" if parsed_comment is None else " *("+parsed_comment+")*"
parsed_comment = re.sub(r"([^<]|\A)(http(s)://.*?)( |\Z)", "\\1<\\2>\\4", parsed_comment) # see #97
@@ -399,7 +396,8 @@ def compact_formatter(action, change, parsed_comment, categories):
link = link_formatter(create_article_path("Special:AbuseFilter/history/{number}/diff/prev/{historyid}".format(number=change["logparams"]['newId'], historyid=change["logparams"]["historyId"])))
content = _("[{author}]({author_url}) edited abuse filter [number {number}]({filter_url})").format(author=author, author_url=author_url, number=change["logparams"]['newId'], filter_url=link)
elif action == "abusefilter/create":
- link = link_formatter(create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId'])))
+ link = link_formatter(
+ create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId'])))
content = _("[{author}]({author_url}) created abuse filter [number {number}]({filter_url})").format(author=author, author_url=author_url, number=change["logparams"]['newId'], filter_url=link)
elif action == "merge/merge":
link = link_formatter(create_article_path(change["title"]))
@@ -808,7 +806,7 @@ def embed_formatter(action, change, parsed_comment, categories):
link = create_article_path("Special:AbuseFilter/history/{number}/diff/prev/{historyid}".format(number=change["logparams"]['newId'], historyid=change["logparams"]["historyId"]))
embed["title"] = _("Edited abuse filter number {number}").format(number=change["logparams"]['newId'])
elif action == "abusefilter/create":
- link = create_article_path("Special:AbuseFilter/{number}".format( number=change["logparams"]['newId']))
+ link = create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId']))
embed["title"] = _("Created abuse filter number {number}").format(number=change["logparams"]['newId'])
elif action == "merge/merge":
link = create_article_path(change["title"].replace(" ", "_"))
@@ -1135,8 +1133,7 @@ class Recent_Changes_Class(object):
self.unsent_messages = []
self.mw_messages = {}
self.namespaces = None
- self.session = requests.Session()
- self.session.headers.update(settings["header"])
+ self.session = session
if settings["limitrefetch"] != -1:
self.file_id = storage["rcid"]
else:
diff --git a/session.py b/session.py
new file mode 100644
index 0000000..e8a5381
--- /dev/null
+++ b/session.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+# Recent changes Goat compatible Discord webhook is a project for using a webhook as recent changes page from MediaWiki.
+# Copyright (C) 2020 Frisk
+
+# This program 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.
+
+# This program 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 this program. If not, see .
+
+import requests
+from configloader import settings
+
+session = requests.Session()
+session.headers.update(settings["header"])