goldsmith / Wikipedia

A Pythonic wrapper for the Wikipedia API
https://wikipedia.readthedocs.org/
MIT License
2.88k stars 519 forks source link

Error with accented chars in search term: KeyError: u'fullurl' #68

Closed alphapapa closed 9 years ago

alphapapa commented 9 years ago

e.g.:

import sys
import wikipedia as wp

s = wp.summary(str(sys.argv[1:]))

Then running script.py "Après" fails with:

Traceback (most recent call last):
  File "/home/me/.bin/w", line 25, in <module>
    s = wp.summary(str(sys.argv[1:]))
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/util.py", line 28, in __call__
    ret = self._cache[key] = self.fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 231, in summary
    page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 276, in page
    return WikipediaPage(title, redirect=redirect, preload=preload)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 299, in __init__
    self.__load(redirect=redirect, preload=preload)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 398, in __load
    self.url = page['fullurl']
KeyError: u'fullurl'
goldsmith commented 9 years ago

Hmm, sounds like a bash unicode parsing error. The following works fine for me directly from the REPL:

>>> wp.summary("Après")
u'Apr\xe8s is a studio album of mostly French covers by Iggy Pop. It was released on 9 May 2012 on Virgin Records.'
alphapapa commented 9 years ago

Ok, I finally figured out the problem:

>>> type(sys.argv[1:])
<type 'list'>
>>> type(sys.argv[1:][0])
<type 'str'>

So using, e.g.:

wp.summary(' '.join(sys.argv[1:]))

works.

So I guess one of the functions in the backtrace should either check the datatype or convert its argument to a string, because the exception currently raised gives no clue as to the problem.