Profile picture

Co-founder @ RMOTR

Python HN Library Interactive Docs

Last updated: December 8th, 20182018-12-08Project preview

Version: 0.0.3

python-hn is a small library to access Hacker New's Search API powered by Algolia.

It's designed to be simple to use and pythonic, but also provide the full power of Search API.

This is an interactive documentation page, you can fork this project and start playing with it immediately using the Fork button to the right of this page 👉

Quick example¶

The main function of the library is search_by_date, which accepts multiple parameters; among them, is a query string containing the phrase to search for.

In [1]:
from hn import search_by_date

from utils import output_results
In [2]:
results = search_by_date('python')

By default, all the different "post types" will be included: stories, comments, polls, etc.

In [3]:
output_results(results, 10)
IDPost Type Text URL
18512469Story Tworoutines in Python – Mixing synchronous and asynchronous code https://news.ycombinator.com/item?id=18512469
18512399Comment What kind of problems are... https://news.ycombinator.com/item?id=18512399
18512088Comment I'm not against more... https://news.ycombinator.com/item?id=18512088
18512023Comment I trained a guy that was ... https://news.ycombinator.com/item?id=18512023
18512003Comment My biggest issue with lis... https://news.ycombinator.com/item?id=18512003
18511911Story Ask HN: Can you recommend a modern, accessible P2P framework/library?https://news.ycombinator.com/item?id=18511911
18511771Comment Unfortunately this projec... https://news.ycombinator.com/item?id=18511771
18511769Comment Something I didn't u... https://news.ycombinator.com/item?id=18511769
18511281Comment I was similarly stumped, ... https://news.ycombinator.com/item?id=18511281
18511175Story MIT AI: Python (Guido van Rossum) https://news.ycombinator.com/item?id=18511175

As you can see, we're receiving both results for stories and comments.

Searching by author¶

The second parameter of the search_by_date function is the author's username of the post (story or comment). Example:

In [4]:
# 'pg' is Paul Graham: https://news.ycombinator.com/user?id=pg
results = search_by_date('lisp', 'pg')
In [5]:
output_results(results, 10)
IDPost Type Text URL
7446596Comment This was all the code it ... https://news.ycombinator.com/item?id=7446596
7261591Story Source of the recent outagelet https://news.ycombinator.com/item?id=7261591
7128860Comment Why doesn't the arti... https://news.ycombinator.com/item?id=7128860
6705398Comment I thought this would be a... https://news.ycombinator.com/item?id=6705398
6557282Comment Has "Lisp hacker&quo...; https://news.ycombinator.com/item?id=6557282
6400769Comment It's a real person. ... https://news.ycombinator.com/item?id=6400769
6210178Comment Wow is this list good. I... https://news.ycombinator.com/item?id=6210178
5575255Story Danny Sullivan's Twitter list about Watertownhttps://news.ycombinator.com/item?id=5575255
5419609Comment Unfortunately I can't nam... https://news.ycombinator.com/item?id=5419609
5376251Comment It happens. There are in... https://news.ycombinator.com/item?id=5376251

Filtering results by post type¶

You can pass multiple boolean parameters to filter the post types: stories, comments, show_hn, ask_hn, front_page, polls, pollopt.

In [6]:
results = search_by_date('python', stories=True)
In [8]:
output_results(results, 10)
IDPost Type Text URL
18512469Story Tworoutines in Python – Mixing synchronous and asynchronous code https://news.ycombinator.com/item?id=18512469
18511911Story Ask HN: Can you recommend a modern, accessible P2P framework/library? https://news.ycombinator.com/item?id=18511911
18511175Story MIT AI: Python (Guido van Rossum) https://news.ycombinator.com/item?id=18511175
18509313Story Show HN: Efficient environment variables management and typing for python https://news.ycombinator.com/item?id=18509313
18509054Story Snippet: How to Get List of Dictionary's Key in Python Using Unpacking https://news.ycombinator.com/item?id=18509054
18508579Story I'm making a new Python library that lets you write less code to do more thingshttps://news.ycombinator.com/item?id=18508579
18506130Story Viewing Python execution with source code rewriting https://news.ycombinator.com/item?id=18506130
18505267Story The Updated Guide to Unicode on Python (2013) https://news.ycombinator.com/item?id=18505267
18505111Story GhostiFi – Working with Vultr and Cloudflare API in Python https://news.ycombinator.com/item?id=18505111
18504195Story Ask HN: How to deploy an AI App? https://news.ycombinator.com/item?id=18504195

Also filtering by author:

In [9]:
results = search_by_date('lisp', author='pg', stories=True)
In [10]:
output_results(results, 10)
IDPost Type Text URL
7261591Story Source of the recent outagelet https://news.ycombinator.com/item?id=7261591
5575255Story Danny Sullivan's Twitter list about Watertown https://news.ycombinator.com/item?id=5575255
5092711Story What we discovered about InstallMonetizer https://news.ycombinator.com/item?id=5092711
2463285Story Andreessen Horowitz Leads $1.75M Round In Freebie Marketplace Listiahttps://news.ycombinator.com/item?id=2463285
878576Story Final Startup School Speaker List (Evan and Biz now coming) https://news.ycombinator.com/item?id=878576
842685Story Doug McIlroy: McCarthy Presents Lisp https://news.ycombinator.com/item?id=842685
93526Story New: Best Comments (via "Lists" Link at Bottom) https://news.ycombinator.com/item?id=93526
35935Story Arrington: The FCC Needs To Listen To Google https://news.ycombinator.com/item?id=35935
7687Story My Pairwise Test Results (not bad, but got the book part wrong) https://news.ycombinator.com/item?id=7687

Filtering by query, author and including both stories and comments:

In [11]:
results = search_by_date('lisp', author='pg', stories=True, comments=True)
In [12]:
output_results(results, 10)
IDPost Type Text URL
7446596Comment This was all the code it ... https://news.ycombinator.com/item?id=7446596
7261591Story Source of the recent outagelet https://news.ycombinator.com/item?id=7261591
7128860Comment Why doesn't the arti... https://news.ycombinator.com/item?id=7128860
6705398Comment I thought this would be a... https://news.ycombinator.com/item?id=6705398
6557282Comment Has "Lisp hacker&quo...; https://news.ycombinator.com/item?id=6557282
6400769Comment It's a real person. ... https://news.ycombinator.com/item?id=6400769
6210178Comment Wow is this list good. I... https://news.ycombinator.com/item?id=6210178
5575255Story Danny Sullivan's Twitter list about Watertownhttps://news.ycombinator.com/item?id=5575255
5419609Comment Unfortunately I can't nam... https://news.ycombinator.com/item?id=5419609
5376251Comment It happens. There are in... https://news.ycombinator.com/item?id=5376251

Other examples:

In [23]:
results = search_by_date('lisp', author='pg', polls=True)
In [24]:
output_results(results, 10)
IDPost Type Text URL
1185896Poll Poll: Ask or Leaders in Top Bar?https://news.ycombinator.com/item?id=1185896
In [25]:
results = search_by_date('python', show_hn=True)
In [26]:
output_results(results, 10)
IDPost Type Text URL
18509313Story Show HN: Efficient environment variables management and typing for python https://news.ycombinator.com/item?id=18509313
18502878Story Show HN: Packagr.app – a cloud hosted PyPI server for Python developers https://news.ycombinator.com/item?id=18502878
18484789Story Show HN: Legendary 6502 Python emulator https://news.ycombinator.com/item?id=18484789
18484351Story Show HN: Python memoization across program runs https://news.ycombinator.com/item?id=18484351
18483859Story Show HN: All-pair similarity search on millions of sets in Python and on laptophttps://news.ycombinator.com/item?id=18483859
18473982Story Show HN: Prime number sieve in 31 lines of Python https://news.ycombinator.com/item?id=18473982
18429985Story Show HN: Straightforward implementation from a paper to Python code by PyTorch https://news.ycombinator.com/item?id=18429985
18415274Story Show HN: Fast spectogram library in python and rust https://news.ycombinator.com/item?id=18415274
18414367Story Show HN: Starlette – An asyncio Web framework for Python https://news.ycombinator.com/item?id=18414367
18409967Story Show HN: Empirical Eye – Bring ML+AI to the Physical World https://news.ycombinator.com/item?id=18409967

Tags¶

HN Search API supports "tags", that can be logically combined to create complex expressions. There are 3 types of tags included in python-hn:

  • 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'))

PostTypes also have aliases that can be used instead of writing the full tag.

In [33]:
from hn import PostType, Author, StoryID
from hn.tags import Story, Comment, AskHN, ShowHN
In [34]:
PostType('story') == Story
Out[34]:
True

The power of the tags arise when we combine them logically with or and and expressions. For example, you could ask for posts that are either Ask HN or Show HN. To combine tags with an or expression, the operator is |:

In [30]:
tags = PostType('ask_hn') | PostType('show_hn')
In [31]:
results = search_by_date('python', tags=tags)
In [32]:
output_results(results, 10)
IDPost Type Text URL
18511911Story Ask HN: Can you recommend a modern, accessible P2P framework/library? https://news.ycombinator.com/item?id=18511911
18509313Story Show HN: Efficient environment variables management and typing for python https://news.ycombinator.com/item?id=18509313
18504195Story Ask HN: How to deploy an AI App? https://news.ycombinator.com/item?id=18504195
18502878Story Show HN: Packagr.app – a cloud hosted PyPI server for Python developers https://news.ycombinator.com/item?id=18502878
18487176Story Ask HN: Blocking os.urandom calls in Python = 3.6 https://news.ycombinator.com/item?id=18487176
18486050Story Ask HN: How to Evolve a Legacy .NET Application with Python https://news.ycombinator.com/item?id=18486050
18484789Story Show HN: Legendary 6502 Python emulator https://news.ycombinator.com/item?id=18484789
18484351Story Show HN: Python memoization across program runs https://news.ycombinator.com/item?id=18484351
18483859Story Show HN: All-pair similarity search on millions of sets in Python and on laptophttps://news.ycombinator.com/item?id=18483859
18473982Story Show HN: Prime number sieve in 31 lines of Python https://news.ycombinator.com/item?id=18473982

Using aliases:

In [35]:
tags = AskHN | ShowHN
In [36]:
results = search_by_date('python', tags=tags)
In [37]:
output_results(results, 10)
IDPost Type Text URL
18511911Story Ask HN: Can you recommend a modern, accessible P2P framework/library? https://news.ycombinator.com/item?id=18511911
18509313Story Show HN: Efficient environment variables management and typing for python https://news.ycombinator.com/item?id=18509313
18504195Story Ask HN: How to deploy an AI App? https://news.ycombinator.com/item?id=18504195
18502878Story Show HN: Packagr.app – a cloud hosted PyPI server for Python developers https://news.ycombinator.com/item?id=18502878
18487176Story Ask HN: Blocking os.urandom calls in Python = 3.6 https://news.ycombinator.com/item?id=18487176
18486050Story Ask HN: How to Evolve a Legacy .NET Application with Python https://news.ycombinator.com/item?id=18486050
18484789Story Show HN: Legendary 6502 Python emulator https://news.ycombinator.com/item?id=18484789
18484351Story Show HN: Python memoization across program runs https://news.ycombinator.com/item?id=18484351
18483859Story Show HN: All-pair similarity search on millions of sets in Python and on laptophttps://news.ycombinator.com/item?id=18483859
18473982Story Show HN: Prime number sieve in 31 lines of Python https://news.ycombinator.com/item?id=18473982

To combine tags with an and expression, the operator is &. For example, all the posts that are comments of the story ID 7261591:

In [38]:
tags = PostType('comment') & StoryID('7261591')
In [39]:
results = search_by_date(tags=tags)
In [40]:
output_results(results, 10)
IDPost Type Text URL
7298300Comment Sorry, this is uneducated...https://news.ycombinator.com/item?id=7298300
7269494Comment I was able to keep a 10k&...https://news.ycombinator.com/item?id=7269494
7269489Comment Hmm, that sounds like qui...https://news.ycombinator.com/item?id=7269489
7268211Comment You just need enough core...https://news.ycombinator.com/item?id=7268211
7266280Comment Serving 403s to w00tw00ts...https://news.ycombinator.com/item?id=7266280
7265942Comment Things like "Top 10 ... https://news.ycombinator.com/item?id=7265942
7265025Comment Right ... websites with t...https://news.ycombinator.com/item?id=7265025
7264560Comment In this case using nginx ...https://news.ycombinator.com/item?id=7264560
7262931Comment I'm not sure I parti... https://news.ycombinator.com/item?id=7262931
7262890Comment Interestingly, distinguis...https://news.ycombinator.com/item?id=7262890

Filtering by date, points or number of comments¶

It is also possible to pass different filters to restrain the search by creation date, number of points or comments. Namely, the parameters are:

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

They 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.

A few more examples using filters:

All post types created after October 1st, 2018¶

The filter is created_at__gt, gt is the > operator. The created_at filter will try to parse dates automatically (eg: 2018 is interpreted as 2018-01-01).

In [43]:
results = search_by_date(stories=True, created_at__gt='2018-10')
In [44]:
output_results(results, 10)
IDPost Type Text URL
18512608Story Apple resumes iPhone X production in face of weak XS sales, report claims https://news.ycombinator.com/item?id=18512608
18512600Story Modulator crate and Play app (Rust coding series) https://news.ycombinator.com/item?id=18512600
18512591Story Facebook policy chief admits hiring PR firm to attack George Soros https://news.ycombinator.com/item?id=18512591
18512561Story 7 Block Cipher Modes of Operation https://news.ycombinator.com/item?id=18512561
18512551Story Life is like a hurricane: An oral history of the Disney Afternoon https://news.ycombinator.com/item?id=18512551
18512548Story Demystification of TC https://news.ycombinator.com/item?id=18512548
18512535Story Intro to deployment strategies: blue-green, canary, and more https://news.ycombinator.com/item?id=18512535
18512514Story 10 Simple Ciphers You Should Know https://news.ycombinator.com/item?id=18512514
18512508Story Japan startup’s superflies may hold key to food self-sufficiency – even in spacehttps://news.ycombinator.com/item?id=18512508
18512502Story Probabilistic programming and proof that Poland was robbed – 2018 Chess Olympiadhttps://news.ycombinator.com/item?id=18512502
Created after October 1st, 2017 and before January 1st 2018¶
In [47]:
results = search_by_date(stories=True, created_at__gt='2017-10', created_at__lt='2018')
In [48]:
output_results(results, 10)
IDPost Type Text URL
16043719Story RAM-less Buffers https://news.ycombinator.com/item?id=16043719
16043643Story Profitable remote job site built with single PHP file (4.5K lines of code)https://news.ycombinator.com/item?id=16043643
16043612Story Subreddit Gender Ratios https://news.ycombinator.com/item?id=16043612
16043585Story Core Infrastructure Initiative Best Practices Badge https://news.ycombinator.com/item?id=16043585
16043578Story IOHIDeous OS X Local Kernel Vulnerability https://news.ycombinator.com/item?id=16043578
16043561Story Dave Barry's 2017 Year in Review https://news.ycombinator.com/item?id=16043561
16043557Story New Year’s Resolutions and the Science of Willpower (2015) https://news.ycombinator.com/item?id=16043557
16043554Story Top Cryptocurrencies or Altcoins to have in 2018 https://news.ycombinator.com/item?id=16043554
16043548Story The Google Book https://news.ycombinator.com/item?id=16043548
16043542Story AI and Deep Learning in 2017 – A Year in Review https://news.ycombinator.com/item?id=16043542
A few more examples:¶
In [52]:
# Stories with *exactly* 1000 points
results = search_by_date(stories=True, points=1000)
In [53]:
output_results(results, 10)
IDPost Type Text URL
14090063Story IndieHackers.com acquired by Stripehttps://news.ycombinator.com/item?id=14090063
13489156Story First they came for the Iranians https://news.ycombinator.com/item?id=13489156
In [56]:
# Stories including "Python" with more than 1000 points
results = search_by_date('python', stories=True, points__gt=1000)
In [57]:
output_results(results, 10)
IDPost Type Text URL
17515492Story “I'm basically giving myself a permanent vacation from being BDFL”https://news.ycombinator.com/item?id=17515492
13319904Story Grumpy: Go running Python https://news.ycombinator.com/item?id=13319904
In [58]:
# Comments with more than 50 points
results = search_by_date('python', comments=True, points__gt=50)
In [59]:
output_results(results, 10)
IDPost Type Text URL
8374713Comment The older I get, the more...https://news.ycombinator.com/item?id=8374713
8167733Comment The idea that Python is s...https://news.ycombinator.com/item?id=8167733
8070592Comment I really think the future...https://news.ycombinator.com/item?id=8070592
8002089Comment I work at Monash Universi...https://news.ycombinator.com/item?id=8002089
7923883Comment So I've finally been... https://news.ycombinator.com/item?id=7923883
7802095Comment That pretty much reiterat...https://news.ycombinator.com/item?id=7802095
7799923Comment I doubt the lazy people w...https://news.ycombinator.com/item?id=7799923
7733529Comment The key to fast startup d...https://news.ycombinator.com/item?id=7733529
7733148Comment Having written a bunch of...https://news.ycombinator.com/item?id=7733148
7716040Comment This is a pretty good lis...https://news.ycombinator.com/item?id=7716040
In [60]:
# Stories with 100 comments or more
results = search_by_date(stories=True, num_comments__gt=100)
In [61]:
output_results(results, 10)
IDPost Type Text URL
18511184Story Ditching the MacBook Pro for a MacBook Air https://news.ycombinator.com/item?id=18511184
18510528Story Ask HN: How can I learn to read mathematical notation? https://news.ycombinator.com/item?id=18510528
18509735Story The Google Cemetery – A list of dead Google products and why they diedhttps://news.ycombinator.com/item?id=18509735
18508311Story Still in love with Rust https://news.ycombinator.com/item?id=18508311
18507407Story Ask HN: Why not more hiring of junior devs, then on-the-job-training? https://news.ycombinator.com/item?id=18507407
18506170Story Slow Software https://news.ycombinator.com/item?id=18506170
18505755Story I Can Be the Bank: Individual Investors Buy Busted Mortgages https://news.ycombinator.com/item?id=18505755
18505672Story Texas Is About to Create OPEC’s Worst Nightmare https://news.ycombinator.com/item?id=18505672
18505136Story American Is Killed by Bow and Arrow on Remote Indian Island https://news.ycombinator.com/item?id=18505136
18504879Story Why Aren't There C Conferences? https://news.ycombinator.com/item?id=18504879
Notebooks AI
Notebooks AI Profile20060