Finalizing the discussions module

This commit is contained in:
Frisk 2020-04-05 23:50:36 +02:00
parent 3e17faf68e
commit f6e7e45a38
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
3 changed files with 70 additions and 49 deletions

View file

@ -16,9 +16,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging, gettext, schedule, requests
import logging, gettext, schedule, requests, json, datetime
from collections import defaultdict
from configloader import settings
from misc import datafile, WIKI_SCRIPT_PATH
from misc import datafile, WIKI_SCRIPT_PATH, send_to_discord
from session import session
# Initialize translation
@ -43,20 +44,38 @@ fetch_url = "https://services.fandom.com/discussion/{wikiid}/posts?sortDirection
def embed_formatter(post):
"""Embed formatter for Fandom discussions."""
pass
embed = defaultdict(dict)
data = {"embeds": []}
embed["author"]["name"] = post["createdBy"]["name"]
embed["author"]["icon_url"] = post["createdBy"]["avatarUrl"]
embed["author"]["url"] = "{wikiurl}f/u/{creatorId}".format(wikiurl=WIKI_SCRIPT_PATH, creatorId=post["creatorId"])
if post["isReply"]:
embed["title"] = _("Replied to {title}").format(title=post["_embedded"]["thread"][0]["title"])
embed["url"] = "{wikiurl}f/p/{threadId}/r/{postId}".format(wikiurl=WIKI_SCRIPT_PATH, threadId=post["threadId"], postId=post["id"])
else:
embed["title"] = _("Created {title}").format(title=post["title"])
embed["url"] = "{wikiurl}f/p/{threadId}".format(wikiurl=WIKI_SCRIPT_PATH, threadId=post["threadId"])
if settings["fandom_discussions"]["appearance"]["embed"]["show_content"]:
embed["description"] = post["rawContent"]
embed["footer"]["text"] = post["forumName"]
embed["timestamp"] = datetime.datetime.fromtimestamp(post["creationDate"]["epochSecond"]).isoformat()+"Z"
data["embeds"].append(dict(embed))
data['avatar_url'] = settings["avatars"]["embed"]
data['allowed_mentions'] = {'parse': []}
formatted_embed = json.dumps(data, indent=4)
send_to_discord(formatted_embed)
def compact_formatter(post):
"""Compact formatter for Fandom discussions."""
message = None
if post["isReply"]:
message = _("[{author}](<{url}f/u/{creatorId}>) created [{title}](<{url}f/p/{threadId}>) in ${forumName}".format(
author=post["createdBy"]["name"], url=WIKI_SCRIPT_PATH, creatorId=post["creatorId"], title=post["title"], threadId=post["threadId"], forumName=post["forumName"]))
message = _("[{author}](<{url}f/u/{creatorId}>) created [{title}](<{url}f/p/{threadId}>) in {forumName}").format(
author=post["createdBy"]["name"], url=WIKI_SCRIPT_PATH, creatorId=post["creatorId"], title=post["title"], threadId=post["threadId"], forumName=post["forumName"])
else:
message = _("[${author}](<{url}f/u/{creatorId}>) created a [reply](<{url}f/p/{threadId}/r/{postId}>) to [{title}](<{url}f/p/{threadId}>) in {forumName}".format(
message = _("[{author}](<{url}f/u/{creatorId}>) created a [reply](<{url}f/p/{threadId}/r/{postId}>) to [{title}](<{url}f/p/{threadId}>) in {forumName}").format(
author=post["createdBy"]["name"], url=WIKI_SCRIPT_PATH, creatorId=post["creatorId"], threadId=post["threadId"], postId=post["id"], title=post["_embedded"]["thread"][0]["title"], forumName=post["forumName"]
))
{"content": message}
)
send_to_discord(json.dumps({'content': message, 'allowed_mentions': {'parse': []}}))
def fetch_discussions():
@ -74,16 +93,16 @@ def fetch_discussions():
else:
if request_json:
for post in request_json:
if post["id"] > storage["discussion_id"]:
if int(post["id"]) > storage["discussion_id"]:
formatter(post)
if post["id"] > storage["discussion_id"]:
storage["discussion_id"] = post["id"]
if int(post["id"]) > storage["discussion_id"]:
storage["discussion_id"] = int(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"})
request = session.get(url, timeout=10, allow_redirects=False, headers={"Accept": "application/hal+json"})
except requests.exceptions.Timeout:
discussion_logger.warning("Reached timeout error for request on link {url}".format(url=url))
return None

36
misc.py
View file

@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json, logging, sys, re
import json, logging, sys, re, time
from html.parser import HTMLParser
from urllib.parse import urlparse, urlunparse
import requests
@ -277,3 +277,37 @@ 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)
def send_to_discord_webhook(data):
header = settings["header"]
if isinstance(data, str):
header['Content-Type'] = 'application/json'
else:
header['Content-Type'] = 'application/x-www-form-urlencoded'
try:
result = requests.post(settings["webhookURL"], data=data,
headers=header, timeout=10)
except requests.exceptions.Timeout:
misc_logger.warning("Timeouted while sending data to the webhook.")
return 3
except requests.exceptions.ConnectionError:
misc_logger.warning("Connection error while sending the data to a webhook")
return 3
else:
return handle_discord_http(result.status_code, data, result)
def send_to_discord(data):
if messagequeue:
messagequeue.add_message(data)
else:
code = send_to_discord_webhook(data)
if code == 3:
messagequeue.add_message(data)
elif code == 2:
time.sleep(5.0)
messagequeue.add_message(data)
elif code < 2:
time.sleep(2.0)
pass

View file

@ -28,8 +28,9 @@ from bs4 import BeautifulSoup
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_SCRIPT_PATH, WIKI_JUST_DOMAIN, create_article_path, messagequeue
from misc import link_formatter, ContentParser, safe_read, add_to_dict, datafile, \
WIKI_API_PATH, WIKI_SCRIPT_PATH, WIKI_JUST_DOMAIN, create_article_path, messagequeue, send_to_discord_webhook, \
send_to_discord
from session import session
if settings["fandom_discussions"]["enabled"]:
@ -132,39 +133,6 @@ def profile_field_name(name, embed):
else:
return _("unknown")
def send_to_discord_webhook(data):
header = settings["header"]
if isinstance(data, str):
header['Content-Type'] = 'application/json'
else:
header['Content-Type'] = 'application/x-www-form-urlencoded'
try:
result = requests.post(settings["webhookURL"], data=data,
headers=header, timeout=10)
except requests.exceptions.Timeout:
logger.warning("Timeouted while sending data to the webhook.")
return 3
except requests.exceptions.ConnectionError:
logger.warning("Connection error while sending the data to a webhook")
return 3
else:
return handle_discord_http(result.status_code, data, result)
def send_to_discord(data):
if messagequeue:
messagequeue.add_message(data)
else:
code = send_to_discord_webhook(data)
if code == 3:
messagequeue.add_message(data)
elif code == 2:
time.sleep(5.0)
messagequeue.add_message(data)
elif code < 2:
time.sleep(2.0)
pass
def pull_comment(comment_id):
try: