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

Add categorymembers query #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions wikipedia/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,37 @@ def page(title=None, pageid=None, auto_suggest=True, redirect=True, preload=Fals
raise ValueError("Either a title or a pageid must be specified")


def category_members(category=None, pageid=None, cmlimit=10, cmtype='page'):
'''
Get list of page titles belonging to a category.

Keyword arguments:

* cmlimit - the maximum number of titles to return
* cmtype - which type of page to include. ("page", "subcat", or "file")
'''

if category is not None:
query_params = {
'list': 'categorymembers',
'cmtitle': 'Category:' + category,
'cmlimit': str(cmlimit),
'cmtype': cmtype
}
elif pageid is not None:
query_params = {
'list': 'categorymembers',
'cmpageid': pageid,
'cmlimit': str(cmlimit),
'cmtype': cmtype
}
else:
raise ValueError("Either a category or a pageid must be specified")

request = _wiki_request(query_params)

return map(lambda member: member['title'], request['query']['categorymembers'])


class WikipediaPage(object):
'''
Expand Down