mirror of
https://gitlab.com/chicken-riders/RcGcDw.git
synced 2025-02-23 00:24:09 +00:00
Remade embed upload events to reduce amount of requests
This commit is contained in:
parent
86439a6570
commit
0083fbf4f6
|
@ -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,33 +126,59 @@ 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)
|
try:
|
||||||
if "-1" not in urls: # image still exists and not removed
|
urls = image_data["imageinfo"]
|
||||||
try:
|
for num, revision in enumerate(urls):
|
||||||
img_info = next(iter(urls.values()))["imageinfo"]
|
if revision["timestamp"] == change["logparams"]["img_timestamp"]: # find the correct revision corresponding for this log entry
|
||||||
for num, revision in enumerate(img_info):
|
image_direct_url = "{rev}?{cache}".format(rev=revision["url"], cache=int(time.time() * 5)) # cachebusting
|
||||||
if revision["timestamp"] == change["logparams"]["img_timestamp"]: # find the correct revision corresponding for this log entry
|
break
|
||||||
image_direct_url = "{rev}?{cache}".format(rev=revision["url"], cache=int(time.time() * 5)) # cachebusting
|
except KeyError:
|
||||||
break
|
logger.exception(
|
||||||
except KeyError:
|
"Wiki did not respond with extended information about file. The preview will not be shown.")
|
||||||
logger.exception(
|
|
||||||
"Wiki did not respond with extended information about file. The preview will not be shown.")
|
|
||||||
else:
|
else:
|
||||||
logger.warning("Request for additional image information have failed. The preview will not be shown.")
|
logger.warning("Request for additional image information have failed. The preview will not be shown.")
|
||||||
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,33 +193,28 @@ 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(
|
try:
|
||||||
"{wiki}?action=query&format=json&prop=revisions&titles={article}&rvprop=content".format(
|
content = image_data['revisions'][0]["slots"]["main"]['*']
|
||||||
wiki=ctx.client.WIKI_API_PATH, article=sanitize_to_url(change["title"])), "query", "pages")
|
matches = re.search(re.compile(settings["license_regex"], re.IGNORECASE), content)
|
||||||
if article_content is None:
|
if matches is not None:
|
||||||
logger.warning("Something went wrong when getting license for the image")
|
license = matches.group("license")
|
||||||
return 0
|
else:
|
||||||
if "-1" not in article_content:
|
if re.search(re.compile(settings["license_regex_detect"], re.IGNORECASE), content) is None:
|
||||||
content = list(article_content.values())[0]['revisions'][0]['*']
|
license = _("**No license!**")
|
||||||
try:
|
|
||||||
matches = re.search(re.compile(settings["license_regex"], re.IGNORECASE), content)
|
|
||||||
if matches is not None:
|
|
||||||
license = matches.group("license")
|
|
||||||
else:
|
else:
|
||||||
if re.search(re.compile(settings["license_regex_detect"], re.IGNORECASE), content) is None:
|
license = "?"
|
||||||
license = _("**No license!**")
|
except IndexError:
|
||||||
else:
|
logger.error(
|
||||||
license = "?"
|
"Given regex for the license detection is incorrect. It does not have a capturing group called \"license\" specified. Please fix license_regex value in the config!")
|
||||||
except IndexError:
|
license = "?"
|
||||||
logger.error(
|
except re.error:
|
||||||
"Given regex for the license detection is incorrect. It does not have a capturing group called \"license\" specified. Please fix license_regex value in the config!")
|
logger.error(
|
||||||
license = "?"
|
"Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config!")
|
||||||
except re.error:
|
license = "?"
|
||||||
logger.error(
|
except KeyError:
|
||||||
"Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config!")
|
logger.exception("Unknown error when retriefing the image data for a license, full content: {}".format(image_data))
|
||||||
license = "?"
|
|
||||||
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"]:
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue