I am using PRAW for Reddit API in a Python/GTK application. I have been successful in using the API, but I can’t seem to be able to decode the JSON for use. It should be known that I am a beginner in Python and GTK applications.
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE
import gettext
from gettext import gettext as _
gettext.textdomain('redditreader')
from gi.repository import Gtk # pylint: disable=E0611
import logging
logger = logging.getLogger('redditreader')
from redditreader_lib import Window
from redditreader.AboutRedditreaderDialog import AboutRedditreaderDialog
from redditreader.PreferencesRedditreaderDialog import PreferencesRedditreaderDialog
import praw
import json
import simplejson
from pprint import pprint
# See redditreader_lib.Window.py for more details about how this class works
class RedditreaderWindow(Window):
__gtype_name__ = "RedditreaderWindow"
def finish_initializing(self, builder): # pylint: disable=E1002
"""Set up the main window"""
super(RedditreaderWindow, self).finish_initializing(builder)
self.AboutDialog = AboutRedditreaderDialog
self.PreferencesDialog = PreferencesRedditreaderDialog
# Code for other initialization actions should be added here.
r = praw.Reddit(user_agent='example')
try:
submissions = r.get_front_page(limit=5)
[str(x) for x in submissions]
jsondatafirst = simplejson.loads(str(submissions))
jsondata = unicode(jsondatafirst, 'utf-8')
print(jsondata)
except (simplejson.decoder.JSONDecodeError, ValueError):
print 'Decoding JSON has failed'
With PRAW you do not need to do any json decoding as PRAW handles all of that for you.
Say for example for each submission you want to print out the number of upvotes, the number of downvotes, and the submission title. You could do:
If you want to see all the attributes available to use on a submission object you can run:
Additionally if you want to get the comments from a submission then you can use the
submission.commentsproperty. You can also manually look at the json response for a request to see what attributes should be available through PRAW (example).The attributes are not explicitly listed anywhere for the objects because the attributes are created directly from whatever the key name is in the associated json response for the request.