Пример #1
0
	def upload(self, file = None, filename = None, description = '', ignore = False, file_size = None,
			url = None, session_key = None):
		if self.version[:2] < (1, 16):
			return compatibility.old_upload(self, file = file, filename = filename, 
						description = description, ignore = ignore, 
						file_size = file_size)
		
		image = self.Images[filename]
		if not image.can('upload'):
			raise errors.InsufficientPermission(filename)
		

		
		predata = {}
		
		predata['comment'] = description
		if ignore: 
			predata['ignorewarnings'] = 'true'
		predata['token'] = image.get_token('edit')
		predata['action'] = 'upload'
		predata['format'] = 'json'
		predata['filename'] = filename
		if url:
			predata['url'] = url
		if session_key:
			predata['session_key'] = session_key 
		
		if file is None:
			postdata = predata
		else:			
			if type(file) is str:
				file_size = len(file)
				file = StringIO(file)
			if file_size is None:
				file.seek(0, 2)
				file_size = file.tell()
				file.seek(0, 0)
				
			postdata = upload.UploadFile('file', filename, file_size, file, predata)
		
		wait_token = self.wait_token()
		while True:
			try:
				data = self.raw_call('api', postdata).read()
				info = json.loads(data)
				if not info:
					info = {}
				if self.handle_api_result(info, kwargs = predata):
					return info.get('upload', {})
			except errors.HTTPStatusError, e:
				if e[0] == 503 and e[1].getheader('X-Database-Lag'):
					self.wait(wait_token, int(e[1].getheader('Retry-After')))
				elif e[0] < 500 or e[0] > 599:
					raise
				else:
					self.wait(wait_token)
			except errors.HTTPError:
				self.wait(wait_token)
Пример #2
0
    def upload(
        self, file=None, filename=None, description="", ignore=False, file_size=None, url=None, session_key=None
    ):
        if self.version[:2] < (1, 16):
            return compatibility.old_upload(
                self, file=file, filename=filename, description=description, ignore=ignore, file_size=file_size
            )

        image = self.Images[filename]
        if not image.can("upload"):
            raise errors.InsufficientPermission(filename)

        predata = {}

        predata["comment"] = description
        if ignore:
            predata["ignorewarnings"] = "true"
        predata["token"] = image.get_token("edit")
        predata["action"] = "upload"
        predata["format"] = "json"
        predata["filename"] = filename
        if url:
            predata["url"] = url
        if session_key:
            predata["session_key"] = session_key

        if file is None:
            postdata = self._query_string(predata)
        else:
            if type(file) is str:
                file_size = len(file)
                file = StringIO(file)
            if file_size is None:
                file.seek(0, 2)
                file_size = file.tell()
                file.seek(0, 0)

            postdata = upload.UploadFile("file", filename, file_size, file, predata)

        wait_token = self.wait_token()
        while True:
            try:
                data = self.raw_call("api", postdata).read()
                info = json.loads(data)
                if not info:
                    info = {}
                if self.handle_api_result(info, kwargs=predata):
                    return info.get("upload", {})
            except errors.HTTPStatusError, e:
                if e[0] == 503 and e[1].getheader("X-Database-Lag"):
                    self.wait(wait_token, int(e[1].getheader("Retry-After")))
                elif e[0] < 500 or e[0] > 599:
                    raise
                else:
                    self.wait(wait_token)
            except errors.HTTPError:
                self.wait(wait_token)
Пример #3
0
    def upload(self, fileobj=None, filename=None, description='', ignore=False, file_size=None, url=None, session_key=None):
        """
        Parameters:
            fileobj: File-like object with the data to upload.
            filename: Destination filename (on the wiki).
            description: Add this description to the file (on the wiki).
            ignore: Add "ignorewarnings"=true parameter to the query
                    (required to upload a new/updated version of an existing image.)
        """
        if self.version[:2] < (1, 16):
            print("DEBUG: upload() - Using old upload method...")
            return compatibility.old_upload(self, fileobj=fileobj, filename=filename, description=description, ignore=ignore, file_size=file_size)

        image = self.Images[filename]
        if not image.can('upload'):
            raise errors.InsufficientPermission(filename)

        predata = {}

        predata['comment'] = description
        if ignore:
            predata['ignorewarnings'] = 'true'
        # This will likely invoke an api call.
        # If image.name (title) is invalid, then this could cause an exception.
        # (Would be KeyError for old mwclient code, is now ValueError)
        # If an exception is raised, should we do anything about it? - No, we can't do much from here.
        predata['token'] = image.get_token('edit') # May raise KeyError/ValueError
        predata['action'] = 'upload'
        predata['format'] = 'json'
        predata['filename'] = filename
        if url:
            predata['url'] = url
        if session_key:
            predata['session_key'] = session_key

        if fileobj is None:
            postdata = self._query_string(predata)
        else:
            if type(fileobj) is str:
                file_size = len(fileobj)
                fileobj = StringIO(fileobj)
            if file_size is None:
                fileobj.seek(0, 2)          # Seek to end of file.
                file_size = fileobj.tell()
                fileobj.seek(0, 0)

            postdata = upload.UploadFile('file', filename, file_size, fileobj, predata)

        wait_token = self.wait_token()
        while True:
            try:
                data = self.raw_call('api', postdata).read()
                if pythonver >= 3:
                    info = json.loads(data.decode('utf-8'))
                else:
                    info = json.loads(data)
                if not info:
                    info = {}
                if self.handle_api_result(info, kwargs=predata):
                    return info.get('upload', {}) # 'upload' should be the only key...
            except errors.HTTPStatusError as exc:
                e = exc.args if pythonver >= 3 else exc
                if e[0] == 503 and e[1].getheader('X-Database-Lag'):
                    self.wait(wait_token, int(e[1].getheader('Retry-After')))
                elif e[0] < 500 or e[0] > 599:
                    raise
                else:
                    self.wait(wait_token)
            except errors.HTTPError:
                self.wait(wait_token)
            fileobj.seek(0, 0)