-
Notifications
You must be signed in to change notification settings - Fork 76
Home
lucjon edited this page Sep 14, 2010
·
28 revisions
Welcome to the Py-StackExchange wiki!
New API Version
A new API version (1.0) has been released. As far as I’m aware, there aren’t really any changes, but if you encounter any difficulties, just pull from the 0.9
branch instead.
- Fix/investigate this bug
- Implement search and post revisions
-
Convert keyword arguments to correct strings (i.e.,done.('hello', 'world')
becomes'hello;world'
,10
becomes'10'
). -
Look at adding support for gzip-compressed responsesdone.
Before checking here, I’d suggest updating to the latest version of the library, either through a git pull
or downloading a ZIP or tarball and extracting it over the existing files – bugs are being fixed all the time.
-
I’m getting an ImportError on
simplejson
. Why?
This is because you are running a version of Python below 2.6 (i.e., you don’t have the in-built JSON module.) and don’t have thesimplejson
library installed. (Or you’re just weird and like messing about with your Python install. Like me.) The solution is generally to installsimplejson
from PyPI (easy_install simplejson
.)
simplejson
there, it’s just under the django.utils
namespace. To use it in Py-StackExchange, edit the stackweb.py file and change the line:
import simplejson as json
from django.utils import simplejson as json
-
Why do I get an empty list on
user.answers
oruser.questions
orbadge.recipients
etc.?
To reduce the number of API calls the library makes, collections which would require another request to populate require explicit ‘fetching’ of the data, with, for exampleuser.answers.fetch()
. This will return the list with the new items, and also update the property on the original object. -
Why does
answer.body
orquestion.body
raise anAttributeError
?
Question and answer bodies must be explicitly fetched (through the specification of a parameter) to save bandwidth. (This is a restriction ‘imposed’ by the API.) To do this, you can either use thebody='true'
keyword argument to your request:
so = stackexchange.Site(stackexchange.StackOverflow)
q = so.question(4, body='true')
user.answers
), call so.be_inclusive()
before making any requests.
-
Why do I get a
ValueError: No JSON object could be decoded
message?
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.
Otherwise, please send me a stack trace and any other details you have through StackApps – just submit an answer. -
What’s the Py-StackExchange equivalent of the URL
users/41981/questions
?
Just usesite.{questions|answers|etc}(user_id=41981)
. -
Why do I get a ‘not a gzip file’ or similar error?
Something could be disposing of, or decompressing, the gzip headers or data respectively. In this case, before making your first request, usesite.use_gzip = False
.
If this doesn’t solve the problem, send me a bug report through StackApps (see previous question). -
fetch_next()
returns an empty tuple where it shouldn’t. Sometimes it works if the code is moved somewhere else.
I’m not exactly sure what’s going on here, but try rearranging your code if your problem matches this description.
Otherwise, send me a bug report through StackApps. -
How do I format dates like they do on the site — like ‘3 seconds ago’ (or for Jon Skeet, ‘-∞ seconds ago’)?
If the last part isn’t an absolute requirement, you can use thestack{exchange|auth}.format_relative_date(date)
function. Just pass in the date from the post object, and it’ll give you back a string.
if post.owner_id == 22656:
return u'-∞ seconds ago'
else:
return stackexchange.format_relative_date(post.creation_date)
-
Can I be defensive against request throttling by the API?
Yes. Just add theso.impose_throttling = True
property to your Site object. This will (in theory) raise an error when you make more than 30 requests in 5 seconds.
so.throttle_stop = False
. This might not always work as expected, however. For more information on API throttling, see: http://stackapps.com/questions/1143/request-throttling-limits.