🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

hackernews-client

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hackernews-client

An unofficial but simple Python client for Hacker News Web API.

0.1.2b1
PyPI
Maintainers
1

hackernews-client

CircleCI codebeat badge Open Source Love

Use this wrapper library to download Hacker News headlines into your Python program.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

You will need the python-requests library so we can make HTTP calls. Install with pip:

pip install requests

Installing

In your Terminal

git clone https://github.com/samisnotinsane/hackernews-client.git
cd hackernews-client

Run the example script: news.py

python news.py

this fetches:

  • Best stories
  • Search
    • story
    • comment
    • poll
    • job
  • Most recent items (last n items)

You may make another copy of this script

cp news.py ./my_copy_news.py
python my_copy_news.py

modifying it to suit your use case.

Running the tests

This package contains tests which verifies the integrity of the internal logic.

Unit tests

To run all unit tests, make sure you're in the root directory (hackernews-client). Use Python's built in unittest library

python -m unittest -v

If you have clone from master branch, these should always pass. At the time of testing, sometimes these tests failed with SSLErrors due to too many requests being made to the Hacker News API too quickly. Wait ~20 secs before running the unit tests again.

Alternatively, it could be that a breaking change was made to the Web API which would require updating this library. Feel free to raise an Issue or a Pull Request to remediate the problem.

Usage

Begin by importing the library. This example assumes you're working within news.py in the root directory of the repo.

from hackernews import hn

The client is then initialised in the following way

news_client = hn.NewsClient()

In this case, news_client object gives access to all available methods. The data contained in the methods and the instances returned all closely follow the HackerNews API.

You may retrieve a list of best 10 stories in the following way

news_client.get_best_story(fetchMax=10)

adjusting the value of fetchMax up to and including a maximum of 500.

For completeness, full documentation with examples are provided below.

Documentation

Class: NewsClient

  • get_item

Parameters:

NameTypeRequiredDescriptionDefault
item_idintYesunique item id of Hacker News story, comment etcNone

Returns: a list of Item (each of type dict). See class documentation for Item which contains a full description of each property.

  • get_top_story

Parameters:

NameTypeRequiredDescriptionDefault
fetchMaxintYesNumber of stories to fetch, max value = 500500

Returns: a list of top stories as Item from the url https://hacker-news.firebaseio.com/v0/topstories.json

  • get_ask_story

Parameters:

NameTypeRequiredDescriptionDefault
fetchMaxintYesNumber of stories to fetch, max value = 200200

Returns: a list of Ask HN stories as Item from the url https://hacker-news.firebaseio.com/v0/askstories.json

  • get_new_story

Parameters:

NameTypeRequiredDescriptionDefault
fetchMaxintYesNumber of stories to fetch, max value = 500500

Returns: a list of new stories as Item from the url https://hacker-news.firebaseio.com/v0/newstories.json

  • get_show_story

Parameters:

NameTypeRequiredDescriptionDefault
fetchMaxintYesNumber of stories to fetch, max value = 200200

Returns: a list of Show HN stories as Item from the url https://hacker-news.firebaseio.com/v0/showstories.json

  • get_best_story

Parameters:

NameTypeRequiredDescriptionDefault
fetchMaxintYesNumber of stories to fetch, max value = 200200

Returns: a list of best stories as Item from the url https://hacker-news.firebaseio.com/v0/beststories.json

  • get_job_story

Parameters:

NameTypeRequiredDescriptionDefault
fetchMaxintYesNumber of stories to fetch, max value = 200200

Returns: a list of job stories as Item from the url https://hacker-news.firebaseio.com/v0/jobstories.json

  • get_max_item_id

Parameters: None

Returns: int of the largest item id from the url: https://hacker-news.firebaseio.com/v0/maxitem.json

  • get_item_by_id

Parameters:

NameTypeRequiredDescriptionDefault
idintYesunique Item id of Hacker News storyNone

Returns: Item representing the Hacker News story with given id fetched from url: https://hacker-news.firebaseio.com/v0/item/<item_id>.json

  • get_user_by_id

Parameters:

NameTypeRequiredDescriptionDefault
idintYesunique Item id of Hacker News storyNone

Returns: User representing the Hacker News user with given id fetched from url: https://hacker-news.firebaseio.com/v0/user/<user_id>.json

Class: Item

Stories, comments, jobs, Ask HNs and even polls are just items. They're identified by their ids, which are unique integers.

FieldDescription
idThe item's unique id.
deletedtrue if the item is deleted.
typeThe type of item. One of "job", "story", "comment", "poll", or "pollopt".
byThe username of the item's author.
timeCreation date of the item, in Unix Time.
textThe comment, story or poll text. HTML.
deadtrue if the item is dead.
parentThe comment's parent: either another comment or the relevant story.
pollThe pollopt's associated poll.
kidslist of Item of this item's comments, in ranked display order.
urlThe URL of the story.
scoreThe story's score, or the votes for a pollopt.
titleThe title of the story, poll or job.
partsA list of related pollopts, in display order.
descendantsIn the case of stories or polls, the total comment count.

Class: User

Users are identified by case-sensitive ids. Only users that have public activity (comments or story submissions) on the site are available through the API.

FieldDescription
idThe user's unique username. Case-sensitive. Required.
delayDelay in minutes between a comment's creation and its visibility to other users.
createdCreation date of the user, in Unix Time.
karmaThe user's karma.
aboutThe user's optional self-description. HTML.
submittedList of the user's stories, polls and comments.

Screenshot

Hacker News Client

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Author of haxor whose library inspired me to make my first Python library
  • Author of python-packaging whose tutorial helped me get my head around the Python import system

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts