Package: hnpy
a Hacker News API wrapper written in PythonPurposeThis was written with the goal of sticking close to the API in terms of
naming and structure, but with the added benefits that come with using
Python objects.InstallationUse one of the following, depending on how your system is configured:pipinstallhnpypip3installhnpypython3-mpipinstallhnpyUsageCreate an instance of the main class like this:importhnpyhn=hnpy.HackerNews()ItemsIf you know an item’s ID, you can access it like this:post=hn.item(8863)An Item can be one of five types: “job”, “story”, “comment”, “poll”, or
“pollopt.” You can determine its type by accessingpost.type.Item objects are loaded lazily, meaning they only make network requests
when information that they don’t already have is needed.Items can be checked for equality against other Items or against the ID
inintorstrform.Items have various attributes which depend on their type. You can usehasattr()to programmatically determine whether a certain Item has
an attribute or not. More on attributeshere. Attribute names in
hnpy are copied from field names in the API.Items have five special attributes:Attribute nameExplanationcontentThe “meat” of the Item —text,url, ortitle,
with a fallback to an empty
string if none exist.linkURL to access the Item.parentItem that this one replies to
(not always present).pollItem of typepollthat this
one belongs to (only present onpolloptItems).byUser who created this Item.Thelinkandcontentattributes are noted here because the API
does not directly provide them, so they are not documented elsewhere.
Theparent,pollandbyattributes are noted because they
contain fully-featured, lazily-loaded Items as opposed to just the IDs
that the API responds with. The rest of an Items attributes are
delivered in the same format that they are received from the API.The attributes can be accessed like so:post.contentpost.linkpost.parentpost.pollpost.byItems also have 2 special methods which iterate and deliver Items. Each
takes an optionallimit=parameter which defaults to 25.Method nameUsekids()Iterate over the comments that
reply to this Item.parts()Iterate over the options of this
Item (only present onpolltypes).The methods can be used like this:forkidinpost.kids(limit=5):print(kid.content)foroptinpost.parts():print(opt.content)ListingsYou can use hnpy to view the following listings provided by Hacker News:Top (this is the view shown on the homepage of Hacker News)BestNewAsk HN StoriesShow HN StoriesJob StoriesJust like the special methods of an Item, these methods of a HackerNews
object take an optionallimit=parameter. They iterate over the
specified listing, yielding lazy Items as they go.Example:forpostinhn.best(limit=5):print(post.link)The method names are:top()best()new()ask()show()jobs()UsersA User can be created from a user’s name using theHackerNews.user()method:user=hn.user('jl')Besides the attributesnoted in the Hacker News API
documentation, Users
contain the following attributes and methods:Attribute/method namePurposelinkA URL to view the User’s profile
online.submitted()Iterate over Items created by the
user, with optionallimit=parameter.MiscThe HackerNews object has a methodupdates()which returns an
Updates object, which has iterating methods just like those of other
objects (optionallimit=parameter, objects yielded) which give the
most recently changed item and profiles (more infohere).
Its two methods areitems()andprofiles().The HackerNews object also has a methodmax_item()which returns a
lazy version of the newest Item.CreditsThis package came originally from myA
Botproject. It was adapted to
support lazy loading of objects, and as a consequence has only four
classes (HackerNews, Item, User, Updates), rather than having a class
for every type of Item with a complex inheritance model. This version
also supports Users and Updates, which the old version did not.This package was inspired by the Python Reddit API Wrapper aka PRAW
(docs/source)
in its use of objects and iteration methods andlimit=parameters.