Fixed a thing

This commit is contained in:
Frisk 2018-07-01 20:15:48 +02:00
parent e3ffeb5e13
commit 20182ecb81
No known key found for this signature in database
GPG key ID: 0E9A7D3C0A01586C

View file

@ -102,43 +102,50 @@ def webhook_formatter(action, STATIC, **params):
link = "https://{wiki}.gamepedia.com/index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format(wiki=settings["wiki"], pageid=params["pageid"], diff=params["diff"], oldrev=params["oldrev"], article=article_encoded) link = "https://{wiki}.gamepedia.com/index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format(wiki=settings["wiki"], pageid=params["pageid"], diff=params["diff"], oldrev=params["oldrev"], article=article_encoded)
embed["title"] = "{article} ({new}{minor}{editsize})".format(article=params["title"], editsize="+"+str(editsize) if editsize>0 else editsize, new= _("(N!) ") if action == 37 else "", minor=_("m ") if action == 1 and params["minor"] else "") embed["title"] = "{article} ({new}{minor}{editsize})".format(article=params["title"], editsize="+"+str(editsize) if editsize>0 else editsize, new= _("(N!) ") if action == 37 else "", minor=_("m ") if action == 1 and params["minor"] else "")
elif action == 5: #sending files elif action == 5: #sending files
license = None
urls = safe_read(recent_changes.safe_request("https://{wiki}.gamepedia.com/api.php?action=query&format=json&prop=imageinfo&list=&meta=&titles={filename}&iiprop=timestamp%7Curl&iilimit=2".format(wiki=settings["wiki"], filename=params["title"])), "query", "pages") urls = safe_read(recent_changes.safe_request("https://{wiki}.gamepedia.com/api.php?action=query&format=json&prop=imageinfo&list=&meta=&titles={filename}&iiprop=timestamp%7Curl&iilimit=2".format(wiki=settings["wiki"], filename=params["title"])), "query", "pages")
undolink = "" undolink = ""
link ="https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], article=article_encoded) link ="https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], article=article_encoded)
additional_info_retrieved = False
if urls is not None: if urls is not None:
img_info = next(iter(urls.values()))["imageinfo"] if "-1" not in urls: #oage removed before we asked for it
embed["image"]["url"] = img_info[0]["url"] img_info = next(iter(urls.values()))["imageinfo"]
embed["image"]["url"] = img_info[0]["url"]
additional_info_retrieved = True
else: else:
return pass
if params["overwrite"]: if params["overwrite"]:
img_timestamp = [x for x in img_info[1]["timestamp"] if x.isdigit()] if additional_info_retrieved:
undolink = "https://{wiki}.gamepedia.com/index.php?title={filename}&action=revert&oldimage={timestamp}%21{filenamewon}".format(wiki=settings["wiki"], filename=article_encoded, timestamp="".join(img_timestamp), filenamewon = article_encoded[5:]) img_timestamp = [x for x in img_info[1]["timestamp"] if x.isdigit()]
undolink = "https://{wiki}.gamepedia.com/index.php?title={filename}&action=revert&oldimage={timestamp}%21{filenamewon}".format(wiki=settings["wiki"], filename=article_encoded, timestamp="".join(img_timestamp), filenamewon = article_encoded[5:])
embed["fields"] = [{"name": _("Options"), "value": _("([preview]({link}) | [undo]({undolink}))").format(link=embed["image"]["url"], undolink=undolink)}]
embed["title"] = _("Uploaded a new version of {name}").format(name=params["title"]) embed["title"] = _("Uploaded a new version of {name}").format(name=params["title"])
embed["fields"] = [{"name": _("Options"), "value": _("([preview]({link}) | [undo]({undolink}))").format(link=embed["image"]["url"], undolink=undolink)}]
else: else:
embed["title"] = _("Uploaded {name}").format(name=params["title"]) embed["title"] = _("Uploaded {name}").format(name=params["title"])
article_content = safe_read(recent_changes.safe_request("https://{wiki}.gamepedia.com/api.php?action=query&format=json&prop=revisions&titles={article}&rvprop=content".format(wiki=settings["wiki"], article=quote_plus(params["title"], safe=''))), "query", "pages") article_content = safe_read(recent_changes.safe_request("https://{wiki}.gamepedia.com/api.php?action=query&format=json&prop=revisions&titles={article}&rvprop=content".format(wiki=settings["wiki"], article=quote_plus(params["title"], safe=''))), "query", "pages")
if article_content is None: if article_content is None:
logging.warning("Something went wrong when getting license for the image") logging.warning("Something went wrong when getting license for the image")
return 0 return 0
content = list(article_content.values())[0]['revisions'][0]['*'] if "-1" not in article_content:
try: content = list(article_content.values())[0]['revisions'][0]['*']
matches = re.search(re.compile(settings["license_regex"], re.IGNORECASE), content) try:
if matches is not None: matches = re.search(re.compile(settings["license_regex"], re.IGNORECASE), content)
license = matches.group("license") if matches is not None:
else: license = matches.group("license")
if re.search(re.compile(settings["license_regex_detect"], re.IGNORECASE), content) is None:
license = _("**No license!**")
else: else:
license = "?" if re.search(re.compile(settings["license_regex_detect"], re.IGNORECASE), content) is None:
except IndexError: license = _("**No license!**")
logging.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!") else:
license = "?" license = "?"
except re.error: except IndexError:
logging.error("Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config!") logging.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!")
license = "?" license = "?"
embed["fields"] = [{"name": _("Options"), "value": _("([preview]({link}))").format(link=embed["image"]["url"])}] except re.error:
params["desc"] = _("{desc}\nLicense: {license}").format(desc=params["desc"], license=license) logging.error("Given regex for the license detection is incorrect. Please fix license_regex or license_regex_detect values in the config!")
license = "?"
if additional_info_retrieved:
embed["fields"] = [{"name": _("Options"), "value": _("([preview]({link}))").format(link=embed["image"]["url"])}]
params["desc"] = _("{desc}\nLicense: {license}").format(desc=params["desc"], license=license if license is not None else "?")
elif action == 6: elif action == 6:
link = "https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], article=params["title"].replace(" ", "_")) link = "https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], article=params["title"].replace(" ", "_"))
embed["title"] = _("Deleted page {article}").format(article=params["title"]) embed["title"] = _("Deleted page {article}").format(article=params["title"])