-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.npm.py
291 lines (254 loc) · 12.3 KB
/
scrape.npm.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import os
import json
import asyncio
import time
from datetime import datetime, timezone
import re
from crawl4ai import AsyncWebCrawler
from crawl4ai.extraction_strategy import LLMExtractionStrategy
from bs4 import BeautifulSoup
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY not found. Ensure it is set in the .env file.")
# Set the OpenAI API key for ChatGPT
client = OpenAI(api_key=api_key)
# Define data directories
DATA_DIR = "data"
RAW_HTML_DIR = os.path.join(DATA_DIR, "raw_html/docs.npmjs.com")
PROCESSED_DIR = os.path.join(DATA_DIR, "processed/docs.npmjs.com")
STATS_FILE = os.path.join(DATA_DIR, "processed/docs.npmjs.com/stats.json")
# Create necessary directories
os.makedirs(DATA_DIR, exist_ok=True)
os.makedirs(RAW_HTML_DIR, exist_ok=True)
os.makedirs(PROCESSED_DIR, exist_ok=True)
def save_raw_html(url, html_content):
"""Save raw HTML content to file."""
filename = re.sub(r'[^\w\-_.]', '_', url.split('//')[1]) + '.html'
filepath = os.path.join(RAW_HTML_DIR, filename)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(html_content)
return filepath
def update_stats(url, fetch_time, store_time, success, error=None, jsonl_entries=0):
"""Update the statistics file with new data."""
try:
if os.path.exists(STATS_FILE):
with open(STATS_FILE, 'r') as f:
stats = json.load(f)
# Skip if it's the first run with example data
if len(stats["scraping_stats"]["pages"]) == 1 and stats["scraping_stats"]["pages"][0]["url"] == "https://example.com/package/test":
stats["scraping_stats"]["pages"] = []
stats["scraping_stats"]["total_pages_processed"] = 0
stats["scraping_stats"]["total_success"] = 0
stats["scraping_stats"]["total_failures"] = 0
stats["scraping_stats"]["average_fetch_time_ms"] = 0
stats["scraping_stats"]["average_store_time_ms"] = 0
stats["scraping_stats"]["total_jsonl_entries"] = 0
stats["scraping_stats"]["average_entries_per_page"] = 0
else:
stats = {
"scraping_stats": {
"total_pages_processed": 0,
"total_success": 0,
"total_failures": 0,
"average_fetch_time_ms": 0,
"average_store_time_ms": 0,
"total_jsonl_entries": 0,
"average_entries_per_page": 0,
"pages": []
}
}
# Update counters
stats["scraping_stats"]["total_pages_processed"] += 1
if success:
stats["scraping_stats"]["total_success"] += 1
stats["scraping_stats"]["total_jsonl_entries"] += jsonl_entries
else:
stats["scraping_stats"]["total_failures"] += 1
# Calculate new averages
total_pages = len(stats["scraping_stats"]["pages"])
if total_pages > 0:
current_fetch_avg = stats["scraping_stats"]["average_fetch_time_ms"]
current_store_avg = stats["scraping_stats"]["average_store_time_ms"]
stats["scraping_stats"]["average_fetch_time_ms"] = (current_fetch_avg * total_pages + fetch_time) / (total_pages + 1)
stats["scraping_stats"]["average_store_time_ms"] = (current_store_avg * total_pages + store_time) / (total_pages + 1)
if success:
stats["scraping_stats"]["average_entries_per_page"] = stats["scraping_stats"]["total_jsonl_entries"] / stats["scraping_stats"]["total_success"]
else:
stats["scraping_stats"]["average_fetch_time_ms"] = fetch_time
stats["scraping_stats"]["average_store_time_ms"] = store_time
if success:
stats["scraping_stats"]["average_entries_per_page"] = jsonl_entries
# Add new page entry
page_entry = {
"timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z"),
"url": url,
"fetch_time_ms": fetch_time,
"store_time_ms": store_time,
"total_time_ms": fetch_time + store_time,
"success": success,
"error": str(error) if error else None,
"jsonl_entries": jsonl_entries
}
stats["scraping_stats"]["pages"].append(page_entry)
# Save updated stats
with open(STATS_FILE, 'w') as f:
json.dump(stats, f, indent=2)
print(f"Updated stats for {url} - Total pages: {stats['scraping_stats']['total_pages_processed']}, "
f"Success: {stats['scraping_stats']['total_success']}, "
f"Failures: {stats['scraping_stats']['total_failures']}, "
f"Total JSONL entries: {stats['scraping_stats']['total_jsonl_entries']}, "
f"Avg entries/page: {stats['scraping_stats']['average_entries_per_page']:.1f}")
except Exception as e:
print(f"Error updating stats: {e}")
def filter_relevant_links(links, base_url="https://docs.npmjs.com"):
"""Filter links to include only those that belong to the given base URL."""
relevant_links = [link for link in links if link.startswith(base_url)]
return relevant_links
def extract_links(html_content):
"""Extract all relevant links from the provided HTML content."""
soup = BeautifulSoup(html_content, "html.parser")
links = []
for a_tag in soup.find_all("a", href=True):
href = a_tag["href"]
# Only keep relevant links (absolute or relative URLs starting with "/")
if href.startswith("http") or href.startswith("/"):
links.append(href if href.startswith("http") else f"https://docs.npmjs.com{href}")
return list(set(links)) # Remove duplicates
async def fetch_content(url):
"""Fetch content from the given URL using AsyncWebCrawler."""
fetch_start = time.time()
try:
print(f"\nStarting fetch for URL: {url}")
async with AsyncWebCrawler(verbose=True) as crawler:
result = await crawler.arun(
url=url,
extraction_strategy=LLMExtractionStrategy(
provider="openai/gpt-4o",
api_token=api_key,
instruction="Extract the most relevant content about npm commands, features, and documentation.",
),
bypass_cache=True,
)
fetch_time = (time.time() - fetch_start) * 1000 # Convert to milliseconds
print(f"Fetch completed in {fetch_time:.2f}ms - URL: {url}")
# Start measuring store time for all storage operations
store_start = time.time()
# Save raw HTML
html_filepath = save_raw_html(url, result.html)
print(f"Saved raw HTML to: {html_filepath}")
# Process and save JSON content
extracted_content = json.loads(result.extracted_content)
print(f"Successfully extracted content with {len(extracted_content)} sections")
content_filename = re.sub(r'[^\w\-_.]', '_', url.split('//')[1]) + '.json'
content_filepath = os.path.join(PROCESSED_DIR, content_filename)
with open(content_filepath, 'w', encoding='utf-8') as f:
json.dump(extracted_content, f, indent=2)
print(f"Saved processed content to: {content_filepath}")
# Process and save JSONL content
jsonl_path = os.path.join(PROCESSED_DIR, "npm_documentation.jsonl")
print(f"Processing content for JSONL generation...")
formatted_entries = process_content_for_jsonl(extracted_content, url)
num_entries = 0
if formatted_entries:
try:
with open(jsonl_path, 'a', encoding='utf-8') as f:
for entry in formatted_entries:
json_line = json.dumps(entry, ensure_ascii=False)
f.write(json_line + "\n")
num_entries += 1
print(f"Successfully appended {num_entries} entries to JSONL file: {jsonl_path}")
except Exception as e:
print(f"Error writing to JSONL file: {e}")
else:
print(f"No entries generated for JSONL file from URL: {url}")
# Calculate total store time for all operations
store_time = (time.time() - store_start) * 1000 # Convert to milliseconds
print(f"Content processing and storage completed in {store_time:.2f}ms")
# Update statistics with both fetch and store times
update_stats(url, fetch_time, store_time, True, jsonl_entries=num_entries)
return extracted_content
except Exception as e:
total_time = (time.time() - fetch_start) * 1000
print(f"Error processing {url}: {str(e)}")
update_stats(url, total_time, 0, False, error=str(e), jsonl_entries=0)
raise
def process_content_for_jsonl(content, url):
"""Process a single page's content into JSONL format."""
print(f"\nProcessing content for JSONL - URL: {url}")
combined_content = "\n\n".join(
f"Section {section.get('index')}:\n{' '.join(section.get('content', [])).strip()}"
for section in content
if section.get('content')
)
if not combined_content:
print(f"No valid content to process for {url}")
return []
print(f"Sending content to ChatGPT for processing - URL: {url}")
prompt = (
f"Based on the following npm documentation content, create questions a user might ask and answer them in detail. "
f"Focus on practical npm usage, package management, and common workflows.\n\n"
f"Content:\n{combined_content}\n\n"
f"Format the output as a single valid JSON array where each entry matches:\n"
f'{{"text": "System: You are an AI assistant specialized in npm package management. Provide detailed, practical answers about npm usage.\\n\\nUser: [Generated question]\\n\\nAssistant: [Generated answer]"}}'
)
try:
print(f"Making API call to ChatGPT...")
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an AI assistant specialized in npm package management."},
{"role": "user", "content": prompt},
],
max_tokens=3000,
temperature=0.7,
)
gpt_output = response.choices[0].message.content.strip()
print(f"Received response from ChatGPT - Length: {len(gpt_output)} chars")
print(f"First 200 chars of response: {gpt_output[:200]}")
try:
entries = json.loads(gpt_output)
print(f"Successfully parsed JSON with {len(entries)} Q&A pairs for {url}")
if len(entries) > 0:
print(f"Sample entry: {json.dumps(entries[0], indent=2)}")
return entries
except json.JSONDecodeError as e:
print(f"JSON decode error for {url}: {e}")
print("Raw GPT output:", gpt_output)
return []
except Exception as e:
print(f"Error processing content with ChatGPT for {url}: {e}")
return []
async def main():
base_url = "https://docs.npmjs.com"
print(f"Fetching content from base URL: {base_url}")
async with AsyncWebCrawler(verbose=True) as crawler:
result = await crawler.arun(
url=base_url,
extraction_strategy=LLMExtractionStrategy(
provider="openai/gpt-4o",
api_token=api_key,
instruction="Extract the HTML structure to identify all relevant npm documentation links.",
),
bypass_cache=True,
)
print("Raw HTML content fetched!")
links = filter_relevant_links(extract_links(result.html))
print(f"Found {len(links)} links.")
print("Links:", links)
if not links:
print("No links found. Exiting...")
return
for idx, link in enumerate(links):
print(f"Processing link {idx + 1}/{len(links)}: {link}")
try:
await fetch_content(link)
except Exception as e:
print(f"Error processing link {link}: {e}")
continue
print("Scraping completed!")
if __name__ == "__main__":
asyncio.run(main())