Fix discussion issue, add stacker for discussions

This commit is contained in:
Frisk 2020-11-28 23:31:13 +01:00
parent 59d2869f4f
commit ef679e042c
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
3 changed files with 17 additions and 10 deletions

View file

@ -357,7 +357,7 @@ async def discussion_handler():
try:
while True:
fetch_all = db_cursor.execute(
"SELECT wiki, rcid, postid FROM rcgcdw WHERE postid != '-1'")
"SELECT wiki, rcid, postid FROM rcgcdw WHERE postid != '-1' OR postid IS NULL")
for db_wiki in fetch_all.fetchall():
header = settings["header"]
header["Accept"] = "application/hal+json"
@ -376,8 +376,8 @@ async def discussion_handler():
discussion_feed_resp = await feeds_response.json(encoding="UTF-8")
if "title" in discussion_feed_resp:
error = discussion_feed_resp["error"]
if error == "site doesn't exists":
if db_wiki["rcid"] != -1:
if error == "site doesn't exists": # Discussions disabled
if db_wiki["rcid"] != -1: # RC feed is disabled
db_cursor.execute("UPDATE rcgcdw SET postid = ? WHERE wiki = ?",
("-1", db_wiki["wiki"],))
else:
@ -426,11 +426,14 @@ async def discussion_handler():
await generic_msg_sender_exception_logger(traceback.format_exc(),
"Exception on Feeds article comment request",
Post=str(post)[0:1000], Wiki=db_wiki["wiki"])
message_list = defaultdict(list)
for post in discussion_feed: # Yeah, second loop since the comments require an extra request
if post["id"] > db_wiki["postid"]:
for target in targets.items():
try:
await essential_feeds(post, comment_pages, db_wiki, target)
message = await essential_feeds(post, comment_pages, db_wiki, target)
if message is not None:
message_list[target[0]].append(message)
except asyncio.CancelledError:
raise
except:
@ -440,6 +443,10 @@ async def discussion_handler():
else:
logger.exception("Exception on Feeds formatter")
await generic_msg_sender_exception_logger(traceback.format_exc(), "Exception in feed formatter", Post=str(post)[0:1000], Wiki=db_wiki["wiki"])
for messages in message_list.values():
messages = stack_message_list(messages)
for message in messages:
await send_to_discord(message)
if discussion_feed:
DBHandler.add(db_wiki["wiki"], post["id"], True)
await asyncio.sleep(delay=2.0) # hardcoded really doesn't need much more

View file

@ -11,7 +11,7 @@ from src.i18n import langs
logger = logging.getLogger("rcgcdw.discussion_formatters")
async def feeds_compact_formatter(post_type, post, message_target, wiki, article_page=None):
async def feeds_compact_formatter(post_type, post, message_target, wiki, article_page=None) -> DiscordMessage:
"""Compact formatter for Fandom discussions."""
_ = langs[message_target[0][0]]["discussion_formatters"].gettext
message = None
@ -58,10 +58,10 @@ async def feeds_compact_formatter(post_type, post, message_target, wiki, article
return
else:
message = ""+_("Unknown event `{event}` by [{author}]({author_url}), report it on the [support server](<{support}>).").format(event=post_type, author=author, author_url=author_url, support=settings["support"])
await send_to_discord(DiscordMessage("compact", "discussion", message_target[1], content=message, wiki=wiki))
return DiscordMessage("compact", "discussion", message_target[1], content=message, wiki=wiki)
async def feeds_embed_formatter(post_type, post, message_target, wiki, article_page=None):
async def feeds_embed_formatter(post_type, post, message_target, wiki, article_page=None) -> DiscordMessage:
"""Embed formatter for Fandom discussions."""
_ = langs[message_target[0][0]]["discussion_formatters"].gettext
embed = DiscordMessage("embed", "discussion", message_target[1], wiki=wiki)
@ -159,7 +159,7 @@ async def feeds_embed_formatter(post_type, post, message_target, wiki, article_p
else:
embed.add_field(_("Report this on the support server"), change_params)
embed.finish_embed()
await send_to_discord(embed)
return embed
class DiscussionsFromHellParser:

View file

@ -239,7 +239,7 @@ async def essential_info(change: dict, changed_categories, local_wiki: Wiki, tar
return await appearance_mode(identification_string, change, parsed_comment, changed_categories, local_wiki, target, paths, rate_limiter, additional_data=additional_data)
async def essential_feeds(change: dict, comment_pages: dict, db_wiki: sqlite3.Row, target: tuple):
async def essential_feeds(change: dict, comment_pages: dict, db_wiki: sqlite3.Row, target: tuple) -> src.discord.DiscordMessage:
"""Prepares essential information for both embed and compact message format."""
appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter
identification_string = change["_embedded"]["thread"][0]["containerType"]
@ -248,4 +248,4 @@ async def essential_feeds(change: dict, comment_pages: dict, db_wiki: sqlite3.Ro
comment_page = comment_pages.get(change["forumId"], None)
if comment_page is not None:
comment_page["fullUrl"] = "/".join(db_wiki["wiki"].split("/", 3)[:3]) + comment_page["relativeUrl"].replace(")", "\)").replace("()", "\(")
await appearance_mode(identification_string, change, target, db_wiki["wiki"], article_page=comment_page)
return await appearance_mode(identification_string, change, target, db_wiki["wiki"], article_page=comment_page)