Interacting with reddit

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.

Getting started with the reddit API

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:

  1. listings: The endpoints in reddit are called listings. They contain parameters like after/before, limit, count, show.
  2. modhashes: This is a token which is used to prevent the cross site request forgery(CSRF) exploit. We can get the modhash for us by using GET /api/me.json.
  3. fullnames: A fullname is a combination of a thing's type and its unique ID which forms a compact encoding of a globally unique ID on reddit.
  4. account: This deals with the user's account. Using this we can register, login, set force https, update the account, update email and so on.

Registering a new account

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:

  • username: Used to identify the reddit community member uniquely
  • email: An optional field used to communicate directly with a user
  • password: Secure password to login into the reddit platform
  • verify password: This field should be the same as the password field
  • captcha: This field is used to check whether the user who is trying to login is a human or a programmable bot

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.

Modifying account information

Now, let's add an email to our account's profile which we intentionally left undone while creating the account in the previous section.

  1. Let us begin the process by creating a session object, which allows us to maintain certain parameters and cookies across all requests.
    >>> import requests
    >>> client = requests.session()
    >>> client.headers = {'User-Agent': 'Reddit API - update profile'}
    
  2. Let us create a DATA attribute with the 'user', 'passwd' and 'api type' attributes.
    >>> DATA = {'user': 'OUR_USERNAME', 'passwd': 'OUR_PASSWORD', 'api type': 'json'}
    
  3. We can access our reddit account by making a 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)
    
  4. The reddit api response to the above post request will be stored in the 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'}}}
    
  5. We need to send the 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)
    
  6. The response to the update call is stored in 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": []}}'
    
  7. Now, let us check whether the 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
    

Performing a simple search

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 subreddits

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'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150