-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
172 lines (138 loc) · 5.09 KB
/
scraper.py
File metadata and controls
172 lines (138 loc) · 5.09 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Scraper layer — pulls data from RSS, web pages, and APIs without any LLM."""
import hashlib
import json
import re
from collections import Counter
from datetime import datetime
from urllib.parse import urlparse
import feedparser
import requests
from bs4 import BeautifulSoup
import db
# Common stop words to filter out during keyword extraction
STOP_WORDS = set("""
a about above after again against all am an and any are aren't as at be because
been before being below between both but by can't cannot could couldn't did didn't
do does doesn't doing don't down during each few for from further get got had hadn't
has hasn't have haven't having he he'd he'll he's her here here's hers herself him
himself his how how's i i'd i'll i'm i've if in into is isn't it it's its itself
let's me more most mustn't my myself no nor not of off on once only or other ought
our ours ourselves out over own same shan't she she'd she'll she's should shouldn't
so some such than that that's the their theirs them themselves then there there's
these they they'd they'll they're they've this those through to too under until up
very was wasn't we we'd we'll we're we've were weren't what what's when when's where
where's which while who who's whom why why's will with won't would wouldn't you you'd
you'll you're you've your yours yourself yourselves just also like using used use
new one two first get got can will may
""".split())
def extract_text(html):
"""Extract clean text from HTML."""
soup = BeautifulSoup(html, "html.parser")
for tag in soup(["script", "style", "nav", "footer", "header", "aside"]):
tag.decompose()
text = soup.get_text(separator=" ", strip=True)
text = re.sub(r'\s+', ' ', text)
return text
def extract_keywords(text, top_n=15):
"""Extract keywords using simple TF scoring — no LLM needed."""
words = re.findall(r'[a-z]{3,}', text.lower())
words = [w for w in words if w not in STOP_WORDS and len(w) > 2]
counts = Counter(words)
return [word for word, _ in counts.most_common(top_n)]
def content_hash(text):
"""Generate hash for deduplication."""
return hashlib.sha256(text.strip().lower().encode()).hexdigest()[:16]
def scrape_rss(source):
"""Scrape an RSS feed source."""
url = source["url"]
feed = feedparser.parse(url)
added = 0
for entry in feed.entries:
title = entry.get("title", "Untitled")
link = entry.get("link", "")
summary = entry.get("summary", "")
author = entry.get("author", "")
# Get full content if available
content = ""
if hasattr(entry, "content"):
content = entry.content[0].get("value", "")
text = extract_text(content or summary)
if len(text) < 50:
continue
h = content_hash(text)
keywords = extract_keywords(text)
doc_id = db.insert_document(
source_id=source["id"],
title=title,
content=text,
url=link,
author=author,
keywords=keywords,
doc_hash=h
)
if doc_id:
added += 1
return added
def scrape_web(source):
"""Scrape a web page."""
url = source["url"]
config = json.loads(source["config"]) if source["config"] else {}
headers = {
"User-Agent": "KnowledgeEngine/1.0 (research bot)"
}
try:
resp = requests.get(url, headers=headers, timeout=15)
resp.raise_for_status()
except requests.RequestException as e:
print(f" Error fetching {url}: {e}")
return 0
soup = BeautifulSoup(resp.text, "html.parser")
title = soup.title.string if soup.title else urlparse(url).netloc
# If selector specified in config, use it
selector = config.get("selector")
if selector:
elements = soup.select(selector)
text = " ".join(extract_text(str(el)) for el in elements)
else:
text = extract_text(resp.text)
if len(text) < 50:
return 0
h = content_hash(text)
keywords = extract_keywords(text)
doc_id = db.insert_document(
source_id=source["id"],
title=title,
content=text,
url=url,
keywords=keywords,
doc_hash=h
)
return 1 if doc_id else 0
def scrape_source(source):
"""Route to the right scraper based on source type."""
source_type = source["type"]
if source_type == "rss":
return scrape_rss(source)
elif source_type == "web":
return scrape_web(source)
else:
print(f" Unknown source type: {source_type}")
return 0
def scrape_all():
"""Scrape all enabled sources."""
sources = db.get_sources(enabled_only=True)
total = 0
for source in sources:
print(f" Scraping: {source['name']} ({source['type']})")
added = scrape_source(source)
total += added
print(f" Added {added} documents")
# Update last_scraped
conn = db.get_conn()
conn.execute(
"UPDATE sources SET last_scraped = datetime('now') WHERE id = ?",
(source["id"],)
)
conn.commit()
conn.close()
return total