-
Notifications
You must be signed in to change notification settings - Fork 4
/
hn.py
153 lines (123 loc) · 4.4 KB
/
hn.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import logging
import re
import sqlite3
import sys
import urllib
from concurrent.futures import ThreadPoolExecutor
from html import unescape
from urllib.parse import urlparse
import requests
from main import send_batch, get_user_id
DATABASE_PATH = 'hn.db'
HREF_REGEX = re.compile(r'href="([^"]+)"')
HN_URL = 'https://news.ycombinator.com/'
NUM_ITEMS_TO_FETCH = 500
# Create a sqlite database to store IDs that have been retrieved
def create_id_database():
conn = sqlite3.connect(DATABASE_PATH)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS ids
(id INTEGER PRIMARY KEY)
''')
conn.commit()
conn.close()
def add_ids(hn_ids: list[int]):
conn = sqlite3.connect(DATABASE_PATH)
c = conn.cursor()
hn_ids_prepared = [(i,) for i in hn_ids]
c.executemany('''
INSERT OR REPLACE INTO ids (id) VALUES (?)
''', hn_ids_prepared)
conn.commit()
conn.close()
def ids_exist(hn_ids: list[int]) -> list[int]:
conn = sqlite3.connect(DATABASE_PATH)
c = conn.cursor()
c.execute('''
SELECT id FROM ids WHERE id IN ({})
'''.format(','.join('?' * len(hn_ids))), hn_ids)
ids = [row[0] for row in c.fetchall()]
conn.close()
return ids
def get_hn_urls(most_recent_ids):
"""
Get a batch of 100 URLs from the Hacker News API
"""
# Retrieve all items starting from max_item downwards that haven't been retrieved before
existing_ids = set(ids_exist(most_recent_ids))
non_existing_ids = [i for i in most_recent_ids if i not in existing_ids]
# Call fetch_urls_for_item in parallel using threads
pool = ThreadPoolExecutor(max_workers=25)
futures = [pool.submit(fetch_urls_for_item, item_id) for item_id in non_existing_ids]
urls = []
for future in futures:
urls += future.result()
return urls
def fetch_urls_for_item(item_id):
# Extract URLs from the text
try:
return _try_fetch_urls_for_item(item_id)
except Exception as e:
print(f"Error fetching {item_id}", e)
return []
def _try_fetch_urls_for_item(item_id):
new_urls = []
item = requests.get(f'https://hacker-news.firebaseio.com/v0/item/{item_id}.json', timeout=5).json()
if item is not None:
text = item.get('text', '')
timestamp = item['time'] * 1000
for line in text.split():
# Find all URLs in the text
matches = HREF_REGEX.findall(line)
for url in matches:
# url = match.group(1)
# Decode URL in format https://news.ycombinator.com/item?id=33268319
decoded_url = unescape(url)
try:
parsed_url = urlparse(decoded_url)
except ValueError:
print("Unable to parse URL: ", decoded_url)
continue
if parsed_url.netloc:
new_urls.append((decoded_url, timestamp))
print(f"Found URL in text for item {item_id}: {decoded_url}")
if 'url' in item:
new_urls.append((item['url'], timestamp))
print(f"Found URL in item {item_id} of type {item['type']}: {item['url']}")
return new_urls
def main():
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
user_id = get_user_id()
create_id_database()
max_item = requests.get('https://hacker-news.firebaseio.com/v0/maxitem.json').json()
while True:
items = []
ids_processed = []
while len(items) < 10:
most_recent_ids = list(range(max_item, max_item - NUM_ITEMS_TO_FETCH, -1))
urls_and_timestamps = get_hn_urls(most_recent_ids)
max_item -= NUM_ITEMS_TO_FETCH
if not urls_and_timestamps:
continue
time = max(t for _, t in urls_and_timestamps)
urls = [url for url, _ in urls_and_timestamps]
item = {
'url': HN_URL,
'status': 200,
'timestamp': time,
'content': {
'title': "Hacker News",
'extract': "",
'links': urls,
'extra_links': [],
},
'error': None
}
items.append(item)
ids_processed += most_recent_ids
print(f"Sending batch of {len(items)}")
send_batch(items, user_id)
add_ids(ids_processed)
if __name__ == '__main__':
main()