Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F11691000
create_surname
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
create_surname
View Options
#!/usr/bin/env python3
"""
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
]
# 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'
)
# Instance of "surname"
claim
=
pywikibot
.
Claim
(
repo
,
'P31'
)
target
=
pywikibot
.
ItemPage
(
repo
,
'Q101352'
)
claim
.
setTarget
(
target
)
item
.
addClaim
(
claim
)
# Native label: <the surname>
claim
=
pywikibot
.
Claim
(
repo
,
'P1705'
)
target
=
pywikibot
.
WbMonolingualText
(
text
=
surname
,
language
=
'mul'
)
claim
.
setTarget
(
target
)
item
.
addClaim
(
claim
)
# Writing system: "latin"
claim
=
pywikibot
.
Claim
(
repo
,
'P282'
)
target
=
pywikibot
.
ItemPage
(
repo
,
'Q8229'
)
claim
.
setTarget
(
target
)
item
.
addClaim
(
claim
)
item
.
editDescriptions
(
descriptions
,
summary
=
'Adding surname descriptions'
)
print
(
title
)
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Fri, Sep 12, 19:55 (12 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2978033
Default Alt Text
create_surname (2 KB)
Attached To
Mode
rDWD Dæghrefn Wikidata access layer
Attached
Detach File
Event Timeline
Log In to Comment