def second_pass(self): for oparl_object, values in self.missing_links.items(): print("ADDMISSING", oparl_object) wd_item_id = self.id_mapping.get(oparl_object) claims = [] for (key, value) in values: claim = self.create_single_claim(value, self.type_mapping[key]["type"], self.type_mapping[key]["property"]) claims.append(claim) print("CLAIM", claim) wd_item = WDItemEngine(wd_item_id=wd_item_id, item_name=None, domain="", data=claims, server=self.server, base_url_template=self.base_url_template) wd_item.write(self.login) self.save_any("id", self.id_mapping)
def write_claims_to_item(qid, json_data, wdi_login): """ Write new claims to an item. Args: qid (str): Wikidata Identifier json_data (List[Dict]): Data from request wdi_login (WDLogin): Returns (Response): """ # Build statements data = [ build_statement(claim_data.get('pid'), claim_data.get('value'), claim_data.get('type'), claim_data.get('qualifiers'), claim_data.get('references')) for claim_data in json_data ] props = [statement.prop_nr for statement in data] # Get wikidata item item = WDItemEngine(wd_item_id=qid, mediawiki_api_url=MEDIAWIKI_API_URL, data=data, append_value=props) qid = item.write(wdi_login) return jsonify({ "message": f"Successfully Contributed {len(data)} Statement(s) " f"to Wikidata Item '{item.get_label()}' ({qid}).", "status": "success", })
def _try_write(self, entity: wdi_core.WDItemEngine, **kwargs) -> ModificationResult: try: eid = entity.write(self._local_login, **kwargs) return ModificationResult(successful=True, res=eid) except wdi_core.WDApiError as err: logger.warning(err.wd_error_msg['error']) err_code = err.wd_error_msg['error']['code'] msg = err.wd_error_msg['error']['info'] if err_code == ERR_CODE_LANGUAGE: logger.warning("Language was not recognized. Skipping it...") return ModificationResult(successful=False, message=msg)
def _temp_route_oauth_write_test(): # One-off test to ensure pipes are running, add an alias to WikiDP item identity = identify_user() for key in identity.keys(): logging.info('KEY: %s VALUE: %s', key, identity.get(key)) item = WDItemEngine(wd_item_id="Q51139559") item.set_aliases(['WikiDP Application'], append=True) # verify the api is working by getting this item assert item.get_label() == "Wikidata for Digital Preservation" wdi_login = get_wdi_login() # verify edit token exists, this is what WDI calls assert wdi_login.get_edit_token() assert "user" in identity.get('groups') # verify user in user group # verify user in user group assert "autoconfirmed" in identity.get('groups') assert "edit" in identity.get('rights') # verify user in user group assert "editpage" in identity.get('grants') # verify user in user group updated = item.write(wdi_login) # fails due to no permissions return jsonify(updated)
def index_single_object(self, oparl_object, full_pass): oparl_id = oparl_object.get_id() if full_pass: claims = self.get_claims(oparl_object, debug_url=oparl_id) else: id_claim = WDUrl(value=oparl_id, prop_nr=self.mapping["id"]["property"]) type_claim = WDUrl(value=oparl_object.get_oparl_type(), prop_nr=self.mapping["type"]["property"]) claims = [id_claim, type_claim] if self.url_to_item_id.has(oparl_id): wd_item_id = self.url_to_item_id.get(oparl_id) item_name = None domain = "" else: print("Creating new item") wd_item_id = "" item_name = oparl_id domain = None wd_item = WDItemEngine(wd_item_id=wd_item_id, item_name=item_name, domain=domain, data=claims, server=self.server, base_url_template=self.base_url_template) wd_item.set_label(oparl_id) returned = wd_item.write(self.wikibase_login) self.url_to_item_id.set(oparl_id, returned) print(oparl_id) print("http://{}/index.php?title=Item:{}".format( self.server, returned)) print()
def push_elem(self, oparl_object): print("PROESSING", oparl_object["id"]) oparl_id = oparl_object["id"] if oparl_id in self.id_mapping.keys(): wd_item_id = self.id_mapping.get(oparl_id) item_name = None domain = "" else: wd_item_id = "" item_name = oparl_id domain = None claims = self.get_claims(oparl_object) wd_item = WDItemEngine(wd_item_id=wd_item_id, item_name=item_name, domain=domain, data=claims, server=self.server, base_url_template=self.base_url_template) wd_item.set_label(oparl_id) returned = wd_item.write(self.login) self.id_mapping[oparl_id] = returned if item_name: print("CREATED", "http://{}/index.php?title=Item:{}".format(self.server, returned)) else: print("UPDATED", "http://{}/index.php?title=Item:{}".format(self.server, returned))