wikidata: load thumbnail instead of full image

This commit is contained in:
Léon Tiekötter 2022-02-06 23:25:50 +01:00
parent 560a14e77b
commit a50f32bcfc
No known key found for this signature in database
GPG Key ID: 02869F7817D9EC08
1 changed files with 15 additions and 2 deletions

View File

@ -187,7 +187,7 @@ def response(resp):
_IMG_SRC_DEFAULT_URL_PREFIX = "https://commons.wikimedia.org/wiki/Special:FilePath/"
_IMG_SRC_NEW_URL_PREFIX = "https://upload.wikimedia.org/wikipedia/commons/"
_IMG_SRC_NEW_URL_PREFIX = "https://upload.wikimedia.org/wikipedia/commons/thumb/"
def get_thumbnail(img_src):
@ -209,8 +209,21 @@ def get_thumbnail(img_src):
.replace("%28", "(")
.replace("%29", ")")
)
img_src_size = img_src.replace(_IMG_SRC_DEFAULT_URL_PREFIX, "").split("?", 1)[1]
img_src_size = img_src_size[img_src_size.index("=") + 1 : img_src_size.index("&")]
img_src_name_md5 = md5(img_src_name.encode("utf-8")).hexdigest()
img_src = _IMG_SRC_NEW_URL_PREFIX + img_src_name_md5[0] + "/" + img_src_name_md5[0:2] + "/" + img_src_name
img_src = (
_IMG_SRC_NEW_URL_PREFIX
+ img_src_name_md5[0]
+ "/"
+ img_src_name_md5[0:2]
+ "/"
+ img_src_name
+ "/"
+ img_src_size
+ "px-"
+ img_src_name
)
logger.debug('get_thumbnail() redirected: %s', img_src)
return img_src