2

I try to use the StackExchange API and I found the Py-StackExchange library for Python. I installed it through easy_install in Windows.

Here is the code:

from stackexchange import Site, StackOverflow

so = Site(StackOverflow)
my_favourite_guy = so.user(2309097)
print my_favourite_guy.reputation.format()
print len(my_favourite_guy.answers), 'answers'

And here is the error:

Traceback (most recent call last):
  File "C:\Users\Tasos\Desktop\test - Copy.py", line 8, in <module>
    my_favourite_guy = so.user(2309097)
  File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 626, in user
    u, = self.users((nid,), **kw)
  File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 631, in users
    return self._get(User, ids, 'users', kw)
  File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 621, in _get
    return self.build(root, typ, coll, kw)
  File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 598, in build
    json = self._request(url, kw)
  File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 570, in _request
    json, info = request_mgr.json_request(url, new_params)
  File "build\bdist.win-amd64\egg\stackexchange\web.py", line 120, in json_request
    return (json.loads(req.data), req.info)
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I saw in Wiki the following but I don't use any proxy and the code version is the latest through easy_install:

This is probably the result of some proxy/router mangling with request headers. It could be that your router/proxy adds headers requesting gzip data, but doesn't decompress it, and that you are running a slightly old version of the code which does not deal with gzip compression. In this case, just update to the latest version of the library.

Tasos
  • 7,325
  • 18
  • 83
  • 176

1 Answers1

2

The version on PyPI is well out of date (released 2011) and still uses API version 1.1, which has been shut down.

The Github codebase has been updated to use API v2.2, install that directly:

pip install git+https://github.com/lucjon/Py-StackExchange

or using easy_install, download the current master zip:

easy_install https://github.com/lucjon/Py-StackExchange/archive/640eac1525baaf57474ddbc3be2b580f00e4f1e8.zip

To get answers listed you need to call .fetch():

print len(my_favourite_guy.answers.fetch()), 'answers'

This only fetches the first page of answers:

>>> from stackexchange import Site, StackOverflow
>>> so = Site(StackOverflow)
>>> my_favourite_guy = so.user(2309097)
>>> print my_favourite_guy.reputation.format()
563
>>> print len(my_favourite_guy.answers.fetch()), 'answers'
19 answers
>>> my_favourite_guy = so.user(100297)
>>> print my_favourite_guy.reputation.format()
251.2k
>>> print len(my_favourite_guy.answers.fetch()), 'answers'
30 answers

I have a few more than 30 answers, last time I checked. Use .extend_next() calls to fetch the next query set, until you run out.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343