Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F10814221
create_surname
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
create_surname
View Options
#!/usr/bin/env python
"""
This script creates a new surname on Wikidata based on input.
TODO: check no item exists, with P31:Q101352
"""
import
argparse
import
json
import
pywikibot
import
os
# usage: create_surname.py [-h] surname
parser
=
argparse
.
ArgumentParser
(
description
=
'Creates a surname item on Wikidata.'
)
parser
.
add_argument
(
'surname'
,
metavar
=
'surname'
,
nargs
=
1
,
help
=
'the surname to create'
)
args
=
parser
.
parse_args
()
surname
=
args
.
surname
[
0
]
# Consistency check
# if surname[0].islower():
# sys.stderr.write(
# "Can't create a surname with the first letter in lower case.\n"
# )
# sys.exit(5);
# Creates item on Wikidata
site
=
pywikibot
.
Site
(
"wikidata"
,
"wikidata"
)
repo
=
site
.
data_repository
()
data
=
{}
result
=
repo
.
editEntity
(
{},
{},
summary
=
"Creating empty item for surname "
+
surname
)
title
=
result
.
get
(
'entity'
)
.
get
(
'id'
)
item
=
pywikibot
.
ItemPage
(
repo
,
title
=
title
)
item
.
get
()
script_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
# Gets whitelist latin languages to avoid create the element in other scripts.
latin_languages_file
=
os
.
path
.
join
(
script_dir
,
'l10n/latin-whitelist.json'
)
with
open
(
latin_languages_file
)
as
data_file
:
latin_languages
=
json
.
load
(
data_file
)
# Gets descriptions from l10n file, and fill labels for these languages.
l10n_file
=
os
.
path
.
join
(
script_dir
,
'l10n'
,
'surname.json'
)
with
open
(
l10n_file
)
as
data_file
:
descriptions
=
json
.
load
(
data_file
)
labels_languages
=
set
(
descriptions
.
keys
())
.
intersection
(
latin_languages
)
labels
=
{
lang
:
surname
for
lang
in
latin_languages
}
item
.
editLabels
(
labels
,
summary
=
'Adding surname labels'
)
item
.
editDescriptions
(
descriptions
,
summary
=
'Adding surname descriptions'
)
claim
=
pywikibot
.
Claim
(
repo
,
'P31'
)
target
=
pywikibot
.
ItemPage
(
repo
,
'Q101352'
)
claim
.
setTarget
(
target
)
item
.
addClaim
(
claim
)
print
(
title
)
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Tue, Jul 29, 14:29 (11 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2852712
Default Alt Text
create_surname (1 KB)
Attached To
Mode
rDWD Dæghrefn Wikidata access layer
Attached
Detach File
Event Timeline
Log In to Comment