3

I want to run pywikibot from inside Docker container, so I could run some cron jobs with it from the cloud (maybe Azure).

I added code of my bot and user-config.py file to my Docker container, but when it tries to update some page, it uses getpass to read password from input:

Password for user BunykBot on wikipedia:uk (no characters will be shown): ^CWARNING: /usr/local/lib/python3.7/getpass.py:91: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)

Is there any way to give it password from some variable? I see that login.py script that creates .lwp file uses site.login() which uses api.LoginManager, but not gives it password anywhere, so it obtains it from input. Is there any way I could monkeypatch this with not much effort? Or do I need some updated fork of pywikibot?

Bunyk
  • 7,635
  • 8
  • 47
  • 79

4 Answers4

1

So, I decided to go with monkey patching, and just created file that needs to be imported before pywikibot:

import os

def patch_wiki():
    import pywikibot

    original = pywikibot.input

    def new_input(question, password=False, default='', force=False):
        if password:
            return os.getenv('WIKI_PASS')
        return original(question, password, default, force)

    pywikibot.input = new_input

if os.getenv('WIKI_PASS'):
    patch_wiki()

Which works for me.

Bunyk
  • 7,635
  • 8
  • 47
  • 79
0

There are better options, like using BotPasswords or OAuth, see more in https://lists.wikimedia.org/pipermail/pywikibot/2019-December/009968.html

aleskva
  • 1,644
  • 2
  • 21
  • 40
0

see https://phabricator.wikimedia.org/T248471 for a proposed patch to make password use from API much easier

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
0

Use OAuth tokens.

First, follow the official docs to get your tokens.

Then, edit user-config.py to use environment variables:

# …
import os

usernames['wikipedia']['*'] = \
    usernames['meta']['*'] = \
    usernames['commons']['*'] = \
    usernames['wikidata']['*'] = \
    usernames['wiktionary']['*'] = \
    usernames['wikibooks']['*'] = \
    usernames['wikinews']['*'] = \
    usernames['wikiquote']['*'] = \
    usernames['wikisource']['*'] = \
    usernames['wikiversity']['*'] = \
    usernames['wikivoyage']['*'] = \
    os.environ["WP_USERNAME"]

authenticate['*.wikipedia.org'] = \
    authenticate['*.wikimedia.org'] = \
    authenticate['*.wikidata.org'] = \
    authenticate['*.wiktionary.org'] = \
    authenticate['*.wikibooks.org'] = \
    authenticate['*.wikinews.org'] = \
    authenticate['*.wikiquote.org'] = \
    authenticate['*.wikisource.org'] = \
    authenticate['*.wikiversity.org'] = \
    authenticate['*.wikivoyage.org'] = \
    authenticate['*.mediawiki.org'] = \
    tuple(os.environ["WP_OAUTH_KEYS"].split(","))

You can tweak this as you want. With the code above, you have to set the following environment variables:

  • WP_USERNAME: your bot's username
  • WP_OAUTH_KEYS: a comma-separated list of your OAuth keys: <consumer token>,<consumer secret>,<access token>,<access secret>

You can also edit other parts of the user-config.py file to use os.environ["…"] instead of hardcoded values.

bfontaine
  • 18,169
  • 13
  • 73
  • 107