-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhn-ratio.py
executable file
·54 lines (50 loc) · 1.79 KB
/
hn-ratio.py
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
#!/usr/local/bin/python3
from hackernews import HackerNews
import json
import datetime
import os.path
from pathlib import Path
hn = HackerNews()
ITEM_LIMIT = 50
WIDTH = 50
SCORE_THRESHOLD = 20
def story_serializer(story):
story_attrs = {
"item_id": story.item_id,
"title": story.title,
"by": story.by,
"submission_time": story.submission_time.isoformat(),
"score": story.score,
"comments": story.descendants,
"ratio": story.ratio,
"blocks": story.blocks,
"url": story.url,
"hn_url": story.hn_url
}
return story_attrs
def execute(date):
fn_json = os.path.join(os.path.dirname(__file__), "results", "{0}.json".format(date).lower())
json_file = Path(fn_json)
if json_file.is_file():
print("Data already exist for today, exiting.")
return
stories = []
for story_id in hn.top_stories(limit=ITEM_LIMIT):
story = hn.get_item(story_id)
if story.score < SCORE_THRESHOLD:
# we filter out low score posts that are "promoted" by HN temporily but not really "top"
# as well as job posts
continue
story.ratio = 0
if story.descendants is not None:
story.ratio = story.descendants / story.score
story.blocks = int(WIDTH * story.ratio)
stories.append(story)
story.hn_url = "https://news.ycombinator.com/item?id=" + str(story.item_id)
stories.sort(key=lambda s:s.ratio, reverse=True)
print("Number of stroies after filtering: " + str(len(stories)))
with open(fn_json, "w+") as f:
f.write(json.dumps([story_serializer(story) for story in stories], indent=4, sort_keys=True))
if __name__ == "__main__":
current_date = datetime.datetime.now().strftime("%Y-%b-%d")
execute(current_date)