-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.py
94 lines (83 loc) · 2.8 KB
/
example.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
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
from search_hn import SearchHN
hn = SearchHN()
# Get a story, comment, or poll
print(hn.item(1234).get())
"""
{ 'author': 'Alex3917',
'children': [ { 'author': 'pg',
'children': [ { 'author': 'Alex3917',
...
"""
# Get a user
print(hn.user("nicolashahn").get())
"""
{ 'about': 'Full Stack Engineer at Distribute',
'avg': 0.0,
'comment_count': 87,
'created_at': '2013-03-07T21:01:00.000Z',
...
"""
# String together methods to build complex queries
from datetime import datetime
comments = (
hn.comments()
# can be timestamp in seconds or datetime
.created_between(datetime(2017, 9, 1), datetime(2017, 9, 30))
.author("nicolashahn")
.get()
)
for comment in comments:
print(comment)
"""
[{ '_highlightResult': { 'author': { 'matchLevel': 'none',
'matchedWords': [],
'value': 'nicolashahn'},
'comment_text': { 'matchLevel': 'none',
'matchedWords': [],
'value': "I don't know about "
'you or the guy '
'above, but I pretty '
'much always use Uber '
'Pool/Lyft Line over '
'the single rider '
...
"""
# You can inspect the SearchHN object to get query parameters
latest_btc = hn.search("bitcoin").stories().latest()
print(latest_btc)
"""
SearchHN object:
{ 'base_url': 'http://hn.algolia.com/api/v1/search_by_date',
...
'param_obj': {'query': 'bitcoin', 'tags': ['story']},
...
"""
# Or after execution to get the final query url
stories = latest_btc.get(reset=False)
print(latest_btc)
"""
SearchHN object:
{ ...
'full_url': 'http://hn.algolia.com/api/v1/search_by_date?query=bitcoin&tags=story',
...
"""
# top-level JSON fields accessible as attributes
for story in stories:
print(story.title)
"""
Blockstream CEO wants 25,000 BTC ($100M) bet over future of Bitcoin Segwit 1X/2X
What the world's financial bigwigs think about Bitcoin
Why Bitcoin and Ethereum will soon be everywhere (for reals)
...
"""
# get all comments from the latest "Who is hiring" thread:
whoishiring = hn.get_latest_whoishiring_thread().get_story_comments()
python_jobs = [post for post in whoishiring if "python" in post.comment_text.lower()]
print(
"{} jobs available, {} python jobs available".format(
len(whoishiring), len(python_jobs)
)
)
"""
70 jobs available, 45 python jobs available
"""