-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhn.py
More file actions
97 lines (73 loc) · 2.33 KB
/
hn.py
File metadata and controls
97 lines (73 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from typing import cast
import httpx
def get_client():
return httpx.Client(base_url="https://hacker-news.firebaseio.com/v0")
def fetch_top_stories(client: httpx.Client):
r = client.get("/topstories.json")
return r.json()
def fetch_best_stories(client: httpx.Client):
r = client.get("/beststories.json")
return r.json()
def fetch_ask_stories(client: httpx.Client):
r = client.get("/askstories.json")
return r.json()
def fetch_show_stories(client: httpx.Client):
r = client.get("/showstories.json")
return r.json()
def get_item(client: httpx.Client, item_id: int):
r = client.get(f"/item/{item_id}.json")
return r.json()
def get_item_type(item: dict):
item_title = cast(str, item["title"])
item_type = cast(str, item["type"])
if item_type == "story":
if item_title.startswith("Ask HN:"):
return "ask_hn"
elif item_title.startswith("Show HN:"):
return "show_hn"
else:
return "default"
return item_type
def get_top_stories():
client = get_client()
top_stories = fetch_top_stories(client=client)
items = []
for item_id in top_stories:
item = get_item(client=client, item_id=item_id)
if item.get("kids"):
del item["kids"]
items.append(item)
return items
def get_best_stories():
client = get_client()
top_stories = fetch_best_stories(client=client)
items = []
for item_id in top_stories:
item = get_item(client=client, item_id=item_id)
if item.get("kids"):
del item["kids"]
items.append(item)
return items
def get_ask_stories():
client = get_client()
ask_stories = fetch_ask_stories(client=client)
items = []
for item_id in ask_stories:
item = get_item(client=client, item_id=item_id)
if item.get("kids"):
del item["kids"]
items.append(item)
return items
def get_show_stories():
client = get_client()
show_stories = fetch_show_stories(client=client)
items = []
for item_id in show_stories:
item = get_item(client=client, item_id=item_id)
if item.get("kids"):
del item["kids"]
items.append(item)
return items
def save_to_file(file_path: str, file_data: str):
with open(file_path, "w") as f:
f.write(file_data)