[prev in list] [next in list] [prev in thread] [next in thread]
List: pywikipediabot-users
Subject: Re: [pywikibot] How to create a new Property via pywikibot?
From: André Costa <lokal.profil () gmail ! com>
Date: 2016-04-01 7:22:47
Message-ID: CAPzQtCbuLreM8mpjxSuUChf6YaThHTKb0sfuzMJf5wU2pJ7CxQ () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
You can actually use site.EditEntity() even for creating Properties.
Re-using Ricordisamoa's example.
import pywikibot
site = pywikibot.Site('en', 'mywiki')
site.login()
data = {
'datatype': 'string', # mandatory
'descriptions': {
'en': {
'language': 'en',
'value': 'invented description'
}
},
'labels': {
'en': {
'language': 'en',
'value': 'test property'
}
}
}
identification = {
'new': 'property'
}
summary = 'my edit summary'
results = site.editEntity(identification, data, summary)
prop = pywikibot.PropertyPage(site, results.get(u'entity').get('id'))
On 25 March 2016 at 22:18, Ricordisamoa <ricordisamoa@openmailbox.org>
wrote:
> Example
>
> import json
> import pywikibot
>
> site = pywikibot.Site('en', 'mywiki')
> site.login()
> data = {
> 'datatype': 'string', # mandatory
> 'descriptions': {
> 'en': {
> 'language': 'en',
> 'value': 'invented description'
> }
> },
> 'labels': {
> 'en': {
> 'language': 'en',
> 'value': 'test property'
> }
> }
> }
> params = {
> 'action': 'wbeditentity',
> 'new': 'property',
> 'data': json.dumps(data),
> 'summary': 'my edit summary',
> 'token': site.tokens['edit']
> }
> req = site._simple_request(**params)
> print(req.submit())
>
>
>
> Il 23/03/2016 14:57, comfortably numb ha scritto:
>
> Hi pywikibot experts,
>
> How can I create new properties via pywikibot? (I'm trying to do it via
> bot, because I'm doing some experiments on a dedicated wikibase
> installation with - possibly - hundreds of properties to be created... and
> Pywikibot would certainly be my favorite tool!)
>
> In case I have, instead, to directly wrap the action "wbeditentity" from
> mediawiki API (
> https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity ),
> are there some Python examples?
>
> And, in case I have to use the php script "importProperties.php" (
> https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/importProperties.php \
> ), how can I manage properties more complex than the ones contained in the
> example (
> https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/en-elements-properties.csv
> )?
>
>
> Using pywikibot, I'm able to MODIFY existing properties with instructions
> like the following ones (which let me generate the object-content in one
> shot via json ... as I need):
>
> In [1]: import pywikibot ; site = pywikibot.Site() ; repo =
> site.data_repository()
> In [2]: property_page = pywikibot.PropertyPage(repo, u"P2")
> In [3]: myjson = {u'descriptions': {u'en': {u'language': u'en', u'value':
> u'invented description'}}, u'labels': {u'en': {u'language': u'en',
> u'value': u'test property'}}}
> In [4]: property_page.editEntity(myjson)
>
> ...but I cannot CREATE new properties (instantiating a PropertyPage
> object), because pywikibot asks for the identifier of an existing instance:
>
> In [11]: p_page = pywikibot.PropertyPage(repo)
> ---------------------------------------------------------------------------
> InvalidTitle Traceback (most recent call last)
> <ipython-input-11-e72b812b8cd3> in <module>()
> ----> 1 p_page = pywikibot.PropertyPage(repo)
>
> /home/user/src/pywikibot_repo/pywikibot/page.pyc in __init__(self, source,
> title)
> 4027 if not title or not self.id.startswith('P'):
> 4028 raise pywikibot.InvalidTitle(
> -> 4029 u"'%s' is not an property page title" % title)
> 4030 Property.__init__(self, source, self.id)
> 4031
>
> InvalidTitle: '' is not an property page title
>
> In [12]:
>
> In fact, as I understand, in the source code of the "WikibasePage" class,
> I see that
> while for the Item type, a "Special case for empty item" is mentioned (
> https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L3760
> )
>
> # Special case for empty item.
> if title is None or title == '-1':
> super(ItemPage, self).__init__(site, u'-1', ns=ns)
> assert self.id == '-1'
> return
>
> ...for the Property type, instead, an empty object is NOT allowed (
> https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L4168
> )
>
> if not title or not self.id.startswith('P'):
> raise pywikibot.InvalidTitle(
> u"'%s' is not an property page title" % title)
> Property.__init__(self, source, self.id)
>
>
> Thanks a lot for your attention!
>
>
> _______________________________________________
> pywikibot mailing listpywikibot@lists.wikimedia.orghttps://lists.wikimedia.org/mailman/listinfo/pywikibot
>
>
>
> _______________________________________________
> pywikibot mailing list
> pywikibot@lists.wikimedia.org
> https://lists.wikimedia.org/mailman/listinfo/pywikibot
>
>
[Attachment #5 (text/html)]
<div dir="ltr"><div>You can actually use site.EditEntity() even for creating \
Properties.<br><br></div>Re-using Ricordisamoa's example.<br><br><tt>import \
pywikibot</tt><tt><br> </tt><tt><br>
</tt><tt>site = pywikibot.Site('en', 'mywiki')</tt><tt><br>
</tt><tt>site.login()</tt><tt><br>
</tt><tt>data = {</tt><tt><br>
</tt><tt> 'datatype': 'string', # mandatory</tt><tt><br>
</tt><tt> 'descriptions': {</tt><tt><br>
</tt><tt> 'en': {</tt><tt><br>
</tt><tt> 'language': 'en',</tt><tt><br>
</tt><tt> 'value': 'invented \
description'</tt><tt><br> </tt><tt> }</tt><tt><br>
</tt><tt> },</tt><tt><br>
</tt><tt> 'labels': {</tt><tt><br>
</tt><tt> 'en': {</tt><tt><br>
</tt><tt> 'language': 'en',</tt><tt><br>
</tt><tt> 'value': 'test \
property'</tt><tt><br> </tt><tt> }</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt>}</tt><tt><br>
identification = {<br> 'new': 'property'<br>}<br>summary = \
'my edit summary'<br>results = site.editEntity(identification, data, \
summary)<br>prop = pywikibot.PropertyPage(site, \
results.get(u'entity').get('id'))<br><br><br></tt></div><div \
class="gmail_extra"><br><div class="gmail_quote">On 25 March 2016 at 22:18, \
Ricordisamoa <span dir="ltr"><<a href="mailto:ricordisamoa@openmailbox.org" \
target="_blank">ricordisamoa@openmailbox.org</a>></span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
Example<br>
<br>
<tt>import json</tt><tt><br>
</tt><tt>import pywikibot</tt><tt><br>
</tt><tt><br>
</tt><tt>site = pywikibot.Site('en', 'mywiki')</tt><tt><br>
</tt><tt>site.login()</tt><tt><br>
</tt><tt>data = {</tt><tt><br>
</tt><tt> 'datatype': 'string', # mandatory</tt><tt><br>
</tt><tt> 'descriptions': {</tt><tt><br>
</tt><tt> 'en': {</tt><tt><br>
</tt><tt> 'language': 'en',</tt><tt><br>
</tt><tt> 'value': 'invented \
description'</tt><tt><br> </tt><tt> }</tt><tt><br>
</tt><tt> },</tt><tt><br>
</tt><tt> 'labels': {</tt><tt><br>
</tt><tt> 'en': {</tt><tt><br>
</tt><tt> 'language': 'en',</tt><tt><br>
</tt><tt> 'value': 'test \
property'</tt><tt><br> </tt><tt> }</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><tt>params = {</tt><tt><br>
</tt><tt> 'action': 'wbeditentity',</tt><tt><br>
</tt><tt> 'new': 'property',</tt><tt><br>
</tt><tt> 'data': json.dumps(data),</tt><tt><br>
</tt><tt> 'summary': 'my edit summary',</tt><tt><br>
</tt><tt> 'token': site.tokens['edit']</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><tt>req = site._simple_request(**params)</tt><tt><br>
</tt><tt>print(req.submit())</tt><div><div class="h5"><br>
<br>
<div><br>
Il 23/03/2016 14:57, comfortably numb ha scritto:<br>
</div>
</div></div><blockquote type="cite"><div><div class="h5">
<div dir="ltr">
<div>Hi pywikibot experts,<br>
</div>
<div><br>
</div>
<div>How can I create new properties via pywikibot? (I'm trying
to do it via bot, because I'm doing some experiments on a
dedicated wikibase installation with - possibly - hundreds of
properties to be created... and Pywikibot would certainly be
my favorite tool!)</div>
<div><br>
</div>
<div>In case I have, instead, to directly wrap the action
"wbeditentity" from mediawiki API ( <a \
href="https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity" \
target="_blank">https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity</a>
), are there some Python examples?</div>
<div><br>
</div>
<div>And, in case I have to use the php script
"importProperties.php" ( <a \
href="https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/importProperties.php" \
target="_blank">https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/importProperties.php</a> \
), how can I manage properties more complex than the ones
contained in the example ( <a \
href="https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/en-elements-properties.csv" \
target="_blank">https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/en-elements-properties.csv</a> \
)?</div> <div><br>
</div>
<div><br>
</div>
<div>Using pywikibot, I'm able to MODIFY existing properties
with instructions like the following ones (which let me
generate the object-content in one shot via json ... as I
need):<br>
</div>
<div><br>
</div>
<div>
<div><font face="monospace, monospace" size="1">In [1]: import
pywikibot ; site = pywikibot.Site() ; repo =
site.data_repository()</font></div>
<div><font face="monospace, monospace" size="1">In [2]:
property_page = pywikibot.PropertyPage(repo, u"P2")<br>
</font></div>
<div><font face="monospace, monospace" size="1">In [3]: myjson
= {u'descriptions': {u'en': {u'language': \
u'en', u'value':
u'invented description'}}, u'labels': {u'en':
{u'language': u'en', u'value': u'test \
property'}}}<br> </font></div>
<div><font face="monospace, monospace" size="1">In [4]:
property_page.editEntity(myjson)</font><br>
</div>
</div>
<div><br>
</div>
<div>...but I cannot CREATE new properties (instantiating a
PropertyPage object), because pywikibot asks for the
identifier of an existing instance:</div>
<div><br>
</div>
<div>
<div><font face="monospace, monospace" size="1">In [11]:
p_page = pywikibot.PropertyPage(repo)</font></div>
<div><font face="monospace, monospace" \
size="1">---------------------------------------------------------------------------</font></div>
<div><font face="monospace, monospace" size="1">InvalidTitle
Traceback (most recent call
last)</font></div>
<div><font face="monospace, monospace" \
size="1"><ipython-input-11-e72b812b8cd3> in <module>()</font></div>
<div><font face="monospace, monospace" size="1">----> 1
p_page = pywikibot.PropertyPage(repo)</font></div>
<div><font face="monospace, monospace" size="1"><br>
</font></div>
<div><font face="monospace, monospace" \
size="1">/home/user/src/pywikibot_repo/pywikibot/page.pyc in __init__(self, source, \
title)</font></div>
<div><font face="monospace, monospace" size="1"> 4027
if not title or not self.id.startswith('P'):</font></div>
<div><font face="monospace, monospace" size="1"> 4028
raise pywikibot.InvalidTitle(</font></div>
<div><font face="monospace, monospace" size="1">-> 4029
u"'%s' is not an property page \
title" % title)</font></div>
<div><font face="monospace, monospace" size="1"> 4030
Property.__init__(self, source, <a href="http://self.id" \
target="_blank">self.id</a>)</font></div>
<div><font face="monospace, monospace" size="1"> 4031 </font></div>
<div><font face="monospace, monospace" size="1"><br>
</font></div>
<div><font face="monospace, monospace" size="1">InvalidTitle:
'' is not an property page title</font></div>
<div><font face="monospace, monospace" size="1"><br>
</font></div>
<div><font face="monospace, monospace" size="1">In [12]:</font></div>
</div>
<div><br>
</div>
<div>In fact, as I understand, in the source code of the
"WikibasePage" class, I see that</div>
<div>while for the Item type, a "Special case for empty item" is
mentioned ( <a \
href="https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L3760" \
target="_blank">https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L3760</a> \
)</div> <div><br>
</div>
<div>
<div><font face="monospace, monospace" size="1"> #
Special case for empty item.</font></div>
<div><font face="monospace, monospace" size="1"> if
title is None or title == '-1':</font></div>
<div><font face="monospace, monospace" size="1">
super(ItemPage, self).__init__(site, u'-1', ns=ns)</font></div>
<div><font face="monospace, monospace" size="1">
assert <a href="http://self.id" target="_blank">self.id</a> == \
'-1'</font></div>
<div><font face="monospace, monospace" size="1">
return</font></div>
</div>
<div><br>
</div>
<div>...for the Property type, instead, an empty object is NOT
allowed ( <a \
href="https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L4168" \
target="_blank">https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L4168</a> \
)</div> <div><br>
</div>
<div>
<div><font face="monospace, monospace" size="1"> if not
title or not self.id.startswith('P'):</font></div>
<div><font face="monospace, monospace" size="1">
raise pywikibot.InvalidTitle(</font></div>
<div><font face="monospace, monospace" size="1">
u"'%s' is not an property page title" % \
title)</font></div> <div><font face="monospace, monospace" size="1">
Property.__init__(self, source, <a href="http://self.id" \
target="_blank">self.id</a>)</font></div> </div>
<div><br>
</div>
<div><br>
</div>
<div>Thanks a lot for your attention!</div>
</div>
<br>
<fieldset></fieldset>
<br>
</div></div><span class=""><pre>_______________________________________________
pywikibot mailing list
<a href="mailto:pywikibot@lists.wikimedia.org" \
target="_blank">pywikibot@lists.wikimedia.org</a> <a \
href="https://lists.wikimedia.org/mailman/listinfo/pywikibot" \
target="_blank">https://lists.wikimedia.org/mailman/listinfo/pywikibot</a> </pre>
</span></blockquote>
<br>
</div>
<br>_______________________________________________<br>
pywikibot mailing list<br>
<a href="mailto:pywikibot@lists.wikimedia.org">pywikibot@lists.wikimedia.org</a><br>
<a href="https://lists.wikimedia.org/mailman/listinfo/pywikibot" rel="noreferrer" \
target="_blank">https://lists.wikimedia.org/mailman/listinfo/pywikibot</a><br> \
<br></blockquote></div><br></div>
[Attachment #6 (text/plain)]
_______________________________________________
pywikibot mailing list
pywikibot@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikibot
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic