lilbool/vuln-code-analysis
0
1import os
2import re
3import requests
4from bs4 import BeautifulSoup
5
6def sanitize_filename(filename):
7 """Remove ou substitui caracteres inválidos em nomes de arquivos."""
8 return re.sub(r'[<>:"/\\|?*]', '_', filename)
9
10def fetch_task_links(category_url):
11 """Fetch all task links from Rosetta Code's category page."""
12 response = requests.get(category_url)
13 if response.status_code != 200:
14 print(f"[ERROR] Failed to fetch {category_url}. Status code: {response.status_code}")
15 return []
16
17 soup = BeautifulSoup(response.text, 'html.parser')
18 links = soup.select('.mw-category-group ul li a')
19 return [("https://rosettacode.org" + link['href'], link.text) for link in links]
20
21def fetch_code_from_task(task_url):
22 """Fetch code snippets from a specific task on Rosetta Code."""
23 response = requests.get(task_url)
24 if response.status_code != 200:
25 print(f"[ERROR] Failed to fetch {task_url}. Status code: {response.status_code}")
26 return []
27
28 soup = BeautifulSoup(response.text, 'html.parser')
29 code_blocks = soup.find_all('pre')
30 return [code.text for code in code_blocks]
31
32def save_safe_codes(task_name, codes, save_dir):
33 """Save the safe codes as text files."""
34 os.makedirs(save_dir, exist_ok=True)
35 task_name = sanitize_filename(task_name) # Sanitizar o nome da tarefa
36 for i, code in enumerate(codes):
37 filename = f"{task_name}_{i+1}.txt"
38 filepath = os.path.join(save_dir, filename)
39 try:
40 with open(filepath, 'w', encoding='utf-8') as f:
41 f.write(code)
42 print(f"[SUCCESS] Saved: {filepath}")
43 except Exception as e:
44 print(f"[ERROR] Could not save file {filepath}: {e}")
45
46if __name__ == "__main__":
47 category_url = "https://rosettacode.org/wiki/Category:Programming_Tasks"
48 save_directory = "safe-code-analyzer/safe_codes"
49
50 # Fetch tasks
51 tasks = fetch_task_links(category_url)
52 print(f"[INFO] Found {len(tasks)} tasks on Rosetta Code.")
53
54 # Fetch and save codes
55 for task_url, task_name in tasks[:10]: # Ajuste o número de tarefas a serem processadas
56 print(f"[INFO] Fetching codes for task: {task_name}")
57 codes = fetch_code_from_task(task_url)
58 save_safe_codes(task_name, codes, save_directory)
59 