pythonpython-2.7hacker-news-api

How to use Hacker News API in Python?


Hacker News has released an API, how do I use it in Python?

I want get all the top posts. I tried using urllib, but I don't think I am doing right.

here's my code:

import urllib2
response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
html = response.read()
print response.read()

It just prints empty

''

I missed a line, had updated my code.


Solution

  • As @jonrsharpe, explained read() is only one time operation. So if you print html, you will get list of all ids. And if you go through that list, you have to make each request again to get story of each id.

    First you have to convert the received data to python list and go through them all.

    base_url =  'https://hacker-news.firebaseio.com/v0/item/{}.json?print=pretty'
    top_story_ids = json.loads(html)
    for story in top_story_ids:
        response = urllib2.urlopen(base_url.format(story))
        print response.read()
    

    Instead of all this, you could use haxor, it's a Python wrapper for Hacker News API. Following code will fetch you all the ids of top stories :

    from hackernews import HackerNews
    hn = HackerNews()
    top_story_ids = hn.top_stories()
    # >>> top_story_ids
    # [8432709, 8432616, 8433237, ...]
    

    Then you can go through that loop and print all them, for example:

    for story in top_story_ids:
       print hn.get_item(story)
    

    Disclaimer: I wrote haxor.