[Python] specialbots.UploadRobot().run(): looks like I cannot catch an exception?

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    In my script, I want to catch the case when user uploading a file to Commons that already exists.

    My logs:

    Uploading file to commons:commons...
    WARNING: API error fileexists-no-change: The upload is an exact duplicate of the current version of [[:File:005a 309 Пацевичи, 21-05-2004.jpg]].
    ERROR: Upload error:
    Traceback (most recent call last):
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/specialbots/_upload.py", line 422, in upload_file
    success = imagepage.upload(file_url,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/page/_filepage.py", line 325, in upload
    return self.site.upload(self, source_filename=filename, source_url=url,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/site/_decorators.py", line 93, in callee
    return fn(self, *args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/site/_apisite.py", line 3029, in upload
    return Uploader(self, filepage, **kwargs).upload()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/site/_upload.py", line 134, in upload
    return self._upload(self.ignore_warnings, self.report_success)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/site/_upload.py", line 435, in _upload
    return self.submit(final_request, result, data.get('result'),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/site/_upload.py", line 455, in submit
    result = request.submit()['upload']
    ^^^^^^^^^^^^^^^^
    File "/home/vitaly/p/upload_to_commons_with_categories_from_iptc/myenv/lib/python3.12/site-packages/pywikibot/data/api/_requests.py", line 1144, in submit
    raise pywikibot.exceptions.APIError(**error)
    pywikibot.exceptions.APIError: fileexists-no-change: The upload is an exact duplicate of the current version of [[:File:005a 309 Пацевичи, 21-05-2004.jpg]].
    [stasherrors: [{'message': 'uploadstash-exception', 'params': ['UploadStashBadPathException', "Path doesn't exist."], 'code': 'uploadstash-exception', 'type': 'error'}];
    servedby: mw-api-ext.eqiad.main-64bb4cbfc5-c2h2c;
    help: See https://commons.wikimedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/&gt
    ; for notice of API deprecations and breaking changes.]
    1 read operation
    Script terminated successfully.


    I tried try/except:

    bot = UploadRobot(
    sys.argv[1],
    description=description,
    keep_filename=True,
    ignore_warning=True,
    target_site=pywikibot.Site('commons', 'commons'),
    summary=summary,
    always=True
    )

    try:
    bot.run()
    # except pywikibot.exceptions.APIError as e:
    # sys.exit('ERROR from Wikimedia API')
    except Exception as e:
    print('xxxxxxxxxxxxxx')


    but exception is not caught.

    Also tried post_processor:

    def post(file_url, filename):
    if filename is None:
    # upload failed (duplicate or other error)
    print(f"⚠️ Skipped {file_url!r}: already exists or error")
    else:
    print(f"✅ Uploaded as {filename!r}")

    bot = UploadRobot(
    sys.argv[1],
    description=description,
    keep_filename=True,
    ignore_warning=True,
    target_site=pywikibot.Site('commons', 'commons'),
    summary=summary,
    always=True,
    post_processor=post
    )


    but also nothing printed from my post()

    Related documentation https://doc.wikimedia.org/pywikibot....specialbots.html#specialbots.UploadRobot.run

    Continue reading...

Compartilhe esta Página