import os, argparse, sys, re, json
from mwclient import (Site,
                      errors)

# API CALL
# https://hub.xpub.nl/sandbox/itchwiki/api.php?action=smwbrowse&browse=pvalue&params={ "limit": 1500, "offset": 0, "property" : "Title", "search": "" }&format=json
# generated orgs.json
#             >>> result = site.api('query', prop='coordinates', titles='Oslo|Copenhagen')
# login
site = Site(host='hub.xpub.nl/sandbox', path='/itchwiki/')

wd =os.path.dirname(os.path.abspath(__file__))  # parent working directory
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

# To query a large number of ite:

for i in range(0, 1500, 100):
    # variable  i will increase 100 at each iteration
    # between 0 and 1400
    # and will make the offset parameter change
    print('\n', f'Querying from {i} to {i+100}', '\n')

    ask_query = f'[[Category:Title]]|format=json|limit=100|offset={i}'

    response = site.api(action='ask', query=ask_query)
    for pagetitle in response['query']['results']:
        print(pagetitle)
        page = site.pages[pagetitle]
#         # text = page.text()
        page.save('{{Publication}}\n[[Category:Title]]')
