Reddit is one of the popular social networking, entertainment and news websites where registered members can submit content, such as text posts or direct links. It allows the registered users to vote the submissions either "up" or "down" to rank the posts on the site's pages. Each content entry is categorized by area of interest called SUBREDDITS.
In this section, we are going to access the reddit API directly, using the Python requests library. We are going to cover the topics of a basic overview of reddit API, getting data related to our own reddit account, and using the search API to retrieve the links.
The reddit API consists of four important parts that we need to get familiar with before starting to interact with it. The four parts are:
after
/before
, limit
, count
, show
.GET /api/me.json
.Registering a new account on reddit is easy. First, we need to reach the reddit site—https://www.reddit.com/, and then have to fill up the registration form which pops up when we click on sign in or create an account link in the top right corner. The Registration form includes:
Let us create a new account with a username and a password of our choice. For now, leave the email field empty. We are going to add it in the next section.
In the following examples, I'm assuming that the username and password we created before are OUR_USERNAME
and OUR_PASSWORD
respectively.
Now, let's add an email to our account's profile which we intentionally left undone while creating the account in the previous section.
>>> import requests >>> client = requests.session() >>> client.headers = {'User-Agent': 'Reddit API - update profile'}
DATA
attribute with the 'user'
, 'passwd'
and 'api type'
attributes.>>> DATA = {'user': 'OUR_USERNAME', 'passwd': 'OUR_PASSWORD', 'api type': 'json'}
post
request call to the URL—https://ssl.reddit.com/api/login with the login credentials stored in the DATA
attribute.>>> response = client.post('https://ssl.reddit.com/api/login', data=DATA)
response
variable. The response
object contains the data
and errors
information as shown in the following example:>>> print response.json() {u'json': {u'errors': [], u'data': {u'need_https': False, u'modhash': u'v4k68gabo0aba80a7fda463b5a5548120a04ffb43490f54072', u'cookie': u'32381424,2014-11-09T13:53:30,998c473d93cfeb7abcd31ac457c33935a54caaa7'}}}
modhash
value obtained in the previous response to perform an update call to change our email
. Now, let us call the reddit's update API as shown in the following example:>>> modhash = response.json()['json']['data']['modhash'] >>> update_params = {"api_type": "json", "curpass": "OUR_PASSWORD", ... "dest": "www.reddit.com", "email": "[email protected]", ... "verpass": "OUR_PASSWORD", "verify": True, 'uh': modhash} >>> r = client.post('http://www.reddit.com/api/update', data=update_params)
r
. If there are no errors, then the status_code
will be 200
and errors
attributes value will be an empty list as shown in the following example:>>> print r.status_code 200 >>> r.text u'{"json": {"errors": []}}'
email
field is set by getting info about the currently authenticated user. If the has_mail
attribute is True
, then we can assume that the email is successfully updated.>>> me = client.get('http://www.reddit.com/api/me.json') >>> me.json()['data']['has_mail'] True
We can use reddit's search API to search the entire site or in a subreddit. In this section we'll look at making a search API request. Proceed with the following steps to make a search request.
To make a search api call, we need to send a get request to http://www.reddit.com/search.json
url with a search query q
in the parameters.
>>> search = requests.get('http://www.reddit.com/search.json', params={'q': 'python'}) >>> search.json().keys() [u'kind', u'data'] >>> search.json()['data']['children'][0]['data'].keys() [u'domain', u'author', u'media', u'score', u'approved_by', u'name', u'created', u'url', u'author_flair_text', u'title' ... ]
The response to search is stored in the search
variable which is a requests.Response
object. The search results are stored in the children
attribute of the data
attribute. We can access title
, author
, score
or another item in the search results as shown in the following example:
>>> search.json()['data']['children'][0]['data']['title'] u'If you could change something in Python what would it be?' >>> search.json()['data']['children'][0]['data']['author'] u'yasoob_python' >>> search.json()['data']['children'][0]['data']['score'] 146
Searching in reddit's subreddits by title and description is same as searching in reddit. For that, we need to send a get request to http://www.reddit.com/search.json
URL with a search query q
in the parameters.
>>> subreddit_search = requests.get('http://www.reddit.com/subreddits/search.json', params={'q': 'python'})
The response to search is stored in the search
variable which is a requests.Response
object. The search results are stored in the data
attribute.
>>> subreddit_search.json()['data']['children'][0]['data']['title'] u'Python'
18.191.234.150