Remade embed upload events to reduce amount of requests

This commit is contained in:
Frisk 2021-05-08 13:21:05 +02:00
parent 86439a6570
commit 0083fbf4f6
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
2 changed files with 64 additions and 41 deletions

View file

@ -18,6 +18,7 @@ import math
import re import re
import time import time
import datetime import datetime
from collections import OrderedDict
from src.discord.message import DiscordMessage from src.discord.message import DiscordMessage
from src.api import formatter from src.api import formatter
from src.i18n import rc_formatters from src.i18n import rc_formatters
@ -125,17 +126,43 @@ def embed_upload_upload(ctx, change) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
action = ctx.event action = ctx.event
embed_helper(ctx, embed, change) embed_helper(ctx, embed, change)
urls = ctx.client.make_api_request("{wiki}?action=query&format=json&prop=imageinfo&list=&meta=&titles={filename}&iiprop=timestamp%7Curl%7Carchivename&iilimit=5".format( # Requesting more information on the image
wiki=ctx.WIKI_API_PATH, filename=sanitize_to_url(change["title"])), "query", "pages") request_for_image_data = None
try:
params = OrderedDict()
params["action"] = "query"
params["format"] = "json"
if settings["license_detection"] and action == "upload/upload":
params["prop"] = "imageinfo|revisions"
params["rvprop"] = "content"
params["rvslots"] = "main"
else:
params["prop"] = "imageinfo"
params["title"] = sanitize_to_url(change["title"])
params["iiprop"] = "timestamp%7Curl%7Carchivename"
params["iilimit"] = "5"
request_for_image_data = ctx.client.make_api_request(params, "query", "pages")
except (ServerError, MediaWikiError):
logger.exception("Couldn't retrieve more information about the image {} because of server/MediaWiki error".format(change["title"]))
except (ClientError, BadRequest):
raise
except KeyError:
logger.exception(
"Couldn't retrieve more information about the image {} because of unknown error".format(
change["title"]))
else:
if "-1" not in request_for_image_data: # Image still exists and not removed
image_data = next(iter(request_for_image_data.values()))
else:
logger.warning("Request for additional image information have failed. The preview will not be shown.")
request_for_image_data = None
link = create_article_path(sanitize_to_url(change["title"])) link = create_article_path(sanitize_to_url(change["title"]))
image_direct_url = None image_direct_url = None
# Make a request for file revisions so we can get direct URL to the image for embed # Make a request for file revisions so we can get direct URL to the image for embed
if urls is not None: if request_for_image_data is not None:
logger.debug(urls)
if "-1" not in urls: # image still exists and not removed
try: try:
img_info = next(iter(urls.values()))["imageinfo"] urls = image_data["imageinfo"]
for num, revision in enumerate(img_info): for num, revision in enumerate(urls):
if revision["timestamp"] == change["logparams"]["img_timestamp"]: # find the correct revision corresponding for this log entry if revision["timestamp"] == change["logparams"]["img_timestamp"]: # find the correct revision corresponding for this log entry
image_direct_url = "{rev}?{cache}".format(rev=revision["url"], cache=int(time.time() * 5)) # cachebusting image_direct_url = "{rev}?{cache}".format(rev=revision["url"], cache=int(time.time() * 5)) # cachebusting
break break
@ -147,11 +174,11 @@ def embed_upload_upload(ctx, change) -> DiscordMessage:
if action in ("upload/overwrite", "upload/revert"): if action in ("upload/overwrite", "upload/revert"):
if image_direct_url: if image_direct_url:
try: try:
revision = img_info[num + 1] revision = image_data["imageinfo"][num + 1]
except IndexError: except IndexError:
logger.exception( logger.exception(
"Could not analize the information about the image (does it have only one version when expected more in overwrite?) which resulted in no Options field: {}".format( "Could not analize the information about the image (does it have only one version when expected more in overwrite?) which resulted in no Options field: {}".format(
img_info)) image_data["imageinfo"]))
else: else:
undolink = "{wiki}index.php?title={filename}&action=revert&oldimage={archiveid}".format( undolink = "{wiki}index.php?title={filename}&action=revert&oldimage={archiveid}".format(
wiki=ctx.client.WIKI_SCRIPT_PATH, filename=sanitize_to_url(change["title"]), archiveid=revision["archivename"]) wiki=ctx.client.WIKI_SCRIPT_PATH, filename=sanitize_to_url(change["title"]), archiveid=revision["archivename"])
@ -166,15 +193,8 @@ def embed_upload_upload(ctx, change) -> DiscordMessage:
else: else:
embed["title"] = _("Uploaded {name}").format(name=change["title"]) embed["title"] = _("Uploaded {name}").format(name=change["title"])
if settings["license_detection"]: if settings["license_detection"]:
article_content = ctx.client.make_api_request(
"{wiki}?action=query&format=json&prop=revisions&titles={article}&rvprop=content".format(
wiki=ctx.client.WIKI_API_PATH, article=sanitize_to_url(change["title"])), "query", "pages")
if article_content is None:
logger.warning("Something went wrong when getting license for the image")
return 0
if "-1" not in article_content:
content = list(article_content.values())[0]['revisions'][0]['*']
try: try:
content = image_data['revisions'][0]["slots"]["main"]['*']
matches = re.search(re.compile(settings["license_regex"], re.IGNORECASE), content) matches = re.search(re.compile(settings["license_regex"], re.IGNORECASE), content)
if matches is not None: if matches is not None:
license = matches.group("license") license = matches.group("license")
@ -191,8 +211,10 @@ def embed_upload_upload(ctx, change) -> DiscordMessage:
logger.error( logger.error(
"Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config!") "Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config!")
license = "?" license = "?"
except KeyError:
logger.exception("Unknown error when retriefing the image data for a license, full content: {}".format(image_data))
if license is not None: if license is not None:
ctx.parsedcomment += _("\nLicense: {}").format(license) embed["description"] += _("\nLicense: {}").format(license)
if image_direct_url: if image_direct_url:
embed.add_field(_("Options"), _("([preview]({link}))").format(link=image_direct_url)) embed.add_field(_("Options"), _("([preview]({link}))").format(link=image_direct_url))
if settings["appearance"]["embed"]["embed_images"]: if settings["appearance"]["embed"]["embed_images"]:

View file

@ -340,6 +340,7 @@ class Wiki(object):
logger.exception("MediaWiki error on request: {}".format(request.url)) logger.exception("MediaWiki error on request: {}".format(request.url))
raise raise
except KeyError: except KeyError:
logger.exception("KeyError while iterating over json_path, full response: {}".format(request.json()))
raise raise
return request_json return request_json