|
|
|
@ -11,7 +11,7 @@ from typing import Generator
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
from src.argparser import command_line_args
|
|
|
|
|
from src.config import settings
|
|
|
|
|
from src.database import db_cursor, db_connection
|
|
|
|
|
from src.database import db
|
|
|
|
|
from src.exceptions import *
|
|
|
|
|
from src.misc import get_paths, get_domain
|
|
|
|
|
from src.msgqueue import messagequeue, send_to_discord
|
|
|
|
@ -35,13 +35,17 @@ all_wikis: dict = {}
|
|
|
|
|
mw_msgs: dict = {} # will have the type of id: tuple
|
|
|
|
|
main_tasks: dict = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# First populate the all_wikis list with every wiki
|
|
|
|
|
# Reasons for this: 1. we require amount of wikis to calculate the cooldown between requests
|
|
|
|
|
# 2. Easier to code
|
|
|
|
|
|
|
|
|
|
for db_wiki in db_cursor.execute('SELECT wiki, rcid FROM rcgcdw GROUP BY wiki ORDER BY ROWID'):
|
|
|
|
|
all_wikis[db_wiki["wiki"]] = Wiki() # populate all_wikis
|
|
|
|
|
all_wikis[db_wiki["wiki"]].rc_active = db_wiki["rcid"]
|
|
|
|
|
async def populate_allwikis():
|
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
|
async with connection.transaction():
|
|
|
|
|
async for db_wiki in connection.cursor('SELECT DISTINCT wiki, rcid FROM rcgcdw'):
|
|
|
|
|
all_wikis[db_wiki["wiki"]] = Wiki() # populate all_wikis
|
|
|
|
|
all_wikis[db_wiki["wiki"]].rc_active = db_wiki["rcid"]
|
|
|
|
|
|
|
|
|
|
queue_limit = settings.get("queue_limit", 30)
|
|
|
|
|
QueuedWiki = namedtuple("QueuedWiki", ['url', 'amount'])
|
|
|
|
@ -92,7 +96,7 @@ class RcQueue:
|
|
|
|
|
all_wikis[wiki].rc_active = -1
|
|
|
|
|
if not self[group]["query"]: # if there is no wiki left in the queue, get rid of the task
|
|
|
|
|
logger.debug(f"{group} no longer has any wikis queued!")
|
|
|
|
|
if not self.check_if_domain_in_db(group):
|
|
|
|
|
if not await self.check_if_domain_in_db(group):
|
|
|
|
|
await self.stop_task_group(group)
|
|
|
|
|
else:
|
|
|
|
|
logger.debug(f"But there are still wikis for it in DB!")
|
|
|
|
@ -101,12 +105,12 @@ class RcQueue:
|
|
|
|
|
self[group]["task"].cancel()
|
|
|
|
|
del self.domain_list[group]
|
|
|
|
|
|
|
|
|
|
def check_if_domain_in_db(self, domain):
|
|
|
|
|
fetch_all = db_cursor.execute(
|
|
|
|
|
'SELECT ROWID, webhook, wiki, lang, display, rcid FROM rcgcdw WHERE rcid != -1 GROUP BY wiki ORDER BY ROWID ASC')
|
|
|
|
|
for wiki in fetch_all.fetchall():
|
|
|
|
|
if get_domain(wiki["wiki"]) == domain:
|
|
|
|
|
return True
|
|
|
|
|
async def check_if_domain_in_db(self, domain):
|
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
|
async with connection.transaction():
|
|
|
|
|
async for wiki in connection.cursor('SELECT DISTINCT wiki FROM rcgcdw WHERE rcid != -1;'):
|
|
|
|
|
if get_domain(wiki["wiki"]) == domain:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
@ -143,50 +147,50 @@ class RcQueue:
|
|
|
|
|
async def update_queues(self):
|
|
|
|
|
"""Makes a round on rcgcdb DB and looks for updates to the queues in self.domain_list"""
|
|
|
|
|
try:
|
|
|
|
|
fetch_all = db_cursor.execute(
|
|
|
|
|
'SELECT ROWID, webhook, wiki, lang, display, rcid FROM rcgcdw WHERE rcid != -1 OR rcid IS NULL GROUP BY wiki ORDER BY ROWID ASC')
|
|
|
|
|
self.to_remove = [x[0] for x in filter(self.filter_rc_active, all_wikis.items())] # first populate this list and remove wikis that are still in the db, clean up the rest
|
|
|
|
|
full = set()
|
|
|
|
|
for db_wiki in fetch_all.fetchall():
|
|
|
|
|
domain = get_domain(db_wiki["wiki"])
|
|
|
|
|
try:
|
|
|
|
|
if db_wiki["wiki"] not in all_wikis:
|
|
|
|
|
raise AssertionError
|
|
|
|
|
self.to_remove.remove(db_wiki["wiki"])
|
|
|
|
|
except AssertionError:
|
|
|
|
|
all_wikis[db_wiki["wiki"]] = Wiki()
|
|
|
|
|
all_wikis[db_wiki["wiki"]].rc_active = db_wiki["rcid"]
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
if domain in full:
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
current_domain: dict = self[domain]
|
|
|
|
|
if current_domain["irc"]:
|
|
|
|
|
logger.debug("DOMAIN LIST FOR IRC: {}".format(current_domain["irc"].updated))
|
|
|
|
|
logger.debug("CURRENT DOMAIN INFO: {}".format(domain))
|
|
|
|
|
logger.debug("IS WIKI IN A LIST?: {}".format(db_wiki["wiki"] in current_domain["irc"].updated))
|
|
|
|
|
logger.debug("LAST CHECK FOR THE WIKI {} IS {}".format(db_wiki["wiki"], all_wikis[db_wiki["wiki"]].last_check))
|
|
|
|
|
if db_wiki["wiki"] in current_domain["irc"].updated: # Priority wikis are the ones with IRC, if they get updated forcefully add them to queue
|
|
|
|
|
current_domain["irc"].updated.remove(db_wiki["wiki"])
|
|
|
|
|
current_domain["query"].append(QueuedWiki(db_wiki["wiki"], 20), forced=True)
|
|
|
|
|
logger.debug("Updated in IRC so adding to queue.")
|
|
|
|
|
continue
|
|
|
|
|
elif all_wikis[db_wiki["wiki"]].last_check+settings["irc_overtime"] < time.time(): # if time went by and wiki should be updated now use default mechanics
|
|
|
|
|
logger.debug("Overtime so adding to queue.")
|
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
|
async with connection.transaction():
|
|
|
|
|
async for db_wiki in connection.cursor('SELECT DISTINCT wiki, row_number() OVER (ORDER BY webhook) AS rowid, webhook, lang, display, rcid FROM rcgcdw WHERE rcid != -1 OR rcid IS NULL ORDER BY webhook'):
|
|
|
|
|
domain = get_domain(db_wiki["wiki"])
|
|
|
|
|
try:
|
|
|
|
|
if db_wiki["wiki"] not in all_wikis:
|
|
|
|
|
raise AssertionError
|
|
|
|
|
self.to_remove.remove(db_wiki["wiki"])
|
|
|
|
|
except AssertionError:
|
|
|
|
|
all_wikis[db_wiki["wiki"]] = Wiki()
|
|
|
|
|
all_wikis[db_wiki["wiki"]].rc_active = db_wiki["rcid"]
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
else: # Continue without adding
|
|
|
|
|
logger.debug("No condition fulfilled so skipping.")
|
|
|
|
|
if domain in full:
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
current_domain: dict = self[domain]
|
|
|
|
|
if current_domain["irc"]:
|
|
|
|
|
logger.debug("DOMAIN LIST FOR IRC: {}".format(current_domain["irc"].updated))
|
|
|
|
|
logger.debug("CURRENT DOMAIN INFO: {}".format(domain))
|
|
|
|
|
logger.debug("IS WIKI IN A LIST?: {}".format(db_wiki["wiki"] in current_domain["irc"].updated))
|
|
|
|
|
logger.debug("LAST CHECK FOR THE WIKI {} IS {}".format(db_wiki["wiki"], all_wikis[db_wiki["wiki"]].last_check))
|
|
|
|
|
if db_wiki["wiki"] in current_domain["irc"].updated: # Priority wikis are the ones with IRC, if they get updated forcefully add them to queue
|
|
|
|
|
current_domain["irc"].updated.remove(db_wiki["wiki"])
|
|
|
|
|
current_domain["query"].append(QueuedWiki(db_wiki["wiki"], 20), forced=True)
|
|
|
|
|
logger.debug("Updated in IRC so adding to queue.")
|
|
|
|
|
continue
|
|
|
|
|
elif all_wikis[db_wiki["wiki"]].last_check+settings["irc_overtime"] < time.time(): # if time went by and wiki should be updated now use default mechanics
|
|
|
|
|
logger.debug("Overtime so adding to queue.")
|
|
|
|
|
pass
|
|
|
|
|
else: # Continue without adding
|
|
|
|
|
logger.debug("No condition fulfilled so skipping.")
|
|
|
|
|
continue
|
|
|
|
|
if not db_wiki["rowid"] < current_domain["last_rowid"]:
|
|
|
|
|
current_domain["query"].append(QueuedWiki(db_wiki["wiki"], 20))
|
|
|
|
|
except KeyError:
|
|
|
|
|
await self.start_group(domain, [QueuedWiki(db_wiki["wiki"], 20)])
|
|
|
|
|
logger.info("A new domain group ({}) has been added since last time, adding it to the domain_list and starting a task...".format(domain))
|
|
|
|
|
except ListFull:
|
|
|
|
|
full.add(domain)
|
|
|
|
|
current_domain["last_rowid"] = db_wiki["rowid"]
|
|
|
|
|
continue
|
|
|
|
|
if not db_wiki["ROWID"] < current_domain["last_rowid"]:
|
|
|
|
|
current_domain["query"].append(QueuedWiki(db_wiki["wiki"], 20))
|
|
|
|
|
except KeyError:
|
|
|
|
|
await self.start_group(domain, [QueuedWiki(db_wiki["wiki"], 20)])
|
|
|
|
|
logger.info("A new domain group ({}) has been added since last time, adding it to the domain_list and starting a task...".format(domain))
|
|
|
|
|
except ListFull:
|
|
|
|
|
full.add(domain)
|
|
|
|
|
current_domain["last_rowid"] = db_wiki["ROWID"]
|
|
|
|
|
continue
|
|
|
|
|
for wiki in self.to_remove:
|
|
|
|
|
await self.remove_wiki_from_group(wiki)
|
|
|
|
|
for group, data in self.domain_list.items():
|
|
|
|
@ -226,24 +230,29 @@ def calculate_delay_for_group(group_length: int) -> float:
|
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_targets(wiki_url: str, additional_requirements: str) -> defaultdict:
|
|
|
|
|
async def generate_targets(wiki_url: str, additional_requirements: str) -> defaultdict:
|
|
|
|
|
"""To minimize the amount of requests, we generate a list of language/display mode combinations to create messages for
|
|
|
|
|
this way we can send the same message to multiple webhooks which have the same wiki and settings without doing another
|
|
|
|
|
request to the wiki just to duplicate the message.
|
|
|
|
|
"""
|
|
|
|
|
combinations = defaultdict(list)
|
|
|
|
|
for webhook in db_cursor.execute('SELECT webhook, lang, display FROM rcgcdw WHERE wiki = ? {}'.format(additional_requirements), (wiki_url,)):
|
|
|
|
|
combination = (webhook["lang"], webhook["display"])
|
|
|
|
|
combinations[combination].append(webhook["webhook"])
|
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
|
async with connection.transaction():
|
|
|
|
|
async for webhook in connection.cursor('SELECT webhook, lang, display FROM rcgcdw WHERE wiki = $1 {}'.format(additional_requirements), wiki_url):
|
|
|
|
|
combination = (webhook["lang"], webhook["display"])
|
|
|
|
|
combinations[combination].append(webhook["webhook"])
|
|
|
|
|
return combinations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def generate_domain_groups():
|
|
|
|
|
"""Generate a list of wikis per domain (fandom.com, wikipedia.org etc.)"""
|
|
|
|
|
"""Generate a list of wikis per domain (fandom.com, wikipedia.org etc.)
|
|
|
|
|
|
|
|
|
|
:returns tuple[str, list]"""
|
|
|
|
|
domain_wikis = defaultdict(list)
|
|
|
|
|
fetch_all = db_cursor.execute('SELECT ROWID, webhook, wiki, lang, display, rcid FROM rcgcdw WHERE rcid != -1 OR rcid IS NULL GROUP BY wiki ORDER BY ROWID ASC')
|
|
|
|
|
for db_wiki in fetch_all.fetchall():
|
|
|
|
|
domain_wikis[get_domain(db_wiki["wiki"])].append(QueuedWiki(db_wiki["wiki"], 20))
|
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
|
async with connection.transaction():
|
|
|
|
|
async for db_wiki in connection.cursor('SELECT DISTINCT wiki, webhook, lang, display, rcid FROM rcgcdw WHERE rcid != -1 OR rcid IS NULL'):
|
|
|
|
|
domain_wikis[get_domain(db_wiki["wiki"])].append(QueuedWiki(db_wiki["wiki"], 20))
|
|
|
|
|
for group, db_wikis in domain_wikis.items():
|
|
|
|
|
yield group, db_wikis
|
|
|
|
|
|
|
|
|
@ -301,10 +310,10 @@ async def scan_group(group: str):
|
|
|
|
|
else:
|
|
|
|
|
local_wiki.rc_active = 0
|
|
|
|
|
DBHandler.add(queued_wiki.url, 0)
|
|
|
|
|
DBHandler.update_db()
|
|
|
|
|
await DBHandler.update_db()
|
|
|
|
|
continue
|
|
|
|
|
categorize_events = {}
|
|
|
|
|
targets = generate_targets(queued_wiki.url, "AND (rcid != -1 OR rcid IS NULL)")
|
|
|
|
|
targets = await generate_targets(queued_wiki.url, "AND (rcid != -1 OR rcid IS NULL)")
|
|
|
|
|
paths = get_paths(queued_wiki.url, recent_changes_resp)
|
|
|
|
|
new_events = 0
|
|
|
|
|
local_wiki.last_check = time.time() # on successful check, save new last check time
|
|
|
|
@ -347,7 +356,7 @@ async def scan_group(group: str):
|
|
|
|
|
if recent_changes: # we don't have to test for highest_rc being null, because if there are no RC entries recent_changes will be an empty list which will result in false in here and DO NOT save the value
|
|
|
|
|
local_wiki.rc_active = highest_rc
|
|
|
|
|
DBHandler.add(queued_wiki.url, highest_rc)
|
|
|
|
|
DBHandler.update_db()
|
|
|
|
|
await DBHandler.update_db()
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
return
|
|
|
|
|
except QueueEmpty:
|
|
|
|
@ -393,110 +402,109 @@ async def message_sender():
|
|
|
|
|
async def discussion_handler():
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
fetch_all = db_cursor.execute(
|
|
|
|
|
"SELECT wiki, rcid, postid FROM rcgcdw WHERE postid != '-1' OR postid IS NULL GROUP BY wiki")
|
|
|
|
|
for db_wiki in fetch_all.fetchall():
|
|
|
|
|
try:
|
|
|
|
|
local_wiki = all_wikis[db_wiki["wiki"]] # set a reference to a wiki object from memory
|
|
|
|
|
except KeyError:
|
|
|
|
|
local_wiki = all_wikis[db_wiki["wiki"]] = Wiki()
|
|
|
|
|
local_wiki.rc_active = db_wiki["rcid"]
|
|
|
|
|
if db_wiki["wiki"] not in rcqueue.irc_mapping["fandom.com"].updated_discussions and local_wiki.last_discussion_check+settings["irc_overtime"] > time.time(): # I swear if another wiki farm ever starts using Fandom discussions I'm gonna use explosion magic
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
rcqueue.irc_mapping["fandom.com"].updated_discussions.remove(db_wiki["wiki"])
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass # to be expected
|
|
|
|
|
header = settings["header"]
|
|
|
|
|
header["Accept"] = "application/hal+json"
|
|
|
|
|
async with aiohttp.ClientSession(headers=header,
|
|
|
|
|
timeout=aiohttp.ClientTimeout(6.0)) as session:
|
|
|
|
|
try:
|
|
|
|
|
feeds_response = await local_wiki.fetch_feeds(db_wiki["wiki"], session)
|
|
|
|
|
except (WikiServerError, WikiError):
|
|
|
|
|
continue # ignore this wiki if it throws errors
|
|
|
|
|
try:
|
|
|
|
|
discussion_feed_resp = await feeds_response.json(encoding="UTF-8")
|
|
|
|
|
if "error" in discussion_feed_resp:
|
|
|
|
|
error = discussion_feed_resp["error"]
|
|
|
|
|
if error == "NotFoundException": # 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:
|
|
|
|
|
await local_wiki.remove(db_wiki["wiki"], 1000)
|
|
|
|
|
DBHandler.update_db()
|
|
|
|
|
continue
|
|
|
|
|
raise WikiError
|
|
|
|
|
discussion_feed = discussion_feed_resp["_embedded"]["doc:posts"]
|
|
|
|
|
discussion_feed.reverse()
|
|
|
|
|
except aiohttp.ContentTypeError:
|
|
|
|
|
logger.exception("Wiki seems to be resulting in non-json content.")
|
|
|
|
|
continue
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
logger.debug("Timeout on reading JSON of discussion post feeed.")
|
|
|
|
|
continue
|
|
|
|
|
except:
|
|
|
|
|
logger.exception("On loading json of response.")
|
|
|
|
|
continue
|
|
|
|
|
if db_wiki["postid"] is None: # new wiki, just get the last post to not spam the channel
|
|
|
|
|
if len(discussion_feed) > 0:
|
|
|
|
|
DBHandler.add(db_wiki["wiki"], discussion_feed[-1]["id"], True)
|
|
|
|
|
else:
|
|
|
|
|
DBHandler.add(db_wiki["wiki"], "0", True)
|
|
|
|
|
DBHandler.update_db()
|
|
|
|
|
continue
|
|
|
|
|
comment_events = []
|
|
|
|
|
targets = generate_targets(db_wiki["wiki"], "AND NOT postid = '-1'")
|
|
|
|
|
for post in discussion_feed:
|
|
|
|
|
if post["_embedded"]["thread"][0]["containerType"] == "ARTICLE_COMMENT" and post["id"] > db_wiki["postid"]:
|
|
|
|
|
comment_events.append(post["forumId"])
|
|
|
|
|
comment_pages: dict = {}
|
|
|
|
|
if comment_events:
|
|
|
|
|
try:
|
|
|
|
|
comment_pages = await local_wiki.safe_request(
|
|
|
|
|
"{wiki}wikia.php?controller=FeedsAndPosts&method=getArticleNamesAndUsernames&stablePageIds={pages}&format=json".format(
|
|
|
|
|
wiki=db_wiki["wiki"], pages=",".join(comment_events)
|
|
|
|
|
), RateLimiter(), "articleNames")
|
|
|
|
|
except aiohttp.ClientResponseError: # Fandom can be funny sometimes... See #30
|
|
|
|
|
comment_pages = None
|
|
|
|
|
except:
|
|
|
|
|
if command_line_args.debug:
|
|
|
|
|
logger.exception("Exception on Feeds article comment request")
|
|
|
|
|
shutdown(loop=asyncio.get_event_loop())
|
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
|
async with connection.transaction():
|
|
|
|
|
async for db_wiki in connection.cursor("SELECT DISTINCT wiki, rcid, postid FROM rcgcdw WHERE postid != '-1' OR postid IS NULL"):
|
|
|
|
|
try:
|
|
|
|
|
local_wiki = all_wikis[db_wiki["wiki"]] # set a reference to a wiki object from memory
|
|
|
|
|
except KeyError:
|
|
|
|
|
local_wiki = all_wikis[db_wiki["wiki"]] = Wiki()
|
|
|
|
|
local_wiki.rc_active = db_wiki["rcid"]
|
|
|
|
|
if db_wiki["wiki"] not in rcqueue.irc_mapping["fandom.com"].updated_discussions and local_wiki.last_discussion_check+settings["irc_overtime"] > time.time(): # I swear if another wiki farm ever starts using Fandom discussions I'm gonna use explosion magic
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
logger.exception("Exception on Feeds article comment request")
|
|
|
|
|
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:
|
|
|
|
|
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
|
|
|
|
|
rcqueue.irc_mapping["fandom.com"].updated_discussions.remove(db_wiki["wiki"])
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass # to be expected
|
|
|
|
|
header = settings["header"]
|
|
|
|
|
header["Accept"] = "application/hal+json"
|
|
|
|
|
async with aiohttp.ClientSession(headers=header,
|
|
|
|
|
timeout=aiohttp.ClientTimeout(6.0)) as session:
|
|
|
|
|
try:
|
|
|
|
|
feeds_response = await local_wiki.fetch_feeds(db_wiki["wiki"], session)
|
|
|
|
|
except (WikiServerError, WikiError):
|
|
|
|
|
continue # ignore this wiki if it throws errors
|
|
|
|
|
try:
|
|
|
|
|
discussion_feed_resp = await feeds_response.json(encoding="UTF-8")
|
|
|
|
|
if "error" in discussion_feed_resp:
|
|
|
|
|
error = discussion_feed_resp["error"]
|
|
|
|
|
if error == "NotFoundException": # Discussions disabled
|
|
|
|
|
if db_wiki["rcid"] != -1: # RC feed is disabled
|
|
|
|
|
await connection.execute("UPDATE rcgcdw SET postid = $1 WHERE wiki = $2", "-1", db_wiki["wiki"])
|
|
|
|
|
else:
|
|
|
|
|
await local_wiki.remove(db_wiki["wiki"], 1000)
|
|
|
|
|
await DBHandler.update_db()
|
|
|
|
|
continue
|
|
|
|
|
raise WikiError
|
|
|
|
|
discussion_feed = discussion_feed_resp["_embedded"]["doc:posts"]
|
|
|
|
|
discussion_feed.reverse()
|
|
|
|
|
except aiohttp.ContentTypeError:
|
|
|
|
|
logger.exception("Wiki seems to be resulting in non-json content.")
|
|
|
|
|
continue
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
logger.debug("Timeout on reading JSON of discussion post feeed.")
|
|
|
|
|
continue
|
|
|
|
|
except:
|
|
|
|
|
logger.exception("On loading json of response.")
|
|
|
|
|
continue
|
|
|
|
|
if db_wiki["postid"] is None: # new wiki, just get the last post to not spam the channel
|
|
|
|
|
if len(discussion_feed) > 0:
|
|
|
|
|
DBHandler.add(db_wiki["wiki"], discussion_feed[-1]["id"], True)
|
|
|
|
|
else:
|
|
|
|
|
DBHandler.add(db_wiki["wiki"], "0", True)
|
|
|
|
|
await DBHandler.update_db()
|
|
|
|
|
continue
|
|
|
|
|
comment_events = []
|
|
|
|
|
targets = await generate_targets(db_wiki["wiki"], "AND NOT postid = '-1'")
|
|
|
|
|
for post in discussion_feed:
|
|
|
|
|
if post["_embedded"]["thread"][0]["containerType"] == "ARTICLE_COMMENT" and post["id"] > db_wiki["postid"]:
|
|
|
|
|
comment_events.append(post["forumId"])
|
|
|
|
|
comment_pages: dict = {}
|
|
|
|
|
if comment_events:
|
|
|
|
|
try:
|
|
|
|
|
comment_pages = await local_wiki.safe_request(
|
|
|
|
|
"{wiki}wikia.php?controller=FeedsAndPosts&method=getArticleNamesAndUsernames&stablePageIds={pages}&format=json".format(
|
|
|
|
|
wiki=db_wiki["wiki"], pages=",".join(comment_events)
|
|
|
|
|
), RateLimiter(), "articleNames")
|
|
|
|
|
except aiohttp.ClientResponseError: # Fandom can be funny sometimes... See #30
|
|
|
|
|
comment_pages = None
|
|
|
|
|
except:
|
|
|
|
|
if command_line_args.debug:
|
|
|
|
|
logger.exception("Exception on Feeds formatter")
|
|
|
|
|
logger.exception("Exception on Feeds article comment request")
|
|
|
|
|
shutdown(loop=asyncio.get_event_loop())
|
|
|
|
|
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"])
|
|
|
|
|
# Lets stack the messages
|
|
|
|
|
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
|
|
|
|
|
logger.exception("Exception on Feeds article comment request")
|
|
|
|
|
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:
|
|
|
|
|
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:
|
|
|
|
|
if command_line_args.debug:
|
|
|
|
|
logger.exception("Exception on Feeds formatter")
|
|
|
|
|
shutdown(loop=asyncio.get_event_loop())
|
|
|
|
|
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"])
|
|
|
|
|
# Lets stack the messages
|
|
|
|
|
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
|
|
|
|
|
await asyncio.sleep(delay=1.0) # Avoid lock on no wikis
|
|
|
|
|
DBHandler.update_db()
|
|
|
|
|
await DBHandler.update_db()
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
except:
|
|
|
|
@ -510,8 +518,6 @@ async def discussion_handler():
|
|
|
|
|
|
|
|
|
|
def shutdown(loop, signal=None):
|
|
|
|
|
global main_tasks
|
|
|
|
|
DBHandler.update_db()
|
|
|
|
|
db_connection.close()
|
|
|
|
|
loop.remove_signal_handler(signal)
|
|
|
|
|
if len(messagequeue) > 0:
|
|
|
|
|
logger.warning("Some messages are still queued!")
|
|
|
|
@ -543,6 +549,9 @@ async def main_loop():
|
|
|
|
|
global main_tasks
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
nest_asyncio.apply(loop)
|
|
|
|
|
await db.setup_connection()
|
|
|
|
|
logger.debug("Connection type: {}".format(db.connection))
|
|
|
|
|
await populate_allwikis()
|
|
|
|
|
try:
|
|
|
|
|
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
|
|
|
|
|
for s in signals:
|
|
|
|
@ -558,6 +567,8 @@ async def main_loop():
|
|
|
|
|
main_tasks["msg_queue_shield"] = asyncio.shield(main_tasks["message_sender"])
|
|
|
|
|
await asyncio.gather(main_tasks["wiki_scanner"], main_tasks["discussion_handler"], main_tasks["message_sender"])
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
await DBHandler.update_db()
|
|
|
|
|
await db.shutdown_connection()
|
|
|
|
|
shutdown(loop)
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
return
|
|
|
|
|