一个非正式但简单的python客户端,用于hacker news web api。
hackernews-client的Python项目详细描述
HackerNews客户端
使用这个包装器库将黑客新闻标题下载到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进行了中断性更改,这将需要更新此库。请随意提出Issue或Pull 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
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | unique item id of Hacker News story, comment etc | None |
返回:
一个Item
(每个类型都是dict
)的列表。请参阅Item
的类文档,其中包含每个属性的完整描述。
get_top_story
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | Number of stories to fetch, max value = 500 | 500 |
返回:
来自url的顶级新闻列表https://hacker-news.firebaseio.com/v0/topstories.json
get_ask_story
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | Number of stories to fetch, max value = 200 | 200 |
返回:
来自url的ask hn故事列表Item
https://hacker-news.firebaseio.com/v0/askstories.json
get_new_story
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | Number of stories to fetch, max value = 500 | 500 |
返回:
来自url的新故事列表https://hacker-news.firebaseio.com/v0/newstories.json
get_show_story
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | Number of stories to fetch, max value = 200 | 200 |
返回:
以Item
形式显示来自url的hn故事列表https://hacker-news.firebaseio.com/v0/showstories.json
get_best_story
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | Number of stories to fetch, max value = 200 | 200 |
返回:
来自url的最佳故事列表https://hacker-news.firebaseio.com/v0/beststories.json
get_job_story
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | Number of stories to fetch, max value = 200 | 200 |
返回:
来自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
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | unique ^{ | None |
返回:
Item
表示给定id
获取的黑客新闻
来自url:https://hacker-news.firebaseio.com/v0/item/<item_id>.json
get_user_by_id
参数:
Name | Type | Required | Description | Default |
---|---|---|---|---|
^{ | int | Yes | unique ^{ | None |
返回:
User
表示给定id
获取的黑客新闻用户
来自url:https://hacker-news.firebaseio.com/v0/user/<user_id>.json
类:Item
故事,评论,工作,问hns,甚至民调都只是项目。它们由它们的id标识,它们是唯一的整数。
Field | Description |
---|---|
id | The item's unique id. |
deleted | ^{ |
type | The type of item. One of "job", "story", "comment", "poll", or "pollopt". |
by | The username of the item's author. |
time | Creation date of the item, in Unix Time. |
text | The comment, story or poll text. HTML. |
dead | ^{ |
parent | The comment's parent: either another comment or the relevant story. |
poll | The pollopt's associated poll. |
kids | list of ^{ |
url | The URL of the story. |
score | The story's score, or the votes for a pollopt. |
title | The title of the story, poll or job. |
parts | A list of related pollopts, in display order. |
descendants | In the case of stories or polls, the total comment count. |
类:User
用户由区分大小写的ID标识。只有在网站上有公共活动(评论或报道提交)的用户才能通过api访问。
Field | Description |
---|---|
id | The user's unique username. Case-sensitive. Required. |
delay | Delay in minutes between a comment's creation and its visibility to other users. |
created | Creation date of the user, in Unix Time. |
karma | The user's karma. |
about | The user's optional self-description. HTML. |
submitted | List of the user's stories, polls and comments. |
屏幕截图
作者
- sameen islam-samisnotinsane
许可证
此项目在麻省理工学院许可下获得许可-有关详细信息,请参见license.md文件
致谢
- 作者haxor的库启发我创建了我的第一个python库
- 作者python-packaging的教程帮助我了解了python导入系统