Skip to main content

HackerNews / Algolia Python Library

This is a simple library to interface with HN Search API (provided by Algolia).

Install | Basic Usage | Development | Roadmap

👉 Note: As an example, I used this library to download ALL Hacker News posts and made it available as a public dataset in Kaggle.

Install instructions

$ pip install python-hn

Usage

from hn import search_by_date

# Search everything (stories, comments, etc) containing the keyword 'python'
search_by_date('python')

# Search everything (stories, comments, etc) from author 'pg' and keyword 'lisp'
search_by_date('lisp', author='pg', created_at__lt='2018-01-01')

# Search only stories
search_by_date('lisp', author='pg', stories=True, created_at__lt='2018-01-01')

# Search stories *or* comments
search_by_date(q='lisp', author='pg', stories=True, comments=True, created_at__lt='2018-01-01')
Tags

Tags are part of HN Search API provided by Algolia. You can read more in their docs. They can form complex queries, for example:

# All the comments in the story `6902129`
tags = PostType('comment') & StoryID('6902129')

The available tags are:

  • PostType: with options story, comment, poll, pollopt, show_hn, ask_hn, front_page.
  • Author: receives the username as param (Author('pg')).
  • StoryID: receives the story id (StoryID('6902129'))
Filters

Filters can be applied to restrict the search by:

  • Creation Date: created_at
  • Points: points
  • Number of comments: num_comments

They can accept >, <, >=, <= operators with a syntax similar to Django's.

  • lt (<): Lower than. Example ponts__lt=100
  • lte (<=): Lower than or equals to. Example ponts__lte=100
  • gt (>): Greater than. Example created_at__gt='2018' (created after 2018-01-01).
  • gte (>=): Greater than or equals to. Example num_comments__gte=50.

Examples (See Algolia docs for more info):

# Created after October 1st, 2018
search_by_date(created_at__gt='2018-10')

# Created after October 1st, 2017 and before January 1st 2018
search_by_date(created_at__gt='2018-10', created_at__lt='2018')

# Stories with *exactly* 1000 points
search_by_date(tags=PostType('story'), points=1000)

# Comments with more than 50 points
search_by_date(tags=PostType('comment'), points__gt=50)

# Stories with 100 comments or more
search_by_date(tags=PostType('story'), num_comments__gt=100)

[TODO]

Development

I'm in the process of updating this project and migrating to uv. You should be able to just do:

$ uv run py.test

Metadata

Release files for python-hn 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-hn 0.0.4
File Size Uploaded Provenance Actions
python_hn-0.0.4.tar.gz 7.8 kB Without provenance Details for python_hn-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-hn 0.0.4
File Interpreter ABI Platform Actions
python_hn-0.0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details for python_hn-0.0.4-py2.py3-none-any.whl

Total release size: 15.2 kB

Release files / python_hn-0.0.4.tar.gz

Download URL python_hn-0.0.4.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1a4276fcfa31470b1efc5819a85acf06a2e0bc367620022d84c6f9e9232d629a
BLAKE2b-256 checksum
How to use checksums
3a0f2b8f11d34b8448ddc89cb3048fb597f03bcc799defd902d41552fe0db40f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.2

Release files / python_hn-0.0.4-py2.py3-none-any.whl

Download URL python_hn-0.0.4-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6f189dc49e87dab6fdf263c4a4479c47180e0088e238c542eb5a6c1e74041ac7
BLAKE2b-256 checksum
How to use checksums
babef13a05e55271fa6dcc5b24985c636b934f85218a72a26f048489d5668de7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.2

Security for python-hn 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page