-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
60 lines (43 loc) · 1.28 KB
/
run.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
"""
hnews-fetcher.
Usage:
hnews-fetcher [<data-type>...] [--from=<id> --to=<id>] [--conns=<count>]
Options:
--conns=<count> Number of concurrent connections [default: 50].
--new-items Only show new comments/stories (not updated/edited ones).
--from=<id> Fetch ID's from this ID.
--to=<id> Fetch ID's to this ID.
"""
import asyncio
import json
import docopt
import requests
from hnews_fetcher.scrape import get_items
event_loop = asyncio.get_event_loop()
def run():
arguments = docopt.docopt(__doc__)
data_types = set(arguments["<data-type>"])
from_id = arguments["--from"]
to_id = arguments["--to"]
conn_count = arguments["--conns"]
if not data_types:
data_types = {"all"}
def output(data):
print(json.dumps(data))
if to_id is None:
to_id = requests.get("https://hacker-news.firebaseio.com/v0/maxitem.json").json()
else:
to_id = int(to_id)
if from_id is None:
from_id = 1
else:
from_id = int(from_id)
if conn_count is None:
conn_count = 50
else:
conn_count = int(conn_count)
event_loop.run_until_complete(
get_items(data_types, output, to_id, from_id=from_id, max_requests=conn_count)
)
if __name__ == "__main__":
run()