from app import *
from time import sleep
import pywikibot

def run_bot(template_param, access_token=None, site=None, max_edits=100000):
    cached_pages = list_cache_contents('bot_cache/')
    edits_made = 0
    for page_name in cached_pages:
        print(page_name)
        cache_fname = 'bot_cache/'+to_cache_name(page_name)
        with open(cache_fname, 'r') as f:
            page_json = json.load(f)
        if run_bot_on_page(page_json, template_param, access_token=access_token, site=site):
            edits_made += 1
            sleep(3)
        if edits_made >= max_edits:
            return

def run_bot_on_page(proposed_edits, template_param, access_token=None, site=None):
    page_name = proposed_edits['page_name']

    for edit in proposed_edits['proposed_edits']:
        edit_hash = edit['orig_hash']
        change = edit['proposed_change']
        match = re.findall(r'^' + template_param, change)
        if match:
            try:
                app.logger.info('Attempting change on {}: {}'.format(page_name, change))
                change_made = perform_bot_edit(page_name, '[[Wikipedia:OABOT|Open access bot]]: add %s identifier to citation with #oabot.' % match[0], edit_hash, change, access_token=access_token, site=site)
                if change_made:
                    return True
            except ValueError:
                app.logger.exception('perform_bot_edit failed on {}'.format(page_name))
    return False

def perform_bot_edit(page_name, summary, template_hash, proposed_addition, access_token=None, site=None):
     # Get the page
    text = main.get_page_over_api(page_name)

    # Perform each edit
    new_text, change_made = make_new_wikicode_for_bot(text, template_hash, proposed_addition, page_name)

    # Save the page
    if change_made:
        if site:
            page = pywikibot.Page(site, page_name)
            page.text = new_text
            page.save(summary)
        else:
            edit_wiki_page(page_name, new_text, access_token, summary, bot='yes')


        # Remove the cache
        cache_fname = "bot_cache/"+to_cache_name(page_name)
        if os.path.isfile(cache_fname):
            os.remove(cache_fname)

    return change_made

def make_new_wikicode_for_bot(text, template_hash, proposed_addition, page_name):
    wikicode = mwparserfromhell.parse(text)
    change_made = False
    for template in wikicode.filter_templates():
        edit = main.TemplateEdit(template, page_name)
        if edit.orig_hash == template_hash:
            try:
                edit.update_template(proposed_addition)
                change_made = True
            except ValueError:
                app.logger.exception('update_template failed on {}'.format(page_name))
                pass # TODO report to the user
    return unicode(wikicode), change_made


if __name__ == '__main__':
    import sys
    template_param = sys.argv[1]
    app.logger.info("Starting additions for parameter: {}".format(template_param))
    site = pywikibot.Site()
    site.login()
    run_bot(template_param, site=site)

Add bot.py to run as an actual bot, via pywikibot