一个非正式但简单的python客户端,用于hacker news web api。

hackernews-client的Python项目详细描述


HackerNews客户端

CircleCIcodebeat badgeOpen Source Love

使用这个包装器库将黑客新闻标题下载到python程序中。

开始

这些说明将为您在本地计算机上启动和运行项目的副本,以便进行开发和测试。

先决条件

您将需要python请求库,以便我们可以进行http调用。使用pip安装:

pip install requests

安装

在您的终端中

git clone https://github.com/samisnotinsane/hackernews-client.git
cd hackernews-client

运行示例脚本:news.py

python news.py

这将获取:

  • 最佳故事
  • 搜索
    • 故事
    • 注释
    • 投票
    • 工作
  • 最近的项目(最后的n项目)

您可以制作此脚本的另一个副本

cp news.py ./my_copy_news.py
python my_copy_news.py

修改它以适合您的用例。

运行测试

此包包含验证内部逻辑完整性的测试。

单元测试

要运行所有单元测试,请确保位于根目录(hackernews-client)。使用python的内置库unittest

python -m unittest -v

如果您有来自master分支的克隆,这些应该始终通过。在测试时,由于对hacker news api的请求太多太快,有时这些测试会因SSLErrors而失败。等待约20秒,然后再次运行单元测试。

或者,可能是对web api进行了中断性更改,这将需要更新此库。请随意提出IssuePull Request来解决问题。

用法

从导入库开始。本例假设您在repo根目录中的news.py中工作。

from hackernews import hn

然后按以下方式初始化客户机

news_client = hn.NewsClient()

在这种情况下,news_client对象允许访问所有可用的方法。方法中包含的数据和返回的实例都紧跟HackerNews API

您可以通过以下方式检索最佳10故事列表

news_client.get_best_story(fetchMax=10)
< p>将^ {CD10>}的值调整为最大值,包括最大值^ {CD11}}。

为了完整起见,下面提供了完整的文档和示例。

文档

类:NewsClient

  • get_item

参数:

NameTypeRequiredDescriptionDefault
^{}intYesunique item id of Hacker News story, comment etcNone

返回: 一个Item(每个类型都是dict)的列表。请参阅Item的类文档,其中包含每个属性的完整描述。

  • get_top_story

参数:

NameTypeRequiredDescriptionDefault
^{}intYesNumber of stories to fetch, max value = 500500

返回: 来自url的顶级新闻列表https://hacker-news.firebaseio.com/v0/topstories.json

  • get_ask_story

参数:

NameTypeRequiredDescriptionDefault
^{}intYesNumber of stories to fetch, max value = 200200

返回: 来自url的ask hn故事列表Itemhttps://hacker-news.firebaseio.com/v0/askstories.json

  • get_new_story

参数:

NameTypeRequiredDescriptionDefault
^{}intYesNumber of stories to fetch, max value = 500500

返回: 来自url的新故事列表https://hacker-news.firebaseio.com/v0/newstories.json

  • get_show_story

参数:

NameTypeRequiredDescriptionDefault
^{}intYesNumber of stories to fetch, max value = 200200

返回: 以Item形式显示来自url的hn故事列表https://hacker-news.firebaseio.com/v0/showstories.json

  • get_best_story

参数:

NameTypeRequiredDescriptionDefault
^{}intYesNumber of stories to fetch, max value = 200200

返回: 来自url的最佳故事列表https://hacker-news.firebaseio.com/v0/beststories.json

  • get_job_story

参数:

NameTypeRequiredDescriptionDefault
^{}intYesNumber of stories to fetch, max value = 200200

返回: 来自url的工作故事列表https://hacker-news.firebaseio.com/v0/jobstories.json

  • get_max_item_id

参数:

返回: ^来自url的最大项目id的{}:https://hacker-news.firebaseio.com/v0/maxitem.json

  • get_item_by_id

参数:

NameTypeRequiredDescriptionDefault
^{}intYesunique ^{} id of Hacker News storyNone

返回: Item表示给定id获取的黑客新闻 来自url:https://hacker-news.firebaseio.com/v0/item/<item_id>.json

  • get_user_by_id

参数:

NameTypeRequiredDescriptionDefault
^{}intYesunique ^{} id of Hacker News storyNone

返回: User表示给定id获取的黑客新闻用户 来自url:https://hacker-news.firebaseio.com/v0/user/<user_id>.json

类:Item

故事,评论,工作,问hns,甚至民调都只是项目。它们由它们的id标识,它们是唯一的整数。

FieldDescription
idThe item's unique id.
deleted^{} if 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, in Unix Time.
textThe comment, story or poll text. HTML.
dead^{} if the item is dead.
parentThe comment's parent: either another comment or the relevant story.
pollThe pollopt's associated poll.
kidslist of ^{} of 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.

类:User

用户由区分大小写的ID标识。只有在网站上有公共活动(评论或报道提交)的用户才能通过api访问。

FieldDescription
idThe 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, in Unix Time.
karmaThe user's karma.
aboutThe user's optional self-description. HTML.
submittedList of the user's stories, polls and comments.

屏幕截图

Hacker News Client

作者

许可证

此项目在麻省理工学院许可下获得许可-有关详细信息,请参见license.md文件

致谢

  • 作者haxor的库启发我创建了我的第一个python库
  • 作者python-packaging的教程帮助我了解了python导入系统

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
具有作为接口的属性的java Hibernate实体类   在Java中检查int l,r的条件l+1<r的最快方法   java如何更新TornadFX ComboBox Kotlin   java Tomcat未调用控制器api   java在Android Studio中的alertdialog中打开新活动   xml VScode Java/Maven环境问题不同的计算机   java我需要修改循环中的一个文本字符串,这样程序就可以复制粘贴相同的字符串,但数字会增加   java如何从Twilio响应消息中获取内容   从Java5+diamond运算符开始初始化泛型集合的java方法   在java中循环,直到用户按下enter键   java如何找到组件属于哪个面板?   java我想计算一个代码需要的总迭代次数   <data 安卓:type=“*/*”/>不允许使用java字符串类型   解析我有一个带有开始日期和目标日期的字符串,我想在Java中获取日期并保存在变量中   在mongodb中使用ReflectionDBObject类插入java对象?