Skip to content

Instantly share code, notes, and snippets.

@lubianat
Last active July 18, 2023 16:02
Show Gist options
  • Save lubianat/fb160b7b0b60455b1a926e651795b6e2 to your computer and use it in GitHub Desktop.
Save lubianat/fb160b7b0b60455b1a926e651795b6e2 to your computer and use it in GitHub Desktop.
Remove Wikibase statements with a deleted item as value
import requests
from login import WD_USER, WD_PASS
import json
from wikibaseintegrator import WikibaseIntegrator
from wikibaseintegrator.wbi_helpers import remove_claims
from wikibaseintegrator.wbi_login import Clientlogin
from helper import wbi_config
## Deleted items are manually selected at this point
deleted_items = ["Q334", "Q348", "Q349", "Q351"]
login_instance = Clientlogin(user=WD_USER, password=WD_PASS)
wbi = WikibaseIntegrator(login=login_instance)
for deleted_item in deleted_items:
query = f"https://nounsdev.wikibase.cloud/w/api.php?action=query&list=backlinks&blnamespace=120&bltitle=Item:{deleted_item}&format=json"
r = requests.get(query)
data = r.json()
pages = [a["title"].replace("Item:", "") for a in data["query"]["backlinks"]]
for page in pages:
item = wbi.item.get(entity_id=page)
claims = item.claims
for claim in claims:
claim_json = claim.get_json()
try:
claim_value = claim_json["mainsnak"]["datavalue"]["value"]["id"]
except TypeError as e:
print(e)
continue
except KeyError as e:
print(e)
continue
claim_id = claim_json["id"]
if claim_value in deleted_items:
claim.remove()
item.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment