Search code examples
pythonjsonapireddit

What are the different sorting types allowed in Reddit's JSON API?


I came across a function that fetches the top images from reddit, with its API.

sr='pics'
sorting=''
url = r'http://www.reddit.com/r/{sr}/{top}.json'.format(sr=sr, top=sorting)

However I found that early on the original coder posted something like this:

#'sorting' is whether or not the sorting of the reddit should be customized or not,
# if it is: Allowed passing params/queries such as t=hour, week, month, year or all"""

I'm not sure about how to use these other 'sorting' commands or where I can find more on how to use them. I'm not even sure if I should look more about the 'sorting' as part of json/reddit/python notation.

What I'd like to do is to not only get the top images which are defined by sorting='', but the bottom images as well (sort other way around, if possible) through another query.


Solution

  • If you go to a subreddit normally and click on top and then links from 'this week' the URL is

    http://www.reddit.com/r/pics/top/?sort=top&t=week

    If I change top/ to top.json

    http://www.reddit.com/r/pics/top.json?sort=top&t=week

    I get the JSON equivalent. However, controversial links from the week URL is

    http://www.reddit.com/r/pics/controversial/?sort=controversial&t=week

    which becomes

    http://www.reddit.com/r/pics/controversial.json?sort=controversial&t=week

    So it does not appear that getting the bottom is possible from this test.

    Like @Martijn commented. This is an reddit API issue and not a Python or JSON issue.