jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/402591 )

Change subject: imagecopy: Convert to use pywikibot.comms.http.fetch
......................................................................


imagecopy: Convert to use pywikibot.comms.http.fetch

Convert scripts/imagecopy.py to use
`pywikibot.comms.http.fetch()` instead of `urllib`.

Bug: T130523
Change-Id: I278081b24ff265a43c37372cd26f6fbae1c3b44e
---
M scripts/imagecopy.py
A tests/data/commonsHelper_description.txt
A tests/imagecopy_tests.py
3 files changed, 89 insertions(+), 17 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/scripts/imagecopy.py b/scripts/imagecopy.py
index 9424c52..7a539b5 100644
--- a/scripts/imagecopy.py
+++ b/scripts/imagecopy.py
@@ -62,24 +62,20 @@
 
 import codecs
 import re
-import socket
 import threading
 import webbrowser
 
 import pywikibot
 
+from requests.exceptions import RequestException
+
 from pywikibot import pagegenerators, config, i18n
 
+from pywikibot.comms.http import fetch
+
 from pywikibot.specialbots import UploadRobot
-from pywikibot.tools import PY2
 
 from scripts import image
-
-if not PY2:
-    from urllib.parse import urlencode
-    from urllib.request import urlopen
-else:
-    from urllib import urlencode, urlopen
 
 try:
     from pywikibot.userinterfaces.gui import Tkdialog, Tkinter
@@ -201,18 +197,27 @@
 
 
 def pageTextPost(url, parameters):
-    """Get data from commons helper page."""
+    """
+    Get data from commons helper page.
+
+    @param url: This parameter is not used here, we keep it here to avoid user
+                scripts from breaking.
+    @param parameters: Data that will be submitted to CommonsHelper.
+    @type parameters: dict
+    @return: A CommonHelper description message.
+    @rtype: str
+    """
     gotInfo = False
     while not gotInfo:
         try:
-            commonsHelperPage = urlopen(
-                "http://tools.wmflabs.org/commonshelper/index.php";, parameters)
-            data = commonsHelperPage.read().decode('utf-8')
+            commonsHelperPage = fetch(
+                'https://tools.wmflabs.org/commonshelper/',
+                method='POST',
+                data=parameters)
+            data = commonsHelperPage.data.content.decode('utf-8')
             gotInfo = True
-        except IOError:
-            pywikibot.output(u'Got an IOError, let\'s try again')
-        except socket.timeout:
-            pywikibot.output(u'Got a timeout, let\'s try again')
+        except RequestException:
+            pywikibot.output("Got a RequestException, let's try again")
     return data
 
 
@@ -243,7 +248,6 @@
                   'doit': 'Uitvoeren'
                   }
 
-        tosend = urlencode(tosend)
         pywikibot.output(tosend)
         CH = pageTextPost('http://tools.wmflabs.org/commonshelper/index.php',
                           tosend)
diff --git a/tests/data/commonsHelper_description.txt 
b/tests/data/commonsHelper_description.txt
new file mode 100644
index 0000000..2e28200
--- /dev/null
+++ b/tests/data/commonsHelper_description.txt
@@ -0,0 +1,25 @@
+
+== {{int:filedesc}} ==
+{{Information
+|Description={{id|http://www.bekasikota.go.id/pages/profil-wakil-walikota-bekasi}}
+|Source={{transferred from|id.wikipedia}}
+|Date={{Original upload date|2018-01-07}}
+|Author={{Original uploader|Ramdan Herawan|wikipedia|id}}
+|Permission=PD-IDGOV.
+|other_versions=
+}}
+
+== {{int:license-header}} ==
+{{PD-IDGov}}
+<!-- Templates "Template:DU-PemerintahIndonesia", "Template:Lambang Indonesia" 
were used in the original description page as well , but do not appear to exist 
on commons. -->
+
+== {{Original upload log}} ==
+{{original description|id.wikipedia|Ahmad+Syaikhu+Wakil+Walikota+Bekasi.jpg}}
+{| class="wikitable filehistory"
+! {{int:filehist-datetime}} !! {{int:filehist-dimensions}} !! 
{{int:filehist-user}} !! {{int:filehist-comment}}
+|-
+| 2018-01-07 01:03 || 448&times;539&times; (46784 bytes) || [[:id:User:Ramdan 
Herawan|Ramdan Herawan]] || 
''<nowiki>http://www.bekasikota.go.id/pages/profil-wakil-walikota-bekasi</nowiki>''
+|}
+__NOTOC__
+
+{{subst:Unc}} <!-- Remove this line once you have added categories -->
\ No newline at end of file
diff --git a/tests/imagecopy_tests.py b/tests/imagecopy_tests.py
new file mode 100644
index 0000000..a5502f6
--- /dev/null
+++ b/tests/imagecopy_tests.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+"""Tests for imagecopy script."""
+#
+# (C) Pywikibot team, 2018
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import absolute_import, unicode_literals
+
+import re
+
+from scripts.imagecopy import pageTextPost
+
+from tests.aspects import TestCase
+from tests import join_data_path
+
+
+class CommonsHelperMethodTest(TestCase):
+    """Test CommonsHelper methods in imagecopy."""
+
+    hostname = 'https://tools.wmflabs.org/commonshelper/'
+
+    def test_pageTextPost(self):
+        """Test scripts.imagecopy.pageTextPost() method."""
+        parameters_dict = {
+            'language': b'id',
+            'image': b'Ahmad Syaikhu Wakil Walikota Bekasi.jpg',
+            'newname': b'Ahmad Syaikhu Wakil Walikota Bekasi.jpg',
+            'project': b'wikipedia',
+            'username': '',
+            'commonsense': '1',
+            'remove_categories': '1',
+            'ignorewarnings': '1',
+            'doit': 'Uitvoeren'}
+
+        commons_helper = pageTextPost('', parameters_dict)
+        # Extract the CommonsHelper description from the html
+        commons_helper = (
+            re.compile(
+                "<textarea .+ name='wpUploadDescription'>(.+)</textarea>",
+                re.DOTALL | re.M).findall(commons_helper)[0])
+        with open(join_data_path('commonsHelper_description.txt')) as f:
+            self.assertEqual(f.read(), commons_helper)

-- 
To view, visit https://gerrit.wikimedia.org/r/402591
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I278081b24ff265a43c37372cd26f6fbae1c3b44e
Gerrit-PatchSet: 15
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Rafidaslam <rafidt...@gmail.com>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Rafidaslam <rafidt...@gmail.com>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to