Code Monkey home page Code Monkey logo

hackernewsapi's Introduction

Hacker News API

Unofficial Python API for Hacker News.

Build Status Test Coverage Version Downloads
Build Status Coverage Status Latest Version Downloads

Features

  • Compatible with Python 2 (2.7+).
  • Supports 'top', 'news2','newest' and 'best' posts
  • Retrieve comments from posts (flat list for now) (story.get_comments())
  • Pagination support for comments
  • Handles external posts, self posts and job posts
  • Get post details for any post (Story.fromid(7024626))

Installation

$ pip install HackerNews

Usage

NOTE: Do not make a lot of requests in a short period of time. HN has it's own throttling system.

from hn import HN

hn = HN()

# print the first 2 pages of newest stories
for story in hn.get_stories(story_type='newest', limit=60):
    print(story.rank, story.title)

API Reference

Class: HN

Get stories from Hacker News

get_stories

Parameters:

Name Type Required Description Default
story_type string No Returns the stories from this page. One of (empty string), news2, newest, best (empty string) (top)
limit integer No Number of stories required from the given page. Cannot be more than 30. 30

Example:

from hn import HN
hn = HN()
hn.get_stories(story_type='newest', limit=10)

get_leaders

Parameters:

Name Type Required Description Default
limit integer No Number of top leaders to return 10

Example:

from hn import HN
hn = HN()

# get top 20 users of HN
hn.get_leaders(limit=20)

Class: Story

Each Story has the following properties

  • rank - the rank of story on the page (keep pagination in mind)
  • story_id - the story's id
  • title - the title of the story
  • is_self - true for self/job stories
  • link - the URL it points to ('' for self posts)
  • domain - the domain of the link ('' for self posts)
  • points - the points/karma on the story
  • submitter - the user who submitted the story ('' for job posts)
  • submitter_profile - the above user's profile link (can be '')
  • published_time - the published time
  • num_comments - the number of comments a story has
  • comments_link - the link to the comments page

Make an object from the ID of a story

fromid

Parameters:

Name Type Required Description Default
item_id integer Yes Initializes an instance of Story for given item_id. Must be a valid story id.

Example:

from hn import Story
story = Story.fromid(6374031)
print story.title

Get a list of Comment's for this story

get_comments

Parameters:

Name Type Required Description Default

Example:

from hn import Story
story = Story.fromid(6374031)
comments = story.get_comments()

Class: Comment

Each Comment has the following properties

  • comment_id - the comment's item id
  • level - comment's nesting level
  • user - user's name who submitted the post
  • time_ago - time when it was submitted
  • body - text representation of comment (unformatted)
  • body_html - html of comment, may not be valid

Class: User

Each User has the following properties

  • username - user's profile name
  • date_created - when the profile was created
  • karma - user's e-points
  • avg - user's average karma per day

Examples

See my_test_bot.py

Tests

To run the tests locally just do:

$ chmod 777 runtests.sh
$ ./runtests.sh

To run individual tests,

$ python -m unittest tests.<module name>

The tests are run on a local test server with predownloaded original responses.

Donations

If HackerNewsAPI has helped you in any way, and you'd like to help the developer, please consider donating.

- BTC: 19dLDL4ax7xRmMiGDAbkizh6WA6Yei2zP5

- Flattr: https://flattr.com/profile/thekarangoel

Contribute

If you want to add any new features, or improve existing ones, feel free to send a pull request!

hackernewsapi's People

Contributors

ueg1990 avatar nucleartide avatar danclaudiupop avatar digital-shokunin avatar habi avatar taylan avatar arch119 avatar bitdeli-chef avatar gauravaror avatar nprescott avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.