-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapispider.py
41 lines (31 loc) · 1.11 KB
/
apispider.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
#!/usr/bin/env python3
import requests
import json
import threading
import time
def getgithublink(id):
url = 'https://hacker-news.firebaseio.com/v0/item/' + str(id) + '.json'
json_result = json.loads(requests.get(url).text)
print(url)
if json_result is None:
return
if 'url' in json_result:
with open('apispider.txt', 'a', ) as gfile:
gfile.write(json.dumps(json_result, indent=2, sort_keys=True) + '\n')
gfile.flush()
def main():
# Technically it's 10 but with the sleep we are saving a little time with one thread
max_threads = 11
maxid = int(requests.get('https://hacker-news.firebaseio.com/v0/maxitem.json?print=pretty').text)
with open('apispider.txt', 'w') as gfile:
pass
while True:
if len(threading.enumerate()) > max_threads:
time.sleep(0.5)
continue
while max_threads >= len(threading.enumerate()):
t = threading.Thread(target=getgithublink,args=(maxid,))
t.setDaemon(True)
t.start()
maxid -= 1
main()