-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
193 lines (162 loc) · 6.83 KB
/
Copy pathapp.py
File metadata and controls
193 lines (162 loc) · 6.83 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import sys
import json
import hashlib
from curl_cffi import requests as cffi_requests
import datetime
import concurrent.futures
from bs4 import BeautifulSoup
from dotenv import load_dotenv
import litellm
# Add System/src to path to import the Crew
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir, "System", "src"))
from system.crew import System
# Load env variables
load_dotenv()
# Scraper Configuration
DB_FILE = "processed_news.json"
now = datetime.datetime.now()
current_year = now.year
current_month = now.strftime("%B")
# -------------------- DATABASE HELPERS --------------------#
def load_db():
if os.path.exists(DB_FILE):
with open(DB_FILE, "r") as f:
try:
return json.load(f)
except:
return {}
return {}
def save_db(db):
with open(DB_FILE, "w") as f:
json.dump(db, f, indent=4)
def get_hash(text):
return hashlib.md5(text.encode()).hexdigest()
# -------------------- SCRAPING LOGIC ------------------#
def scrape_url(company_name, url):
"""Fetches raw content from company news pages."""
try:
r = cffi_requests.get(url, timeout=15, impersonate="chrome120")
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
text_content = ""
for tag in soup.find_all(['h1', 'h2', 'h3', 'p', 'a']):
text_str = tag.get_text(strip=True)
if len(text_str) > 20:
link = tag.get('href', '')
if link.startswith('/'):
from urllib.parse import urljoin
link = urljoin(url, link)
text_content += f"ITEM: {text_str} | LINK: {link}\n"
return company_name, text_content[:10000], None
except Exception as e:
return company_name, None, str(e)
def clean_data_with_ai(company_name, raw_data):
"""Uses LLM to filter for strategic news items only."""
prompt = f"""
### MISSION:
Extract a CLEAN JSON list of high-value, strategic Industry news from {company_name} relevant to AbbVie.
### STRATEGIC AREAS OF INTEREST:
- Immunology, Oncology, Neuroscience, Aesthetics.
### FORMAT:
JSON LIST ONLY: {{"items": [{{"title": "Full Headline", "link": "Direct URL", "date": "Date found", "snippet" : "Short summary of the news" }}]}}
### RAW DATA:
{raw_data}
"""
try:
full_prompt = f"System: You are a data extraction tool. Extract ONLY strategic news for {current_month} {current_year}. Return {{'items': []}} if nothing found.\n\nUser: {prompt}"
completion_kwargs = {
"model": "anthropic/claude-haiku-4-5-20251001",
"messages": [{"role": "user", "content": full_prompt}],
"temperature": 0
}
response = litellm.completion(**completion_kwargs)
# Extract content from response
text = response.choices[0].message.content.strip()
# Most reliable method is regex to extract json block
import re
match = re.search(r'```(?:json)?\s*(\{[\s\S]*\}|\[[\s\S]*\])\s*```', text)
if match:
text = match.group(1)
else:
# Try to find JSON-like structure if backticks are missing
match = re.search(r'(\{[\s\S]*\}|\[[\s\S]*\])', text)
if match:
text = match.group(1)
data = json.loads(text)
return data.get("items", [])
except Exception as e:
print(f"AI cleaning failed!! Error: {e}")
return []
# -------------------- INTEGRATED EXECUTION ------------------#
def run_system():
print(f"Starting Integrated Scraper & Crew System [{current_month} {current_year}]")
links = {
"1": ["Eli Lilly", "https://www.lilly.com/news/press-releases"],
"2": ["Merck", "https://www.merck.com/media/news/"],
"3": ["Pfizer", "https://www.pfizer.com/news/press-releases/"],
"4": ["National Cancer Institute", f"https://www.cancer.gov/news-events"],
"5": ["FDA", "https://www.fda.gov/search?s=Ovarian+Cancer"],
}
db = load_db()
news_queue = []
# 1. Scrape URLs concurrently
print(f"Scraping {len(links)} sources...")
scraped_results = []
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(scrape_url, name, url) for name, url in links.values()]
for future in concurrent.futures.as_completed(futures):
name, data, err = future.result()
if not err:
scraped_results.append((name, data))
print(f" Fetched: {name}")
else:
print(f" Error fetching: {name} - {err}")
# 2. Clean and Check for New Items
print(f"\nFiltering for strategic news using AI...")
for name, raw_data in scraped_results:
items = clean_data_with_ai(name, raw_data)
for item in items:
title = item.get("title", "")
if not title: continue
item_id = get_hash(f"{name}-{title}")
if item_id not in db:
item['source_name'] = name
news_queue.append(item)
db[item_id] = {"title": title, "date": item.get("date", ""), "processed_at": str(datetime.datetime.now())}
print(f" NEW ITEM FOUND: {title}")
save_db(db)
# 3. Trigger the Crew for each new item
if not news_queue:
print("\nNo new strategic news found. System on standby.")
return
print(f"\nFound {len(news_queue)} new items. Initializing CrewAI Agents...")
for news in news_queue[:1]:
print(f"\n" + "-"*50)
print(f"ANALYZING: {news['title']}")
print(f"SOURCE: {news['source_name']}")
inputs = {
'topic': 'Biopharmaceutical Market Dynamics and Competitor Strategy',
'news_item': f"Source: {news['source_name']}. Title: {news['title']}. Date: {news.get('date', 'N/A')}. URL: {news.get('link', 'N/A')}",
'template': (
"TITLE: [title]\n"
"COMPANY: [company name]\n"
"DRUG: [drug name]\n"
f"DATE: {now.strftime('%B %d, %Y')}\n\n"
"QUICK SUMMARY:\n"
"[A high-level summary of the clinical/strategic event]\n\n"
"KEY TAKEAWAYS:\n"
"- [Takeaway 1]\n"
"- [Takeaway 2]\n"
"- [Takeaway 3]\n\n"
"Find the report attached below."
)
}
try:
System().crew().kickoff(inputs=inputs)
print("The report was created successfully.")
except Exception as e:
print(f"Crew analysis failed for this item: {e}")
if __name__ == "__main__":
run_system()