Hacker News API – Overview

Today I will go through the “Unofficial Python API for Hacker News”, which can be found here

What is Hacker News?

Hacker News is a social news website that caters to programmers and entrepreneurs, delivering content related to computer science and entrepreneurship.

Python Wrapper

In the How to use Reddit API in Python, we described how to directly access Reddit API directly. Another way of doing that would be to use one of the Reddit wrappers. A wrapper is an API client, that are commonly used to wrap the API into easy to use functions by doing the API calls itself. When using a wrapper, you don’t have to care what’s going on behind the scenes, which sometimes can be easier for a beginner. Think of it as a interface between python and a web service.

Getting started

Let’s get started and install it by using the pip tool.

pip search HackerNews
HackerNews            - Python API for Hacker News.

pip install HackerNews

Successfully installed HackerNews Cleaning up… pip show HackerNews — Name: HackerNews Version: 1.3.3 Location: /usr/local/lib/python2.7/dist-packages Requires: BeautifulSoup4

API documentation

It is suggested that you read the documentation, which is available on this Github page. The API contains a few classes (HN and Story). The classes provides you with methods. The available methods for the HN class are: get_top_stories() Returns a list of Story objects from the homepage of HN get_newest_stories() Returns a list of Story objects from the newest page of HN The available method for the Story class is: print_story() – Print the details of a story

Run the program

The author of the API provides an example on his Github page. The example prints top and new posts from Hacker news. Open your favorite editor and copy and paste the following code.

#!/usr/bin/env python

from hn import HN

hn = HN()

# print top 10 stories from homepage
for story in hn.get_top_stories()[:10]:
    story.print_story()
    print '*' * 50
    print ''

# print 10 latest stories
for story in hn.get_newest_stories()[:10]:
    story.print_story()
    print '*' * 50
    print ''

# print all self posts from the homepage
for story in hn.get_top_stories():
    if story.is_self_post:
        story.print_story()
        print '*' * 50
        print ''

Save it as test_bot.py and run it. The program will loop through every story (post) on hacker news and give you the top 10 latest stories. For every post it will show information seen below Story details rank – the rank of story on the page story_id – the story’s id title – the title of the story is_self_post – true for self/job stories link – the url it points to (None for self posts) domain – the domain of the link (None for self posts) points – the points/karma on the story submitter – the user who submitted the story submitter_link – the above user profile link published_time – the published time ago num_comments – the number of comments it has comments_link – the link to the comments page Using a python wrapper for an API is nice and simple, but try to understand whats going on behind the scenes. It is important that you understand what is going on in the code, and once you learned it, you can go for wrapper. Try to get knowledge about Json and API’s, to understand how most of them work.

Leave a Reply

Your email address will not be published. Required fields are marked *