Tell me more ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

Now on PyPI!

You can now find Stack.PY on PyPI, Python's package index. This means that you can install the package simply by running the following command in a terminal:

pip install stackpy

enter image description here

About

Stack.PY is based heavily on Stack.PHP and Stack.JS, taking the chained-method concept and applying it to Python. The end result is an extremely easy to use module, named stackpy:

from stackpy import API, Site

# Print the names of all Stack Exchange sites
for site in API.sites:
    print site['name']

# Grab the first question on Stack Overflow
print Site('stackoverflow').questions[0].title

Here are some of the other features you can expect from Stack.PY:

  • Caching: currently Stack.PY ships with an SQLite database backend (used for caching currently). By default, if no cache is set, Stack.PY creates an in-memory SQLite database to cache requests for the current session.
  • Full documentation: using a single command (see the README file) you can generate all of the documentation for the entire module - including an explanation for each parameter of every method.

Many more features are planned:

  • A test suite.
  • A series of examples (currently there is one really primitive example).
  • Full support for rate-limiting and the backoff response.

Stack.PY should run perfectly fine in Python 3k using 2to3.

License

Stack.PY is released under the MIT License.

Contact

I can be reached at admin@quickmediasolutions.com.

Code

The code for Stack.PY is hosted here on Launchpad. You can check out the latest code using:

bzr init
bzr pull lp:stackpy

You can view the code online here.

Stack.PY uses distutils so you can install the module by running:

python setup.py install

...or... if you are using Ubuntu, you can add my PPA and install the appropriate package:

sudo apt-get install python-stackpy (for Python 2k)
sudo apt-get install python3-stackpy (for Python 3k)

share|improve this question
Great work George! – Zagorulkin Dmitry Jul 25 '12 at 9:07
nice work! But can you tell how to make a API.begin_explicit call? I can't find an example of this. – Thomas15v Mar 2 at 20:40
@Thomas15v: I've added an explanation and example in an answer below. – Nathan Osman Mar 2 at 21:19

3 Answers

Example - Downloading a question with its body

from stackpy import Site

site = Site('stackoverflow')
question = site.questions(1732348).filter('withbody')[0]

print '--- %s ---' % question.title
print question.body
share|improve this answer

Example of Explicit Authentication:

Explicit authentication is relatively straightforward with Stack.PY. Assuming you have already registered your application, the instructions look something like this:

  1. Visit your application's page to obtain the following information:

    • key
    • client ID
    • client secret

  2. Provide Stack.PY with the above information:

    from stackpy import API
    
    # Fill in the strings with the appropriate values:
    API.key, API.client_id, API.client_secret = "", "", ""
    
  3. The API.begin_explicit() call consists of the following parameters:

    • a string consisting of the required privileges separated by commas
    • the URI to redirect the user to when the authorization completes
    • an optional string value to be returned with the access token when authorization completes

    Example:

    redirect_uri = API.begin_explicit('read_inbox,no_expiry',
                                      'http://example.com/done')
    

    The return value of the function is the URL that you will need to redirect the user to. That page will allow the user to authorize your application.

  4. The API.complete_explicit() call consists of the following parameters:

    • the value of the GET parameter code
    • the URI you provided to begin_explicit() above

    Example:

    access_token = API.complete_explicit(request.GET['code'],
                                         'http://example.com/done')
    

    The return value is the access token for the user.

share|improve this answer

Do you know, offhand, how complete this library is? Are there parts of the StackExchange API v2.1 that you know are not covered yet?

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged