Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: contentlanguages need to be matched before piping output to target #5

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM node:20-alpine

RUN apk add --no-cache jq=1.7.1-r0 curl=8.8.0-r0 && \
RUN apk add --no-cache curl=8.8.0-r0 python3=3.12.3-r1 && \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd not noticed this removed the jq dependency; that's a nice happy accident

npm install -g wikibase-cli@18.0.3

COPY --chmod=755 ./transferbot.sh /usr/bin/transferbot
COPY --chmod=755 ./mangle_data.py /usr/bin/mangle_data

ENTRYPOINT ["transferbot"]
54 changes: 54 additions & 0 deletions mangle_data.py
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A most amusing name

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3

import argparse
import json
import fileinput
from urllib import request

parser = argparse.ArgumentParser(
description="Adjust a line from wb-cli so it can be fed into the target wiki"
)

parser.add_argument("-t", "--target", action="store", dest="target", required=True)
parser.add_argument("-p", "--pick", action="append", dest="pick", required=True)


def process_line(line, pick=[], select_languages=set()):
data = json.loads(line)
out = {}
for key, value in data.items():
if key not in pick:
continue

if not isinstance(value, dict):
out[key] = value
continue

out[key] = {}
for lang, value in data[key].items():
if lang in select_languages:
out[key][lang] = value

return out


def get_contentlanguages(target_origin):
with request.urlopen(
f"{target_origin}/w/api.php?action=query&meta=wbcontentlanguages&format=json"
) as response:
raw_body = response.read()
body = json.loads(raw_body)
return set(body["query"]["wbcontentlanguages"].keys())


def main():
args = parser.parse_args()
target_languages = get_contentlanguages(args.target)

for line in fileinput.input("-"):
out = process_line(line, pick=args.pick, select_languages=target_languages)
print(json.dumps(out, ensure_ascii=False))


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion transferbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ CREDS
} > $(wb config path)

wb data $@ --instance "$source_wiki_origin" |\
jq --compact-output '{type,labels,descriptions,aliases,datatype}' |\
mangle_data -t "$target_wiki_origin" -p type -p labels -p descriptions -p aliases -p datatype |\
wb create-entity --batch --instance "$target_wiki_origin"
Loading