Skip to content

Commit

Permalink
Merge pull request #24 from lehinevych/categorymembers
Browse files Browse the repository at this point in the history
Categorymembers
  • Loading branch information
lehinevych committed Jun 22, 2018
2 parents 4dd3341 + e6ca9c0 commit 5e6b444
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
5 changes: 5 additions & 0 deletions docs/source/changelog.rst
Expand Up @@ -11,6 +11,11 @@ Contents:

changelog

Master
=======

* Add `category member <https://www.mediawiki.org/wiki/API:Categorymembers>`_ query support by `PR #24 <https://github.com/lehinevych/MediaWikiAPI/pull/24>`_

Version 1.1.2
=============

Expand Down
6 changes: 3 additions & 3 deletions docs/source/conf.py
Expand Up @@ -20,7 +20,7 @@
import sys
sys.path.insert(0, os.path.abspath('../..'))
from datetime import datetime

from mediawikiapi.version import __version__ as version
# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
Expand Down Expand Up @@ -57,9 +57,9 @@
# built documents.
#
# The short X.Y version.
version = u'1.1'
# version = u'1.1.2'
# The full version, including alpha/beta/rc tags.
release = u'1.0'
release = u'1.0.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Expand Up @@ -12,7 +12,7 @@ Release v\ |version|.
:target: https://pypi.python.org/pypi/mediawikiapi

.. image:: https://travis-ci.org/lehinevych/MediaWikiAPI.svg?branch=master
:target: https://travis-ci.org/leginevych/MediaWikiAPI
:target: https://travis-ci.org/lehinevych/MediaWikiAPI

.. image:: https://img.shields.io/github/issues/lehinevych/MediaWikiAPI.svg
:target: https://github.com/lehinevych/MediaWikiAPI/issues
Expand Down
68 changes: 51 additions & 17 deletions mediawikiapi/mediawikiapi.py
Expand Up @@ -212,24 +212,58 @@ def page(self, title=None, pageid=None, auto_suggest=True, redirect=True, preloa
raise ValueError("Either a title or a pageid must be specified")

def languages(self):
'''
List all the currently supported language prefixes (usually ISO language code).
Can be inputted to WikipediaPage.conf to change the Mediawiki that `wikipedia` requests
results from.
Returns: dict of <prefix>: <local_lang_name> pairs. To get just a list of prefixes,
use `wikipedia.languages().keys()`.
'''
response = self.session.request({
'meta': 'siteinfo',
'siprop': 'languages'
}, self.config)
languages = response['query']['languages']
return {
lang['code']: lang['*']
for lang in languages
'''
List all the currently supported language prefixes (usually ISO language code).
Can be inputted to WikipediaPage.conf to change the Mediawiki that `wikipedia` requests
results from.
Returns: dict of <prefix>: <local_lang_name> pairs. To get just a list of prefixes,
use `wikipedia.languages().keys()`.
'''
response = self.session.request({
'meta': 'siteinfo',
'siprop': 'languages'
}, self.config)
languages = response['query']['languages']
return {
lang['code']: lang['*']
for lang in languages
}

def category_members(self, title=None, pageid=None, cmlimit=10, cmtype='page'):
'''
Get list of page titles belonging to a category.
Keyword arguments:
* title - category title. Cannot be used together with "pageid"
* pageid - page id of category page. Cannot be used together with "title"
* cmlimit - the maximum number of titles to return
* cmtype - which type of page to include. ("page", "subcat", or "file")
'''
if title is not None and pageid is not None:
raise ValueError("Please specify only a category or only a pageid, only one param can be specified")
elif title is not None:
query_params = {
'list': 'categorymembers',
'cmtitle': 'Category:{}'.format(title),
'cmlimit': str(cmlimit),
'cmtype': cmtype
}
elif pageid is not None:
query_params = {
'list': 'categorymembers',
'cmpageid': str(pageid),
'cmlimit': str(cmlimit),
'cmtype': cmtype
}
else:
raise ValueError("Either a category or a pageid must be specified")

response = self.session.request(query_params, self.config)
if 'error' in response:
raise ValueError(response['error'].get('info'))
return [member['title'] for member in response['query']['categorymembers']]

def donate(self):
'''
Expand Down
12 changes: 11 additions & 1 deletion tests/page_test.py
Expand Up @@ -31,7 +31,6 @@ def test_redirect_true(self):
def test_redirect_false(self):
"""Test that page raises an error on a redirect when redirect == False."""
mp = api.page("Template:cn", auto_suggest=False, redirect=False)
# self.assertRaises(mediawikiapi.RedirectError, mp)
self.assertIsInstance(mp, mediawikiapi.WikipediaPage)

def test_redirect_no_normalization(self):
Expand Down Expand Up @@ -189,3 +188,14 @@ def test_pageprops(self):
"""Test pageprops of a page"""
self.assertEqual(self.celtuce.pageprops, mock_data['data']["celtuce.pageprops"])
self.assertEqual(self.cyclone.pageprops, mock_data['data']["cyclone.pageprops"])

def test_category_members(self):
"""Test category members"""
self.assertEqual(api.category_members(title='Physics'), mock_data['data']["category_members_physics"])
self.assertEqual(api.category_members(pageid=692318), mock_data['data']["category_members_physics"])
with self.assertRaises(ValueError):
api.category_members(title='Wikipedia', pageid=6923181)
with self.assertRaises(ValueError):
api.category_members(pageid=6923181)
with self.assertRaises(ValueError):
api.category_members(title='')
6 changes: 4 additions & 2 deletions tests/request_mock_data.py
Expand Up @@ -79,8 +79,8 @@
<li id="cite_note-aholic-7"><span class="mw-cite-backlink">^ <a href="#cite_ref-aholic_7-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-aholic_7-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-aholic_7-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text"><cite class="citation web"><a rel="nofollow" class="external text" href="http://www.seedaholic.com/celtuce-wo-sun.html">"Celtuce, 'Wo Sun<span style="padding-right:0.2em;">'</span>"</a>. seedaholic.com<span class="reference-accessdate">. Retrieved <span class="nowrap">28 January</span> 2017</span>.</cite><span title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=unknown&amp;rft.btitle=Celtuce%2C+%27Wo+Sun%27&amp;rft.pub=seedaholic.com&amp;rft_id=http%3A%2F%2Fwww.seedaholic.com%2Fceltuce-wo-sun.html&amp;rfr_id=info%3Asid%2Fen.wikipedia.org%3ACeltuce" class="Z3988"><span style="display:none;">&#160;</span></span></span>
</li>
</ol></div></div>
<div role="navigation" class="navbox" aria-label="Navbox" style="padding:3px"><table class="nowraplinks hlist navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tr><th scope="row" class="navbox-group" style="width:1%;text-align: left;"><a href="/wiki/Help:Taxon_identifiers" title="Help:Taxon identifiers">Taxon identifiers</a></th><td class="navbox-list navbox-odd" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><span style="white-space:nowrap;"><a href="/wiki/Wikidata" title="Wikidata">Wd</a>: <span class="uid"><span class="external"><a href="https://www.wikidata.org/wiki/Q574172" class="extiw" title="wikidata:Q574172">Q574172</a></span></span></span></li>
<div role="navigation" class="navbox" aria-labelledby="Taxon_identifiers" style="padding:3px"><table class="nowraplinks hlist navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tr><th id="Taxon_identifiers" scope="row" class="navbox-group" style="width:1%;text-align: left;"><a href="/wiki/Help:Taxon_identifiers" title="Help:Taxon identifiers">Taxon identifiers</a></th><td class="navbox-list navbox-odd" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><span style="white-space:nowrap;"><a href="/wiki/Wikidata" title="Wikidata">Wikidata</a>: <span class="uid"><span class="external"><a href="https://www.wikidata.org/wiki/Q574172" class="extiw" title="wikidata:Q574172">Q574172</a></span></span></span></li>
<li><span style="white-space:nowrap;"><a href="/wiki/EPPO_Code" title="EPPO Code">EPPO</a>: <span class="uid"><a rel="nofollow" class="external text" href="https://gd.eppo.int/taxon/LACSG">LACSG</a></span></span></li>
<li><span style="white-space:nowrap;"><a href="/wiki/Global_Biodiversity_Information_Facility" title="Global Biodiversity Information Facility">GBIF</a>: <span class="uid"><a rel="nofollow" class="external text" href="https://www.gbif.org/species/6711035">6711035</a></span></span></li>
<li><span style="white-space:nowrap;"><a href="/wiki/Germplasm_Resources_Information_Network" title="Germplasm Resources Information Network">GRIN</a>: <span class="uid"><a rel="nofollow" class="external text" href="https://npgsweb.ars-grin.gov/gringlobal/taxonomydetail.aspx?id=404620">404620</a></span></span></li>
Expand Down Expand Up @@ -346,5 +346,7 @@
"great_wall_of_china.geo_seach_with_non_existing_article_name": [],

"bill_foster_page.images": ['https://upload.wikimedia.org/wikipedia/commons/b/bd/Bill_Foster%2C_Official_Portrait%2C_113th_Congress.jpg'],

"category_members_physics": ['Physics', 'Outline of physics', 'Glossary of classical physics', 'Portal:Physics', 'Action angle coordinates', 'Aerometer', 'Bayesian model of computational anatomy', 'Group actions in computational anatomy', 'Blasius–Chaplygin formula', 'Cabbeling'],
}
}

0 comments on commit 5e6b444

Please sign in to comment.