Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wikibase client (read only) #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PierreSelim
Copy link
Contributor

I've been working on implementation of reading method of the wikibase API:

  • wikibase_item() in the page module
  • WikiBaseSite and entities() in the client module
  • entity module. The Claim class is mainly an helper to access dictionnary returned by wbgetclaims or wbgetentities method from API

Example of how it works:

import mwclient

site = mwclient.Site(('https', 'en.wikipedia.org'))
article = site.Pages['Paris']
item = article.wikibase_item()

# wikibase repo
wikidata = site.wikibase()
print wikidata

# Claims
claims = item.claims()
for claim in claims:
    print claim
    print claim.value

# Only coordinates
for claim in item.claims(prop=['P625']):
    print "%f, %f" % (claim.latitude, claim.longitude)

# Only subdivision (Paris subdivision are Items)
for claim in item.claims(prop=['P150']):
    print claim.item

wikidata = mwclient.WikiBaseSite(('https', 'www.wikidata.org'))
entities = wikidata.entities(ids=['Q42', 'P238'])
for entity in entities:
    print entity
    if entity.entity == 'P238':
        for claim in entity.claims(prop=['P1659']):
            print claim.property_value
# <Property object P238 (('https', 'www.wikidata.org'))>
# <Property object P239 (('https', 'www.wikidata.org'))>
# <Property object P229 (('https', 'www.wikidata.org'))>
# <Item object Q42 (('https', 'www.wikidata.org'))>

@landscape-bot
Copy link

Code Health
Repository health increased by 0.36% when pulling a660b7d on PierreSelim:wikibase into 2a42d77 on mwclient:master.

@danmichaelo
Copy link
Member

Thanks for pushing this! I'll try to read over the code in the weekend.

@PierreSelim
Copy link
Contributor Author

@danmichaelo do not hesitate to comment things that you don't like in my code :)

@danmichaelo
Copy link
Member

When doing tab completion on the Site object, it's a bit confusing to have both wikibase and wikibase_site. I'll suggest prefixing the latter with _. And btw. what about making it a property? It's about time we start using properties in mwclient – and I noticed you've already used properties some places :) How about this?

    @property
    def wikibase_repo(self):
        """
        Get a reference to the associated Wikibase repository site.

        Returns:
            a WikiBaseSite object
        """
        if self._wikibase_repo is None:
            result = self.api('query', meta='wikibase')
            url = result['query']['wikibase']['repo']['url']
            host = url['base'].replace('//', '')
            path = url['scriptpath'] + "/"
            self._wikibase_repo = WikiBaseSite(('https', host),
                                              path=path,
                                              pool=self.connection)
        return self._wikibase_repo

I guess Page.wikibase_item could also be a property

self._labels = None

# caching claims
self.itemclaims = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix this one with underscore as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be the logic thing to do +1 :)

PierreSelim added a commit to PierreSelim/mwclient that referenced this pull request Aug 30, 2015
@@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="Pierre-Selim">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And please remove this file ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ashamed this got pushed ... I just rebased it ^_^

PierreSelim added a commit to PierreSelim/mwclient that referenced this pull request Aug 30, 2015
As discussed in mwclient#96 wikibase_item() and wikibase() are
good candidate for properties.
Typo: dictionnary -> dictionary
PierreSelim added a commit to PierreSelim/mwclient that referenced this pull request Sep 5, 2015
As discussed in mwclient#96 wikibase_item() and wikibase() are
good candidate for properties.
Typo: dictionnary -> dictionary
@waldyrious
Copy link
Member

@PierreSelim and @danmichaelo, since these things sometimes fall off our radar, I'm just pinging you in case there's something other than dev work (e.g. a comment to respond) stalling the discussion. What's the status, what needs to be done?

@PierreSelim
Copy link
Contributor Author

@waldyrious and @danmichaelo true this has totally fell off my radar, I'll probably re-read the review and try to adress what I can this week. Still annoys me to use pywikibot when I don't need to just to get few simple wikibase API results :)

@danmichaelo
Copy link
Member

Cool! I'll try to respond quickly if you post something. If you want to discuss anything more in detail, we could also do a Gitter chat.

PierreSelim added a commit to PierreSelim/mwclient that referenced this pull request Jul 20, 2018
As discussed in mwclient#96 wikibase_item() and wikibase() are
good candidate for properties.
Typo: dictionnary -> dictionary
@PierreSelim
Copy link
Contributor Author

@danmichaelo right now I'm rebasing and squashing for readability purpose :)

Wikibase and wikibase_item():
>>> import mwclient
>>> site = mwclient.Site(('https', 'fr.wikipedia.org'))
>>> p = site.Pages['Nicolas Sarkozy']
>>> p.wikibase_item()
<Item object Q329 (('https', u'www.wikidata.org'))>
>>> site.wikibase()
<WikiBaseSite object '('https', u'www.wikidata.org')/w/'>

Added entities() to WikiBaseSite
It works this way
>>> import mwclient
>>> site = mwclient.WikiBaseSite(('https', 'www.wikidata.org'))
>>> ent = site.entities('Q422')
>>> q422 = ent[0]
>>> q422
<Item object Q422 (('https', 'www.wikidata.org'))>
>>> q422.claims()

Fetching labels, descriptions and sitelinks for entities
>>> import mwclient
>>> site = mwclient.WikiBaseSite(('https', 'www.wikidata.org'))
>>> q = mwclient.entity.Item(site, 'Q42')
>>> q.sitelinks
>>> q.labels
>>> q.descriptions

ids filter for entities():
>>> import mwclient
>>> wikidata = mwclient.WikiBaseSite(('https', 'www.wikidata.org'))
>>> entities = wikidata.entities(ids=['Q42', 'P238'])
>>> for entity in entities:
>>>    print entity
<Property object P238 (('https', 'www.wikidata.org'))>
<Item object Q42 (('https', 'www.wikidata.org'))>

Property filter in claims():
>> import mwclient
>> site = mwclient.Site(('https', 'en.wikipedia.org'))
>> article = site.Pages['Nicolas Sarkozy']
>> item = article.wikibase_item()
>> #only distinction
>> for claim in item.claims(prop=['P166']):
>>     print claim
>> # All Claims
>> for claim in item.claims():
>>     print claim

Claim object have now both properties:
* `snaktype`
* `value` set to `None` if `snaktype` is either `somevalue` or `novalue`
@AdamWill
Copy link
Member

hey again! @waldyrious looped me into helping out with maintaining mwclient since dan is inactive ATM. I'm looking through old pull requests, obviously this one is pretty large and will take a minute to get familiar with, but if you're still interested, could you look at rebasing it? One thing I'd definitely personally want to request to merge this: tests! I'd hate to merge a major new feature with no tests. Do you think you could write some? Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants