import os
from mwclient import Site
from pprint import pprint
from PIL import Image
from functions import update_json, remove_nonwords


site = Site(host='hub.xpub.nl/sandbox', path='/itchwiki/')
wd = os.path.dirname(os.path.abspath(__file__))  # working directory
parent_d = os.path.dirname(wd) # parent directory
imgdir = os.path.join(parent_d, 'archive/images')
os.makedirs(imgdir, exist_ok=True) # create images/ dir

imgsjson_fn = os.path.join(wd, 'images.json')

thumbnail_size = 1200 # largest px dimension of img thumbnails


with open(os.path.join(wd, 'login.txt'), 'r') as login:  # read login user & pwd
    loginlines = login.read()
    user, pwd = loginlines.split('\n')
    site.login(username=user, password=pwd)  # login to wiki

print(site)

for img in site.allimages():
    print(img)
    # print(img.__dict__)  # info contained in each img object
    # important img info to dictionary
    img_dict = {
        'name': img.name,
        'filename': remove_nonwords(img.page_title),
        'timestamp': img.imageinfo['timestamp'],
        'url': img.imageinfo['url'],
        'urldesc': img.imageinfo['descriptionshorturl'],
    }
    pprint(img_dict)

    # location of image storage
    img_fn = os.path.join(imgdir, img_dict['filename'])
    print(img_fn)
    # function updates images.json and returns whether the img needs to be downloaded or not
    download = update_json(imgsjson_fn, img_dict, img_fn)

    # image download
    if download is True:
        print('DOWNLOADING:', img_fn)
        with open(img_fn, 'wb') as img_file:
            img.download(destination=img_file)

        # resize image
        fn, ext = os.path.splitext(img_fn)
        if ext.lower() in ['.jpg', '.jpeg', '.gif', '.png']:  # only img format
            pilimg = Image.open(img_fn)
            pilimg_dim = list(pilimg._size)
            pilimg_dim_sort = sorted(pilimg_dim) # smallest dimension 1st
            img_ratio = pilimg_dim_sort[0] / pilimg_dim_sort[1]
            if pilimg_dim == pilimg_dim_sort:
                # if height was largest
                new_dim = [(thumbnail_size * img_ratio), thumbnail_size]
            else:
                # if with was largest
                new_dim = [thumbnail_size,(thumbnail_size * img_ratio)]
            pilimg.thumbnail(new_dim)
            pilimg.save(img_fn)
    print('\n')
