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 <[email protected]>
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 
> [email protected]https://lists.wikimedia.org/mailman/listinfo/pywikibot
>
>
>
> _______________________________________________
> pywikibot mailing list
> [email protected]
> https://lists.wikimedia.org/mailman/listinfo/pywikibot
>
>
_______________________________________________
pywikibot mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot

Reply via email to