User: added .get_rights() (working) and .exists() (skeleton). Page: added .exists() (skeleton); store text as ._content; get() has a force_reload argument. Category: fixed missing self in .get_members(). Site: self.__api -> self._api; self.__sql -> self._sqltags/v0.1^2
@@ -7,12 +7,12 @@ class Category(Page): | |||||
EarwigBot's Wiki Toolset: Category Class | EarwigBot's Wiki Toolset: Category Class | ||||
""" | """ | ||||
def get_members(limit=50): | |||||
def get_members(self, limit=50): | |||||
""" | """ | ||||
Docstring needed | Docstring needed | ||||
""" | """ | ||||
params = {"action": "query", "list": "categorymembers", "cmlimit": limit} | |||||
params["cmtitle"] = self.title | |||||
params = {"action": "query", "list": "categorymembers", | |||||
"cmlimit": limit, "cmtitle": self.title} | |||||
result = self.site.api_query(params) | result = self.site.api_query(params) | ||||
members = result['query']['categorymembers'] | members = result['query']['categorymembers'] | ||||
return [member["title"] for member in members] | return [member["title"] for member in members] |
@@ -11,14 +11,23 @@ class Page(object): | |||||
""" | """ | ||||
self.site = site | self.site = site | ||||
self.title = title | self.title = title | ||||
self._content = None | |||||
def get(self): | |||||
def exists(self): | |||||
""" | """ | ||||
Docstring needed | Docstring needed | ||||
""" | """ | ||||
params = {'action': 'query', 'prop': 'revisions', 'rvprop':'content', 'rvlimit':'1'} | |||||
params["titles"] = self.title | |||||
result = self.site.api_query(params) | |||||
pageid = result['query']['pages'].keys()[0] | |||||
content = result['query']['pages'][pageid]['revisions'][0]['*'] | |||||
return content | |||||
pass | |||||
def get(self, force_reload=False): | |||||
""" | |||||
Docstring needed | |||||
""" | |||||
if content is None or force_reload: | |||||
params = {"action": "query", "prop": "revisions", | |||||
"rvprop": "content", "rvlimit": 1, "titles": self.title} | |||||
result = self.site.api_query(params) | |||||
content = result["query"]["pages"].values()[0]["revisions"][0]["*"] | |||||
self._content = content | |||||
return content | |||||
return self._content |
@@ -20,8 +20,8 @@ class Site(object): | |||||
self.name = name | self.name = name | ||||
self.project = project | self.project = project | ||||
self.lang = lang | self.lang = lang | ||||
self.__api = api | |||||
self.__sql = sql | |||||
self._api = api | |||||
self._sql = sql | |||||
def api_query(self, params): | def api_query(self, params): | ||||
""" | """ | ||||
@@ -29,7 +29,7 @@ class Site(object): | |||||
""" | """ | ||||
params["format"] = "json" | params["format"] = "json" | ||||
data = urlencode(params) | data = urlencode(params) | ||||
result = urlopen(self.__api, data).read() | |||||
result = urlopen(self._api, data).read() | |||||
return loads(result) | return loads(result) | ||||
def get_page(self, pagename): | def get_page(self, pagename): | ||||
@@ -5,5 +5,28 @@ class User(object): | |||||
EarwigBot's Wiki Toolset: User Class | EarwigBot's Wiki Toolset: User Class | ||||
""" | """ | ||||
def __init__(self): | |||||
def __init__(self, site, username): | |||||
""" | |||||
Docstring needed | |||||
""" | |||||
self.site = site | |||||
self.username = username | |||||
def exists(self): | |||||
""" | |||||
Docstring needed | |||||
""" | |||||
pass | pass | ||||
def get_rights(self): | |||||
""" | |||||
Docstring needed | |||||
""" | |||||
params = {"action": "query", "list": "users", "usprop": "groups", | |||||
"ususers": self.username} | |||||
result = self.site.api_query(params) | |||||
try: | |||||
rights = res['query']['users'][0]['groups'] | |||||
except KeyError: # 'groups' not found, meaning the user does not exist | |||||
return None | |||||
return rights |