• Stars
    star
    153
  • Rank 235,492 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Unofficial Python wrapper for official Hacker News API

haxor

travis coverall version supported license

Unofficial Python wrapper for official Hacker News API.

Installation

pip install haxor

Usage

Import and initialization:

from hackernews import HackerNews
hn = HackerNews()

Items

Stories, comments, jobs, Ask HNs and even polls are just items with unique item id.

To query item information by id:

item = hn.get_item(8863)
# >>> item.title
# 'My YC app: Dropbox - Throw away your USB drive'
# >>> item.item_type
# 'story'
# >>> item.kids
# [ 8952, 9224, 8917, ...]

Since most results are returned as integer IDs (like item.kids above), these results require further iteration. Instead of doing this yourself, use the expand flag to get object-oriented, detailed item info by id:

item = hn.get_item(8863, expand=True)
# >>> item.kids
# [<hackernews.Item: 9224 - None>, <hackernews.Item: 8952 - None>, ...]
# >>> item.by
# <hackernews.User: dhouston>

To query a list of Item IDs:

items = hn.get_items_by_ids([8863, 37236, 2345])
# >>> items
# [<hackernews.Item: 8863 - My YC app: Dropbox - Throw away your USB drive>, <hackernews.Item:
# 37236 - None>, <hackernews.Item: 2345 - The Best Buy Scam.>]

Use the item_type filter to specifically select 'story', 'comment', 'job', or 'poll' items:

items = hn.get_items_by_ids([8863, 37236, 2345], item_type='story')
# >>> items
# [<hackernews.Item: 8863 - My YC app: Dropbox - Throw away your USB drive>, <hackernews.Item: # 2345 - The Best Buy Scam.>]

Stories

The HN API allows for real-time querying for New, Top, Best, Ask HN, Show HN, and Jobs stories.

As an example, to get Item objects of current top stories:

top_stories = hn.top_stories()
# >>> top_stories
# [<hackernews.Item: 16924667 - Ethereum Sharding FAQ>, ...]

Useful Item Queries

To get current largest Item id (most recent story, comment, job, or poll):

max_item = hn.get_max_item()
# >>> max_item
# 16925673

Once again, use the expand flag to get an object-oriented, detailed Item representation:

max_item = hn.get_max_item(expand=True)
# >>> max_item
# <hackernews.Item: 16925673 - None>

To get the x most recent Items:

last_ten = hn.get_last(10)
# >>> last_ten
# [<hackernews.Item: 16925688 - Show HN: Eventbot – Group calendar for Slack teams>, ...]

Users

HN users are also queryable.

To query users by user_id (i.e. username on Hacker News):

user = hn.get_user('pg')
# >>> user.user_id
# 'pg'
# >>> user.karma
# 155040

Use the expand flag to get an object-oriented, detailed Item representation for User attributes:

user = hn.get_user('dhouston', expand=True)
# >>> user.stories
# [<hackernews.Item: 1481914 - Dropbox is hiring a Web Engineer>, ...]
# >>> user.comments
# [<hackernews.Item: 16660140 - None>, <hackernews.Item: 15692914 - None>, ...]
# >>> user.jobs
# [<hackernews.Item: 3955262 - Dropbox seeking iOS and Android engineers>, ...]

To query a list of users:

users = hn.get_users_by_ids(['pg','dhouston'])
# >>> users
# [<hackernews.User: pg>, <hackernews.User: dhouston>]

Examples

Get top 10 stories:

hn.top_stories(limit=10)

# [<hackernews.Item: 16924667 - Ethereum Sharding FAQ>, <hackernews.Item: 16925499 - PipelineDB # v0.9.9 – One More Release Until PipelineDB Is a PostgreSQL Extension>, ...]

Find all the 'jobs' post from Top Stories:

stories = hn.top_stories()
for story in stories:
    if story.item_type == 'job':
        print(story)

# <hackernews.Item: 16925047 - Taplytics (YC W14) is solving hard engineering problems in
# Toronto and hiring>
# ...
# ...

Find Python jobs from monthly who is hiring thread:

# Who is hiring - April 2018
# https://news.ycombinator.com/item?id=16735011

who_is_hiring = hn.get_item(16735011, expand=True)

for comment in who_is_hiring.kids:
    if 'python' in comment.text.lower():
        print(comment)

# <hackernews.Item: 16735358 - None>
# <hackernews.Item: 16737152 - None>
# ...
# ...

API Reference

Class: HackerNews

Parameters:

Name Type Required Description Default
version string No specifies Hacker News API version v0

get_item

Description: Returns Item object

Parameters:

Name Type Required Description Default
item_id string/int Yes unique item id of Hacker News story, comment etc None
expand bool No flag to indicate whether to transform all IDs into objects False

get_items_by_ids

Description: Returns list of Item objects

Parameters:

Name Type Required Description Default
item_ids list of string/int Yes unique item ids of Hacker News stories, comments etc None
item_type string No item type to filter results with None

get_user

Description: Returns User object

Parameters:

Name Type Required Description Default
user_id string Yes unique user id of a Hacker News user None
expand bool No flag to indicate whether to transform all IDs into objects False

get_users_by_ids

Description: Returns list of User objects

Parameters:

Name Type Required Description Default
user_ids list of string/int Yes unique user ids of Hacker News users None

top_stories

Description: Returns list of Item objects of current top stories

Parameters:

Name Type Required Description Default
raw bool No indicate whether to represent all objects in raw json False
limit int No specifies the number of stories to be returned None

new_stories

Description: Returns list of Item objects of current new stories

Parameters:

Name Type Required Description Default
raw bool No indicate whether to represent all objects in raw json False
limit int No specifies the number of stories to be returned None

ask_stories

Description: Returns list of Item objects of latest Ask HN stories

Parameters:

Name Type Required Description Default
raw bool No indicate whether to represent all objects in raw json False
limit int No specifies the number of stories to be returned None

show_stories

Description: Returns list of Item objects of latest Show HN stories

Parameters:

Name Type Required Description Default
raw bool No indicate whether to represent all objects in raw json False
limit int No specifies the number of stories to be returned None

job_stories

Description: Returns list of Item objects of latest Job stories

Parameters:

Name Type Required Description Default
raw bool No indicate whether to represent all objects in raw json False
limit int No specifies the number of stories to be returned None

updates

Description: Returns list of Item and User objects that have been changed/updated recently.

Parameters: N/A

get_max_item

Description: Returns current largest item id or current largest Item object

Parameters:

Name Type Required Description Default
expand bool No flag to indicate whether to transform ID into object False

get_all

Description: Returns all Item objects from HN

Parameters: N/A

get_last

Description: Returns list of num most recent Item objects

Parameters:

Name Type Required Description Default
num int No numbr of most recent records to pull from HN 10

Class: Item

From Official HackerNews Item:

Property Description
item_id The item’s unique id.
deleted true if the item is deleted.
item_type The type of item. One of “job”, “story”, “comment”, “poll”, or “pollopt”.
by The username of the item’s author.
submission_time Creation date of the item, in Python datetime.
text The comment, Ask HN, or poll text. HTML.
dead true if the item is dead.
parent The item’s parent. For comments, either another comment or the relevant story. For pollopts, the relevant poll.
poll The ids of poll's.
kids The ids of the item’s comments, in ranked display order.
url The URL of the story.
score The story’s score, or the votes for a pollopt.
title The title of the story or poll.
parts A list of related pollopts, in display order.
descendants In the case of stories or polls, the total comment count.
raw original JSON response.

Class: User

From Official HackerNews User:

Property Description
user_id The user’s unique username. Case-sensitive.
delay Delay in minutes between a comment’s creation and its visibility to other users.
created Creation date of the user, in Python datetime.
karma The user’s karma.
about The user’s optional self-description. HTML.
submitted List of the user’s stories, polls and comments.
raw original JSON response.

Additional properties when expand is used

Property Description
stories The user’s submitted stories.
comments The user's submitted comments.
jobs The user's submitted jobs.
polls The user's submitted polls.
pollopts The user's submitted poll options.

Development

For local development do pip installation of requirements-dev.txt:

pip install -r requirements-dev.txt

Testing

Run the test suite by running:

python setup.py develop
pytest tests

LICENSE

The mighty MIT license. Please check LICENSE for more details.

More Repositories

1

rockstar

Makes you a Rockstar C++ Programmer in 2 minutes
Python
4,147
star
2

py-caskdb

(educational) build your own disk based KV store
Python
968
star
3

grpc-errors

A handy guide to gRPC errors
C#
549
star
4

fast-sqlite3-inserts

Some bunch of test scripts to generate a SQLite DB with 1B rows in fastest possible way
Rust
337
star
5

status

HTTP Status for Humans
Python
276
star
6

gg-flip

Highly performant Javascript library to flip the signs
HTML
174
star
7

pytorch-flask-api

Python
166
star
8

pytorch-flask-api-heroku

HTML
160
star
9

go-caskdb

(educational) build your own disk based KV store in Go
Go
134
star
10

slackipy

Automate user invites to your Slack channel!
Python
87
star
11

della

Della is a Django app for managing Secret Santa/Gift Exchange.
Python
48
star
12

Reddit-GoodReads-Bot

Python
32
star
13

little-finger

Clojure
31
star
14

cowin-assist

Telegram Bot to check covid vaccine slot availability on CoWin site
Python
30
star
15

prawoauth2

Helper library to make your life easier using OAuth2 for PRAW
Python
29
star
16

kylo

The FAQ Bot
Python
16
star
17

history-bleed

Check your browser history against Cloud Bleed
JavaScript
13
star
18

score-notify

Displays cricket score as notification. OS X Only.
Python
11
star
19

dev-startups-india

list of Indian startups working on building developer tools
9
star
20

fluvio-go

Rust
8
star
21

saveourcinema

code which powers http://saveourcinema.in
HTML
8
star
22

ares

Slack Moderator
Go
8
star
23

Hocus-Pocus

Simple OS X app which shows hidden files. Written in Swift.
Swift
8
star
24

breast-cancer-prediction

Jupyter Notebook
7
star
25

openshift-tornado-starter

Simple repo to get started on Openshift with Python Tornado
6
star
26

cemetery.io

HTML
5
star
27

sign-flip

JavaScript
5
star
28

isso-openshift

This repo helps you install Isso on Openshift with a single click
Python
5
star
29

brave-browser-hardening

mirror of https://gitlab.com/CHEF-KOCH/brave-browser-hardening
5
star
30

Laozi

Laozi is a Goodreads bot for Telegram
Python
5
star
31

heroku-tornado-starter

Simple repo to get started on Heroku to serve static files using Python Tornado
Python
5
star
32

nightreads

Python
5
star
33

slackipycore

Invite Users to Slack using Python
Python
4
star
34

talkwithme

simple chatroom built using Tornado
Python
4
star
35

unnamed-wip-document-db

a document database (like mongo) written in Python. Uses CaskDB as storage backend.
4
star
36

learning-scraping

A project written in Django, which aims to teach Web Scraping for beginners.
Python
3
star
37

polistats

Google Search Trends of Indian Politics
Python
3
star
38

kekday

Find a Redditor's Cake Day!
HTML
3
star
39

good-human

Python
2
star
40

gh-vue

A simple Vue JS project to display my latest commits from Github
JavaScript
2
star
41

learning-tornado

Python
2
star
42

btree

An in-memory B Tree implementation in Go
Go
2
star
43

tildes-plus

JavaScript
2
star
44

mca_scraper

Python
2
star
45

gofish

Go
2
star
46

little-finger-android

Java
2
star
47

hugo-skyfall

Hugo theme - skyfall
CSS
2
star
48

mvcc-go

Go
2
star
49

reddit-india-books

What does Reddit India likes to read?
Python
2
star
50

skyfall

Clean Pelican theme for http://avi.im/blag
HTML
2
star
51

mkstemp

A Hacker News Clone
Python
2
star
52

offenders

List of Telecos and Companies which break Net Neutrality
2
star
53

Bitbucket-import

Python
1
star
54

monkey

An interpreter for the Monkey Programming Language in Golang
Go
1
star
55

wutong

A naughty Telegram bot
Python
1
star
56

tornado-benchmarks

pointless tornado benchmarks
Python
1
star
57

FoodPin

Swift
1
star
58

rtiman

Kickstarter for RTIs
Python
1
star
59

avinassh.github.io

HTML
1
star
60

bast

Delete your Reddit comment history safely
Go
1
star
61

blag

hosts my blog
CSS
1
star
62

goodreads-export

CSS
1
star
63

little-finger-ios

Swift
1
star
64

inks

Go
1
star
65

Calculator-Swift

Simple Calculator app written using Swift
Swift
1
star
66

honk

Go
1
star
67

ratelimit

Go
1
star
68

heroku-static

Ruby
1
star
69

build-your-regex

Go port of Rob Pike's Regex Engine (from C)
Go
1
star
70

hugo-publish

My Github Actions setup for publishing a Hugo blog
1
star