Repository navigation
Expand file tree
/
Copy pathcollector.py
More file actions
44 lines (38 loc) · 1.69 KB
/
Copy pathcollector.py
File metadata and controls
44 lines (38 loc) · 1.69 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
from crewai import Agent
from pydantic import PrivateAttr
import requests
from bs4 import BeautifulSoup
class PortalScraper:
def fetch(self, url: str) -> str:
resp = requests.get(url, timeout=10)
resp.raise_for_status()
return resp.text
def parse(self, html: str, url: str) -> dict:
soup = BeautifulSoup(html, 'html.parser')
paragraphs = soup.find_all('p')
text = ' '.join(p.get_text(strip=True) for p in paragraphs[:5])
return {'text': text, 'url': url}
class CountryLegalDataCollector(Agent):
_portal: PortalScraper = PrivateAttr(default_factory=PortalScraper)
def __init__(self):
super().__init__(
name="CountryLegalDataCollector",
role="Retrieve legal guidelines and documents from official sources",
goal="Gather and validate current legal data for the selected country",
backstory="You are a legal researcher bot that scrapes official sites for the latest laws."
)
def run(self, inputs: dict) -> dict:
country = inputs.get('country', '').lower()
if country in ('US', 'USA', 'United-States'):
urls = {
'defamation': 'https://www.law.cornell.edu/wex/defamation',
'incitement': 'https://www.law.cornell.edu/wex/incitement',
'hate_speech': 'https://en.wikipedia.org/wiki/Hate_speech_in_the_United_States'
}
else:
raise NotImplementedError(f"Scraping logic not defined for country: {country}")
rules = {}
for category, url in urls.items():
html = self._portal.fetch(url)
rules[category] = self._portal.parse(html, url)
return rules