Created
July 5, 2021 16:21
-
-
Save moonhouse/0916585dad87a2b3eb63c4810da168fa to your computer and use it in GitHub Desktop.
Convert quickstatements from quickstatements.toolforge.org/api.php?action=get_commands_from_batch back into quickstatements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
data = json.loads(sys.stdin.read())['data'] | |
fs = "" | |
s = "" | |
for prop in data: | |
statement = f"""{prop['json']['item']}\t{prop['json']['property']}\t{prop['json']['datavalue']['value']['language']}:"{prop['json']['datavalue']['value']['text']}"\t""" | |
if s != statement: | |
fs += statement | |
s = statement | |
if 'qualifier' in prop['json']: | |
qs = f"""{prop['json']['qualifier']['prop']}\t{prop['json']['qualifier']['value']['value']['id']}\t""" | |
fs += qs | |
if 'sources' in prop['json']: | |
for src in prop['json']['sources']: | |
prop = src['prop'].replace('P','S') | |
match src['value']['type']: | |
case "string": | |
s = '"' + src['value']['value'] + '"' | |
case "time": | |
s = f"""{src['value']['value']['time']}/{src['value']['value']['precision']}""" | |
case "wikibase-entityid": | |
s = src['value']['value']['id'] | |
case _: | |
print("Unknown type "+src['value']['type']) | |
fs += f"{prop}\t{s}\t" | |
print(fs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment