Example #1
0
    def _uploadImageOld(self, debug=False):
        if not hasattr(self, '_contents'):
            self.read_file_content()

        filename = self.process_filename()
        # Convert the filename (currently Unicode) to the encoding used on the
        # target wiki
        encodedFilename = filename.encode(self.targetSite.encoding())

        formdata = {
            'wpUploadDescription': self.description,
            'wpUploadAffirm': '1',
            'wpUpload': 'upload bestand',
            'wpEditToken': self.targetSite.getToken(),  # Get an edit token so we can do the upload
            'wpDestFile': filename,  # Set the new filename
        }
        # This somehow doesn't work.
        if self.ignoreWarning:
            formdata["wpIgnoreWarning"] = "1"

        if self.uploadByUrl:
            formdata["wpUploadFileURL"] = self.url
            formdata["wpSourceType"] = 'Url'

        # try to encode the strings to the encoding used by the target site.
        # if that's not possible (e.g. because there are non-Latin-1 characters
        # and the home Wikipedia uses Latin-1), convert all non-ASCII
        # characters to HTML entities.
        for key in formdata:
            assert isinstance(key, basestring), \
                "ERROR: %s is not a string but %s" % (key, type(key))
            try:
                formdata[key] = formdata[key].encode(self.targetSite.encoding())
            except (UnicodeEncodeError, UnicodeDecodeError):
                formdata[key] = pywikibot.UnicodeToAsciiHtml(
                    formdata[key]).encode(self.targetSite.encoding())

        # don't upload if we're in debug mode
        if not debug:
            pywikibot.output(u'Uploading file to %s...' % self.targetSite)

            if self.uploadByUrl:
                # Just do a post with all the fields filled out
                response, returned_html = self.targetSite.postForm(
                    self.targetSite.upload_address(), formdata.items(),
                    cookies=self.targetSite.cookies())
            else:
                response, returned_html = post_multipart(
                    self.targetSite, self.targetSite.upload_address(),
                    formdata.items(),
                    (('wpUploadFile', encodedFilename, self._contents),),
                    cookies=self.targetSite.cookies())
            # There are 2 ways MediaWiki can react on success: either it gives
            # a 200 with a success message, or it gives a 302 (redirection).
            # Do we know how the "success!" HTML page should look like?
            # ATTENTION: if you changed your Wikimedia Commons account not to
            # show an English interface, this detection will fail!
            success_msg = self.targetSite.mediawiki_message('successfulupload')
            if success_msg in returned_html or response.code == 302:
                pywikibot.output(u"Upload successful.")
            # The following is not a good idea, because the server also gives a
            # 200 when something went wrong.
##            if response.code in [200, 302]:
##                pywikibot.output(u"Upload successful.")

            elif response.code == 301:
                pywikibot.output(u"Following redirect...")
                address = response.getheader('Location')
                pywikibot.output(
                    u"Changed upload address to %s. Please update %s.py"
                    % (address, self.targetSite.family.__module__))
                exec('self.targetSite.upload_address = lambda: %r' % address,
                     locals(), globals())
                return self.upload_image(debug)
            else:
                try:
                    # Try to find the error message within the HTML page.
                    # If we can't find it, we just dump the entire HTML page.
                    returned_html = returned_html[
                        returned_html.index('<!-- start content -->') + 22:
                        returned_html.index('<!-- end content -->')]
                except:
                    pass
                pywikibot.output(u'%s\n\n' % returned_html)
                pywikibot.output(u'%i %s' % (response.code, response.msg))

                if self.targetSite.mediawiki_message('uploadwarning'
                                                     ) in returned_html:
                    answer = pywikibot.inputChoice(
                        u"You have recevied an upload warning message. Ignore?",
                        ['Yes', 'No'], ['y', 'N'], 'N')
                    if answer == "y":
                        self.ignoreWarning = 1
                        self.keepFilename = True
                        return self._uploadImageOld(debug)
                else:
                    answer = pywikibot.inputChoice(
                        u'Upload of %s probably failed. Above you see the HTML '
                        u'page which was returned by MediaWiki. Try again?'
                        % filename, ['Yes', 'No'], ['y', 'N'], 'N')
                    if answer == "y":
                        return self._uploadImageOld(debug)
                    else:
                        return
        return filename
Example #2
0
            main = False
        else:
            workingcatname.append(arg)

    if len(workingcatname) == 0:
        workingcatname = raw_input("Which page to start with? ")
    else:
        workingcatname = ' '.join(workingcatname)
    mysite = pywikibot.getSite()
    pywikibot.setAction(
        pywikibot.translate(mysite, msg) + ' ' + workingcatname)
    workingcat = catlib.Category(
        mysite, u'%s:%s' % (mysite.category_namespace(), workingcatname))
    filename = pywikibot.config.datafilepath(
        'category',
        pywikibot.UnicodeToAsciiHtml(workingcatname) + '_exclude.txt')
    try:
        f = codecs.open(filename, 'r', encoding=mysite.encoding())
        for line in f.readlines():
            # remove trailing newlines and carriage returns
            try:
                while line[-1] in ['\n', '\r']:
                    line = line[:-1]
            except IndexError:
                pass
            exclude(line, real_exclude=False)
            pl = pywikibot.Page(mysite, line)
            checked[pl] = pl
        f.close()
        excludefile = codecs.open(filename, 'a', encoding=mysite.encoding())
    except IOError:
Example #3
0
        elif arg.startswith('-keepparent'):
            removeparent = False
        elif arg.startswith('-all'):
            main = False
        else:
            workingcatname.append(arg)

    if len(workingcatname) == 0:
        workingcatname = raw_input("Which page to start with? ")
    else:
        workingcatname = ' '.join(workingcatname)
    mysite = wikipedia.getSite()
    wikipedia.setAction(wikipedia.translate(mysite,msg) + ' ' + workingcatname)
    workingcat = catlib.Category(mysite,mysite.category_namespace()+':'+workingcatname)
    filename = wikipedia.config.datafilepath('category',
                   wikipedia.UnicodeToAsciiHtml(workingcatname) + '_exclude.txt')
    try:
        f = codecs.open(filename, 'r', encoding = mysite.encoding())
        for line in f.readlines():
            # remove trailing newlines and carriage returns
            try:
                while line[-1] in ['\n', '\r']:
                    line = line[:-1]
            except IndexError:
                pass
            exclude(line,real_exclude=False)
            pl = wikipedia.Page(mysite,line)
            checked[pl] = pl
        f.close()
        excludefile = codecs.open(filename, 'a', encoding = mysite.encoding())
    except IOError: