You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
special-issue-11-wiki2html/download_imgs.py

47 lines
1.4 KiB
Python

import os
from mwclient import Site
from pprint import pprint
from functions import update_json
site = Site(host='hub.xpub.nl/sandbox', path='/itchwiki/')
wd = os.path.dirname(os.path.abspath(__file__)) # working directory
imgdir = os.path.join(wd, 'images')
os.makedirs(imgdir, exist_ok=True) # create images/ dir
imgsjson_fn = os.path.join(wd, 'images.json')
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': 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'])
# 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)
print('\n')