jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Retry upload if 'copyuploadbaddomain' API error occurs

- if 'copyuploadbaddomain' API error occurs try to download the
file first and upload it afterwards
- remove pywikibot.data.api import in specialbots/_upload.py
which isn't used
- fix APIError exception which was moved to exceptions module

Bug: T294825
Change-Id: Ib95c829110b30396d6e5bb2e9259e510fc32abbd
---
M pywikibot/site/_apisite.py
M pywikibot/specialbots/_upload.py
2 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 4d11d71..f5607c9 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -2850,7 +2850,7 @@
try:
result = final_request.submit()
self._uploaddisabled = False
- except api.APIError as error:
+ except APIError as error:
# TODO: catch and process foreseeable errors
if error.code == 'uploaddisabled':
self._uploaddisabled = True
diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index e3cf7e9..1aff167 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -20,7 +20,7 @@

import pywikibot
import pywikibot.comms.http as http
-import pywikibot.data.api
+
from pywikibot import config
from pywikibot.backports import List
from pywikibot.bot import BaseBot, QuitKeyboardInterrupt
@@ -383,6 +383,10 @@
Return the filename that was used to upload the image.
If the upload fails, ask the user whether to try again or not.
If the user chooses not to retry, return None.
+
+ .. versionchanged:: 7.0
+ If 'copyuploadbaddomain' API error occurred in first step,
+ download the file and upload it afterwards
"""
filename = self.process_filename(file_url)
if not filename:
@@ -395,35 +399,50 @@
pywikibot.output('Uploading file to {}...'.format(site))

ignore_warnings = self.ignore_warning is True or self._handle_warnings
- if '://' in file_url and not site.has_right('upload_by_url'):
- try:
- file_url = self.read_file_content(file_url)
- except FatalServerError:
- return None

- try:
- success = imagepage.upload(file_url,
- ignore_warnings=ignore_warnings,
- chunk_size=self.chunk_size,
- _file_key=_file_key, _offset=_offset,
- asynchronous=self.asynchronous,
- comment=self.summary)
- except APIError as error:
- if error.code == 'uploaddisabled':
- pywikibot.error(
- 'Upload error: Local file uploads are disabled on {}.'
- .format(site))
- else:
+ download = False
+ while True:
+ if '://' in file_url \
+ and (not site.has_right('upload_by_url') or download):
+ try:
+ file_url = self.read_file_content(file_url)
+ except FatalServerError:
+ pywikibot.exception()
+ return None
+
+ try:
+ success = imagepage.upload(file_url,
+ ignore_warnings=ignore_warnings,
+ chunk_size=self.chunk_size,
+ _file_key=_file_key,
+ _offset=_offset,
+ asynchronous=self.asynchronous,
+ comment=self.summary)
+ except APIError as error:
+ if error.code == 'uploaddisabled':
+ pywikibot.error(
+ 'Upload error: Local file uploads are disabled on {}.'
+ .format(site))
+ elif error.code == 'copyuploadbaddomain' and not download \
+ and '://' in file_url:
+ pywikibot.exception()
+ pywikibot.output('Downloading the file and retry...')
+ download = True
+ continue
+ else:
+ pywikibot.error('Upload error: ', exc_info=True)
+ except Exception:
pywikibot.error('Upload error: ', exc_info=True)
- except Exception:
- pywikibot.error('Upload error: ', exc_info=True)
- else:
- if success:
- # No warning, upload complete.
- pywikibot.output('Upload of {} successful.'.format(filename))
- self._save_counter += 1
- return filename # data['filename']
- pywikibot.output('Upload aborted.')
+ else:
+ if success:
+ # No warning, upload complete.
+ pywikibot.output('Upload of {} successful.'
+ .format(filename))
+ self._save_counter += 1
+ return filename # data['filename']
+ pywikibot.output('Upload aborted.')
+ break
+
return None

def skip_run(self):

To view, visit change 736417. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib95c829110b30396d6e5bb2e9259e510fc32abbd
Gerrit-Change-Number: 736417
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Inductiveload <inductiveload@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged