def modify_metadata(self, metadata, target=None, append=None, priority=None, access_key=None, secret_key=None, debug=None, request_kwargs=None): """Modify the metadata of an existing item on Archive.org. Note: The Metadata Write API does not yet comply with the latest Json-Patch standard. It currently complies with `version 02 <https://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02>`__. :type metadata: dict :param metadata: Metadata used to update the item. :type target: str :param target: (optional) Set the metadata target to update. :type priority: int :param priority: (optional) Set task priority. Usage:: >>> import internetarchive >>> item = internetarchive.Item('mapi_test_item1') >>> md = dict(new_key='new_value', foo=['bar', 'bar2']) >>> item.modify_metadata(md) :rtype: dict :returns: A dictionary containing the status_code and response returned from the Metadata API. """ target = 'metadata' if target is None else target append = False if append is None else append access_key = self.session.access_key if not access_key else access_key secret_key = self.session.secret_key if not secret_key else secret_key debug = False if debug is None else debug request_kwargs = {} if not request_kwargs else request_kwargs url = '{protocol}//archive.org/metadata/{identifier}'.format( protocol=self.session.protocol, identifier=self.identifier) request = MetadataRequest( url=url, metadata=metadata, source_metadata=self.item_metadata.get(target.split('/')[0], {}), target=target, priority=priority, access_key=access_key, secret_key=secret_key, append=append) if debug: return request prepared_request = request.prepare() resp = self.session.send(prepared_request, **request_kwargs) # Re-initialize the Item object with the updated metadata. self.refresh() return resp
def modify_metadata(self, metadata, target=None, append=None, priority=None, access_key=None, secret_key=None, debug=None, request_kwargs=None): """Modify the metadata of an existing item on Archive.org. Note: The Metadata Write API does not yet comply with the latest Json-Patch standard. It currently complies with `version 02 <https://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02>`__. :type metadata: dict :param metadata: Metadata used to update the item. :type target: str :param target: (optional) Set the metadata target to update. :type priority: int :param priority: (optional) Set task priority. Usage:: >>> import internetarchive >>> item = internetarchive.Item('mapi_test_item1') >>> md = dict(new_key='new_value', foo=['bar', 'bar2']) >>> item.modify_metadata(md) :rtype: dict :returns: A dictionary containing the status_code and response returned from the Metadata API. """ target = 'metadata' if target is None else target append = False if append is None else append access_key = self.session.access_key if not access_key else access_key secret_key = self.session.secret_key if not secret_key else secret_key debug = False if debug is None else debug request_kwargs = {} if not request_kwargs else request_kwargs url = '{protocol}//archive.org/metadata/{identifier}'.format( protocol=self.session.protocol, identifier=self.identifier) request = MetadataRequest(url=url, metadata=metadata, source_metadata=self.item_metadata.get( target.split('/')[0], {}), target=target, priority=priority, access_key=access_key, secret_key=secret_key, append=append) if debug: return request prepared_request = request.prepare() resp = self.session.send(prepared_request, **request_kwargs) # Re-initialize the Item object with the updated metadata. self.refresh() return resp
def modify_metadata(self, metadata, target=None, append=None, append_list=None, priority=None, access_key=None, secret_key=None, debug=None, request_kwargs=None): """Modify the metadata of an existing item on Archive.org. Note: The Metadata Write API does not yet comply with the latest Json-Patch standard. It currently complies with `version 02 <https://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02>`__. :type metadata: dict :param metadata: Metadata used to update the item. :type target: str :param target: (optional) Set the metadata target to update. :type priority: int :param priority: (optional) Set task priority. :type append: bool :param append: (optional) Append value to an existing multi-value metadata field. :type append_list: bool :param append_list: (optional) Append values to an existing multi-value metadata field. No duplicate values will be added. Usage:: >>> import internetarchive >>> item = internetarchive.Item('mapi_test_item1') >>> md = dict(new_key='new_value', foo=['bar', 'bar2']) >>> item.modify_metadata(md) :rtype: dict :returns: A dictionary containing the status_code and response returned from the Metadata API. """ append = False if append is None else append access_key = self.session.access_key if not access_key else access_key secret_key = self.session.secret_key if not secret_key else secret_key debug = False if debug is None else debug request_kwargs = {} if not request_kwargs else request_kwargs url = '{protocol}//archive.org/metadata/{identifier}'.format( protocol=self.session.protocol, identifier=self.identifier) # TODO: currently files and metadata targets do not support dict's, # but they might someday?? refactor this check. source_metadata = self.item_metadata request = MetadataRequest( method='POST', url=url, metadata=metadata, headers=self.session.headers, source_metadata=source_metadata, target=target, priority=priority, access_key=access_key, secret_key=secret_key, append=append, append_list=append_list) # Must use Session.prepare_request to make sure session settings # are used on request! prepared_request = request.prepare() if debug: return prepared_request resp = self.session.send(prepared_request, **request_kwargs) # Re-initialize the Item object with the updated metadata. self.refresh() return resp
def modify_metadata(self, metadata, target=None, append=None, append_list=None, priority=None, access_key=None, secret_key=None, debug=None, request_kwargs=None): """Modify the metadata of an existing item on Archive.org. Note: The Metadata Write API does not yet comply with the latest Json-Patch standard. It currently complies with `version 02 <https://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02>`__. :type metadata: dict :param metadata: Metadata used to update the item. :type target: str :param target: (optional) Set the metadata target to update. :type priority: int :param priority: (optional) Set task priority. :type append: bool :param append: (optional) Append value to an existing multi-value metadata field. :type append_list: bool :param append_list: (optional) Append values to an existing multi-value metadata field. No duplicate values will be added. Usage:: >>> import internetarchive >>> item = internetarchive.Item('mapi_test_item1') >>> md = dict(new_key='new_value', foo=['bar', 'bar2']) >>> item.modify_metadata(md) :rtype: dict :returns: A dictionary containing the status_code and response returned from the Metadata API. """ append = False if append is None else append access_key = self.session.access_key if not access_key else access_key secret_key = self.session.secret_key if not secret_key else secret_key debug = False if debug is None else debug request_kwargs = {} if not request_kwargs else request_kwargs url = '{protocol}//archive.org/metadata/{identifier}'.format( protocol=self.session.protocol, identifier=self.identifier) # TODO: currently files and metadata targets do not support dict's, # but they might someday?? refactor this check. source_metadata = self.item_metadata request = MetadataRequest(method='POST', url=url, metadata=metadata, headers=self.session.headers, source_metadata=source_metadata, target=target, priority=priority, access_key=access_key, secret_key=secret_key, append=append, append_list=append_list) # Must use Session.prepare_request to make sure session settings # are used on request! prepared_request = request.prepare() if debug: return prepared_request resp = self.session.send(prepared_request, **request_kwargs) # Re-initialize the Item object with the updated metadata. self.refresh() return resp
def modify_metadata( self, metadata: Mapping, target: str | None = None, append: bool = False, append_list: bool = False, priority: int = 0, access_key: str | None = None, secret_key: str | None = None, debug: bool = False, headers: Mapping | None = None, request_kwargs: Mapping | None = None) -> Request | Response: """Modify the metadata of an existing item on Archive.org. Note: The Metadata Write API does not yet comply with the latest Json-Patch standard. It currently complies with `version 02 <https://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02>`__. :param metadata: Metadata used to update the item. :param target: Set the metadata target to update. :param priority: Set task priority. :param append: Append value to an existing multi-value metadata field. :param append_list: Append values to an existing multi-value metadata field. No duplicate values will be added. :returns: A Request if debug else a Response. Usage:: >>> import internetarchive >>> item = internetarchive.Item('mapi_test_item1') >>> md = {'new_key': 'new_value', 'foo': ['bar', 'bar2']} >>> item.modify_metadata(md) """ append = bool(append) access_key = access_key or self.session.access_key secret_key = secret_key or self.session.secret_key debug = bool(debug) headers = headers or {} request_kwargs = request_kwargs or {} _headers = self.session.headers.copy() _headers.update(headers) url = f'{self.session.protocol}//{self.session.host}/metadata/{self.identifier}' # TODO: currently files and metadata targets do not support dict's, # but they might someday?? refactor this check. source_metadata = self.item_metadata request = MetadataRequest(method='POST', url=url, metadata=metadata, headers=_headers, source_metadata=source_metadata, target=target, priority=priority, access_key=access_key, secret_key=secret_key, append=append, append_list=append_list) # Must use Session.prepare_request to make sure session settings # are used on request! prepared_request = request.prepare() if debug: return prepared_request resp = self.session.send(prepared_request, **request_kwargs) # Re-initialize the Item object with the updated metadata. self.refresh() return resp