User:OmegaK2/python

From Path of Exile Wiki
Jump to navigation Jump to search

Python stuff for the wiki

Misc

import mwclient
site = mwclient.Site('pathofexile.gamepedia.com', path='/', scheme='https')
site.login('Username', 'Password')
import hashlib
from PIL import Image
from tempfile import TemporaryDirectory

def upload_image(fn, path, description):
    im = site.Images[fn]
    if not im.exists:
        with open(os.path.join(path, fn), 'rb') as f:
            r = site.upload(file=f, filename=fn, description=description)
        return r
    
    sha_new = hashlib.sha1(Image.open(os.path.join(path, fn)).tobytes()).hexdigest()
    
    with TemporaryDirectory() as tmp:
        with open(os.path.join(tmp, fn), 'wb') as f:
            im.download(destination=f)
        sha_old = hashlib.sha1(Image.open(os.path.join(tmp, fn)).tobytes()).hexdigest()
        
    if sha_new == sha_old:
        return None
        
    with open(os.path.join(path, fn), 'rb') as f:
        r = site.upload(file=f, filename=fn, description=description, ignore=True)
        return r