Package: hackernews-client
hackernews-clientUse this wrapper library to download Hacker News headlines into your Python program.Getting StartedThese instructions will get you a copy of the project up and running on your local machine for development and testing purposes.PrerequisitesYou will need the python-requests library so we can make HTTP calls. Install with pip:pip install requestsInstallingIn your Terminalgit clone https://github.com/samisnotinsane/hackernews-client.git
cd hackernews-clientRun the example script:news.pypython news.pythis fetches:Best storiesSearchstorycommentpolljobMost recent items (lastnitems)You may make another copy of this scriptcp news.py ./my_copy_news.py
python my_copy_news.pymodifying it to suit your use case.Running the testsThis package contains tests which verifies the integrity of the internal logic.Unit testsTo run all unit tests, make sure you're in the root directory (hackernews-client). Use Python's built inunittestlibrarypython -m unittest -vIf you have clone frommasterbranch, these should always pass. At the time of testing, sometimes these tests failed withSSLErrorsdue 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 anIssueor aPull Requestto remediate the problem.UsageBegin by importing the library. This example assumes you're working withinnews.pyin the root directory of the repo.from hackernews import hnThe client is then initialised in the following waynews_client = hn.NewsClient()In this case,news_clientobject gives access to all available methods. The data contained in the methods and the instances returned all closely follow theHackerNews API.You may retrieve a list of best10stories in the following waynews_client.get_best_story(fetchMax=10)adjusting the value offetchMaxup to and including a maximum of500.For completeness, full documentation with examples are provided below.DocumentationClass:NewsClientget_itemParameters:NameTypeRequiredDescriptionDefaultitem_idintYesunique item id of Hacker News story, comment etcNoneReturns:
a list ofItem(each of typedict). See class documentation forItemwhich contains a full description of each property.get_top_storyParameters:NameTypeRequiredDescriptionDefaultfetchMaxintYesNumber of stories to fetch, max value = 500500Returns:
a list of top stories asItemfrom the urlhttps://hacker-news.firebaseio.com/v0/topstories.jsonget_ask_storyParameters:NameTypeRequiredDescriptionDefaultfetchMaxintYesNumber of stories to fetch, max value = 200200Returns:
a list of Ask HN stories asItemfrom the urlhttps://hacker-news.firebaseio.com/v0/askstories.jsonget_new_storyParameters:NameTypeRequiredDescriptionDefaultfetchMaxintYesNumber of stories to fetch, max value = 500500Returns:
a list of new stories asItemfrom the urlhttps://hacker-news.firebaseio.com/v0/newstories.jsonget_show_storyParameters:NameTypeRequiredDescriptionDefaultfetchMaxintYesNumber of stories to fetch, max value = 200200Returns:
a list of Show HN stories asItemfrom the urlhttps://hacker-news.firebaseio.com/v0/showstories.jsonget_best_storyParameters:NameTypeRequiredDescriptionDefaultfetchMaxintYesNumber of stories to fetch, max value = 200200Returns:
a list of best stories asItemfrom the urlhttps://hacker-news.firebaseio.com/v0/beststories.jsonget_job_storyParameters:NameTypeRequiredDescriptionDefaultfetchMaxintYesNumber of stories to fetch, max value = 200200Returns:
a list of job stories asItemfrom the urlhttps://hacker-news.firebaseio.com/v0/jobstories.jsonget_max_item_idParameters:NoneReturns:intof the largest item id from the url:https://hacker-news.firebaseio.com/v0/maxitem.jsonget_item_by_idParameters:NameTypeRequiredDescriptionDefaultidintYesuniqueItemid of Hacker News storyNoneReturns:Itemrepresenting the Hacker News story with givenidfetched
from url:https://hacker-news.firebaseio.com/v0/item/<item_id>.jsonget_user_by_idParameters:NameTypeRequiredDescriptionDefaultidintYesuniqueItemid of Hacker News storyNoneReturns:Userrepresenting the Hacker News user with givenidfetched
from url:https://hacker-news.firebaseio.com/v0/user/<user_id>.jsonClass:ItemStories, comments, jobs, Ask HNs and even polls are just items. They're identified by their ids, which are unique integers.FieldDescriptionidThe item's unique id.deletedtrueif 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, inUnix Time.textThe comment, story or poll text. HTML.deadtrueif the item is dead.parentThe comment's parent: either another comment or the relevant story.pollThe pollopt's associated poll.kidslist ofItemof 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:UserUsers are identified by case-sensitive ids. Only users that have public activity (comments or story submissions) on the site are available through the API.FieldDescriptionidThe 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, inUnix Time.karmaThe user's karma.aboutThe user's optional self-description. HTML.submittedList of the user's stories, polls and comments.ScreenshotAuthorsSameen Islam-samisnotinsaneLicenseThis project is licensed under the MIT License - see the LICENSE.md file for detailsAcknowledgmentsAuthor ofhaxorwhose library inspired me to make my first Python libraryAuthor ofpython-packagingwhose tutorial helped me get my head around the Python import system