Spaces:
Running
Running
File size: 10,899 Bytes
89ae94f |
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
"""
Service for dark web content operations.
"""
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy import func, or_, text
from datetime import datetime
from typing import List, Optional, Dict, Any, Union
from src.models.dark_web_content import DarkWebContent, DarkWebMention, ContentType, ContentStatus
from src.models.threat import Threat, ThreatCategory, ThreatSeverity, ThreatStatus
from src.api.schemas import PaginationParams
async def create_content(
db: AsyncSession,
url: str,
content: str,
title: Optional[str] = None,
content_type: ContentType = ContentType.OTHER,
content_status: ContentStatus = ContentStatus.NEW,
source_name: Optional[str] = None,
source_type: Optional[str] = None,
language: Optional[str] = None,
relevance_score: float = 0.0,
sentiment_score: float = 0.0,
entity_data: Optional[str] = None,
) -> DarkWebContent:
"""
Create a new dark web content entry.
Args:
db: Database session
url: URL of the content
content: Text content
title: Title of the content
content_type: Type of content
content_status: Status of content
source_name: Name of the source
source_type: Type of source
language: Language of the content
relevance_score: Relevance score (0-1)
sentiment_score: Sentiment score (-1 to 1)
entity_data: JSON string of extracted entities
Returns:
DarkWebContent: Created content
"""
# Extract domain from URL if possible
domain = None
if url:
try:
from urllib.parse import urlparse
parsed_url = urlparse(url)
domain = parsed_url.netloc
except:
pass
db_content = DarkWebContent(
url=url,
domain=domain,
title=title,
content=content,
content_type=content_type,
content_status=content_status,
source_name=source_name,
source_type=source_type,
language=language,
scraped_at=datetime.utcnow(),
relevance_score=relevance_score,
sentiment_score=sentiment_score,
entity_data=entity_data,
)
db.add(db_content)
await db.commit()
await db.refresh(db_content)
return db_content
async def get_content_by_id(db: AsyncSession, content_id: int) -> Optional[DarkWebContent]:
"""
Get dark web content by ID.
Args:
db: Database session
content_id: Content ID
Returns:
Optional[DarkWebContent]: Content or None if not found
"""
result = await db.execute(select(DarkWebContent).filter(DarkWebContent.id == content_id))
return result.scalars().first()
async def get_contents(
db: AsyncSession,
pagination: PaginationParams,
content_type: Optional[List[ContentType]] = None,
content_status: Optional[List[ContentStatus]] = None,
source_name: Optional[str] = None,
search_query: Optional[str] = None,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
) -> List[DarkWebContent]:
"""
Get dark web contents with filtering and pagination.
Args:
db: Database session
pagination: Pagination parameters
content_type: Filter by content type
content_status: Filter by content status
source_name: Filter by source name
search_query: Search in title and content
from_date: Filter by scraped_at >= from_date
to_date: Filter by scraped_at <= to_date
Returns:
List[DarkWebContent]: List of dark web contents
"""
query = select(DarkWebContent)
# Apply filters
if content_type:
query = query.filter(DarkWebContent.content_type.in_(content_type))
if content_status:
query = query.filter(DarkWebContent.content_status.in_(content_status))
if source_name:
query = query.filter(DarkWebContent.source_name == source_name)
if search_query:
search_filter = or_(
DarkWebContent.title.ilike(f"%{search_query}%"),
DarkWebContent.content.ilike(f"%{search_query}%")
)
query = query.filter(search_filter)
if from_date:
query = query.filter(DarkWebContent.scraped_at >= from_date)
if to_date:
query = query.filter(DarkWebContent.scraped_at <= to_date)
# Apply pagination
query = query.order_by(DarkWebContent.scraped_at.desc())
query = query.offset((pagination.page - 1) * pagination.size).limit(pagination.size)
result = await db.execute(query)
return result.scalars().all()
async def count_contents(
db: AsyncSession,
content_type: Optional[List[ContentType]] = None,
content_status: Optional[List[ContentStatus]] = None,
source_name: Optional[str] = None,
search_query: Optional[str] = None,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
) -> int:
"""
Count dark web contents with filtering.
Args:
db: Database session
content_type: Filter by content type
content_status: Filter by content status
source_name: Filter by source name
search_query: Search in title and content
from_date: Filter by scraped_at >= from_date
to_date: Filter by scraped_at <= to_date
Returns:
int: Count of dark web contents
"""
query = select(func.count(DarkWebContent.id))
# Apply filters (same as in get_contents)
if content_type:
query = query.filter(DarkWebContent.content_type.in_(content_type))
if content_status:
query = query.filter(DarkWebContent.content_status.in_(content_status))
if source_name:
query = query.filter(DarkWebContent.source_name == source_name)
if search_query:
search_filter = or_(
DarkWebContent.title.ilike(f"%{search_query}%"),
DarkWebContent.content.ilike(f"%{search_query}%")
)
query = query.filter(search_filter)
if from_date:
query = query.filter(DarkWebContent.scraped_at >= from_date)
if to_date:
query = query.filter(DarkWebContent.scraped_at <= to_date)
result = await db.execute(query)
return result.scalar()
async def create_mention(
db: AsyncSession,
content_id: int,
keyword: str,
keyword_category: Optional[str] = None,
context: Optional[str] = None,
snippet: Optional[str] = None,
mention_type: Optional[str] = None,
confidence: float = 0.0,
is_verified: bool = False,
) -> DarkWebMention:
"""
Create a new dark web mention.
Args:
db: Database session
content_id: ID of the content where the mention was found
keyword: Keyword that was mentioned
keyword_category: Category of the keyword
context: Text surrounding the mention
snippet: Extract of text containing the mention
mention_type: Type of mention
confidence: Confidence score (0-1)
is_verified: Whether the mention is verified
Returns:
DarkWebMention: Created mention
"""
db_mention = DarkWebMention(
content_id=content_id,
keyword=keyword,
keyword_category=keyword_category,
context=context,
snippet=snippet,
mention_type=mention_type,
confidence=confidence,
is_verified=is_verified,
)
db.add(db_mention)
await db.commit()
await db.refresh(db_mention)
return db_mention
async def get_mention_by_id(db: AsyncSession, mention_id: int) -> Optional[DarkWebMention]:
"""
Get dark web mention by ID.
Args:
db: Database session
mention_id: Mention ID
Returns:
Optional[DarkWebMention]: Mention or None if not found
"""
result = await db.execute(select(DarkWebMention).filter(DarkWebMention.id == mention_id))
return result.scalars().first()
async def get_mentions(
db: AsyncSession,
pagination: PaginationParams,
keyword: Optional[str] = None,
content_id: Optional[int] = None,
is_verified: Optional[bool] = None,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
) -> List[DarkWebMention]:
"""
Get dark web mentions with filtering and pagination.
Args:
db: Database session
pagination: Pagination parameters
keyword: Filter by keyword
content_id: Filter by content ID
is_verified: Filter by verification status
from_date: Filter by created_at >= from_date
to_date: Filter by created_at <= to_date
Returns:
List[DarkWebMention]: List of dark web mentions
"""
query = select(DarkWebMention)
# Apply filters
if keyword:
query = query.filter(DarkWebMention.keyword.ilike(f"%{keyword}%"))
if content_id:
query = query.filter(DarkWebMention.content_id == content_id)
if is_verified is not None:
query = query.filter(DarkWebMention.is_verified == is_verified)
if from_date:
query = query.filter(DarkWebMention.created_at >= from_date)
if to_date:
query = query.filter(DarkWebMention.created_at <= to_date)
# Apply pagination
query = query.order_by(DarkWebMention.created_at.desc())
query = query.offset((pagination.page - 1) * pagination.size).limit(pagination.size)
result = await db.execute(query)
return result.scalars().all()
async def create_threat_from_content(
db: AsyncSession,
content_id: int,
title: str,
description: str,
severity: ThreatSeverity,
category: ThreatCategory,
confidence_score: float = 0.0,
) -> Threat:
"""
Create a threat from dark web content.
Args:
db: Database session
content_id: ID of the content
title: Threat title
description: Threat description
severity: Threat severity
category: Threat category
confidence_score: Confidence score (0-1)
Returns:
Threat: Created threat
"""
# Get the content
content = await get_content_by_id(db, content_id)
if not content:
raise ValueError(f"Content with ID {content_id} not found")
# Create the threat
from src.api.services.threat_service import create_threat
threat = await create_threat(
db=db,
title=title,
description=description,
severity=severity,
category=category,
status=ThreatStatus.NEW,
source_url=content.url,
source_name=content.source_name,
source_type=content.source_type,
confidence_score=confidence_score,
)
return threat |