Skip to content

Commit

Permalink
fix: contentlanguages need to be matched before piping output to target
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jul 23, 2024
1 parent 125ff34 commit d682742
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
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 jq=1.7.1-r0 curl=8.8.0-r0 python3=3.12.3-r1 && \
npm install -g wikibase-cli@18.0.3

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

ENTRYPOINT ["transferbot"]
46 changes: 46 additions & 0 deletions adjust_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3

import argparse
import json
import fileinput

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

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


def main():
args = parser.parse_args()
source = json.load(open(args.source))
target = json.load(open(args.target))

source_languages = source["query"]["wbcontentlanguages"].keys()
target_languages = target["query"]["wbcontentlanguages"].keys()

selected_languages = set(source_languages) & set(target_languages)

for line in fileinput.input("-"):
data = json.loads(line)
out = {}
for key, value in data.items():
if key not in args.pick:
continue

if type(value) is not "dict":
out[key] = value
continue

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

print(json.dumps(out, ensure_ascii=False))


if __name__ == "__main__":
main()
7 changes: 6 additions & 1 deletion transferbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ cat <<CREDS
CREDS
} > $(wb config path)

source_languages=$(mktemp)
curl -sS "$source_wiki_origin/w/api.php?action=query&meta=wbcontentlanguages&format=json" > $source_languages
target_languages=$(mktemp)
curl -sS "$target_wiki_origin/w/api.php?action=query&meta=wbcontentlanguages&format=json" > $target_languages

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

0 comments on commit d682742

Please sign in to comment.