Skip to content

Commit

Permalink
fix PageError/RedirectionError/DisambiguationError throwing when crea…
Browse files Browse the repository at this point in the history
…ting WikipediaPage from pageid
  • Loading branch information
goldsmith committed Jan 25, 2014
1 parent 856afb5 commit 182a010
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
16 changes: 11 additions & 5 deletions wikipedia/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ def __str__(self):
class PageError(WikipediaException):
"""Exception raised when no Wikipedia matched a query."""

def __init__(self, page_title):
self.title = page_title
def __init__(self, pageid=None, *args):
if pageid:
self.pageid = pageid
else:
self.title = args[0]

def __unicode__(self):
return u"\"{0}\" does not match any pages. Try another query!".format(self.title)
if hasattr(self, 'title'):
return u"\"{0}\" does not match any pages. Try another query!".format(self.title)
else:
return u"Page id \"{0}\" does not match any pages. Try another id!".format(self.pageid)


class DisambiguationError(WikipediaException):
Expand All @@ -54,8 +60,8 @@ def __unicode__(self):
class RedirectError(WikipediaException):
"""Exception raised when a page title unexpectedly resolves to a redirect."""

def __init__(self, page_title):
self.title = page_title
def __init__(self, title):
self.title = title

def __unicode__(self):
return u"\"{0}\" resulted in a redirect. Set the redirect property to True to allow automatic redirects.".format(self.title)
Expand Down
26 changes: 20 additions & 6 deletions wikipedia/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ def load(self, redirect=True, preload=False):
'inprop': 'url',
'ppprop': 'disambiguation',
}
if getattr(self, 'pageid', None) is None:
if not getattr(self, 'pageid', None):
query_params['titles'] = self.title
else:
query_params['pageids'] = str(self.pageid)
query_params['pageids'] = self.pageid

request = _wiki_request(**query_params)

Expand All @@ -268,7 +268,10 @@ def load(self, redirect=True, preload=False):

# missing is equal to empty string if it is True
if data.get('missing') == '':
raise PageError(self.title)
if hasattr(self, 'title'):
raise PageError(self.title)
else:
raise PageError(pageid=self.pageid)

# same thing for redirect
elif data.get('redirect') == '':
Expand All @@ -291,23 +294,34 @@ def load(self, redirect=True, preload=False):
self.__init__(title, redirect=redirect, preload=preload)

else:
raise RedirectError(self.title)
raise RedirectError(getattr(self, 'title', data['title']))

# since we only asked for disambiguation in ppprop,
# if a pageprop is returned,
# then the page must be a disambiguation page
elif data.get('pageprops'):
request = _wiki_request(titles=self.title, prop='revisions', rvprop='content', rvparse='', rvlimit=1)
query_params = {
'prop': 'revisions',
'rvprop': 'content',
'rvparse': '',
'rvlimit': 1
}
if hasattr(self, 'pageid'):
query_params['pageids'] = self.pageid
else:
query_params['titles'] = self.title
request = _wiki_request(**query_params)
html = request['query']['pages'][pageid]['revisions'][0]['*']

lis = BeautifulSoup(html).find_all('li')
filtered_lis = [li for li in lis if not 'tocsection' in ''.join(li.get('class', []))]
may_refer_to = [li.a.get_text() for li in filtered_lis if li.a]

raise DisambiguationError(self.title, may_refer_to)
raise DisambiguationError(getattr(self, 'title', data['title']), may_refer_to)

else:
self.pageid = pageid
self.title = data['title']
self.url = data['fullurl']

def html(self):
Expand Down

0 comments on commit 182a010

Please sign in to comment.