File size: 5,941 Bytes
48922fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
OSINT engine for comprehensive information gathering.
"""
from typing import Dict, List, Any, Optional
import asyncio
import json
from dataclasses import dataclass
import holehe.core as holehe
from sherlock import sherlock
import face_recognition
import numpy as np
from PIL import Image
import io
import requests
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
import whois
from datetime import datetime
from tenacity import retry, stop_after_attempt, wait_exponential

@dataclass
class PersonInfo:
    name: str
    age: Optional[int] = None
    location: Optional[str] = None
    gender: Optional[str] = None
    social_profiles: List[Dict[str, str]] = None
    images: List[str] = None
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            "name": self.name,
            "age": self.age,
            "location": self.location,
            "gender": self.gender,
            "social_profiles": self.social_profiles or [],
            "images": self.images or []
        }

class OSINTEngine:
    def __init__(self):
        self.geolocator = Nominatim(user_agent="intelligent_search_engine")
        self.known_platforms = [
            "Twitter", "Instagram", "Facebook", "LinkedIn", "GitHub",
            "Reddit", "YouTube", "TikTok", "Pinterest", "Snapchat",
            "Twitch", "Medium", "Dev.to", "Stack Overflow"
        ]
    
    @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
    async def search_username(self, username: str) -> Dict[str, Any]:
        """Search for username across multiple platforms."""
        results = []
        
        # Use holehe for email-based search
        email = f"{username}@gmail.com"  # Example email
        holehe_results = await holehe.check_email(email)
        
        # Use sherlock for username search
        sherlock_results = sherlock.sherlock(username, self.known_platforms, verbose=False)
        
        # Combine results
        for platform, data in {**holehe_results, **sherlock_results}.items():
            if data.get("exists", False):
                results.append({
                    "platform": platform,
                    "url": data.get("url", ""),
                    "confidence": data.get("confidence", "high")
                })
        
        return {
            "username": username,
            "found_on": results
        }
    
    async def search_person(self, name: str, location: Optional[str] = None, 
                          age: Optional[int] = None, gender: Optional[str] = None) -> PersonInfo:
        """Search for information about a person."""
        person = PersonInfo(
            name=name,
            age=age,
            location=location,
            gender=gender
        )
        
        # Initialize social profiles list
        person.social_profiles = []
        
        # Search for social media profiles
        username_variants = [
            name.replace(" ", ""),
            name.replace(" ", "_"),
            name.replace(" ", "."),
            name.lower().replace(" ", "")
        ]
        
        for username in username_variants:
            results = await self.search_username(username)
            person.social_profiles.extend(results.get("found_on", []))
        
        return person
    
    @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
    async def analyze_image(self, image_data: bytes) -> Dict[str, Any]:
        """Analyze an image for faces and other identifiable information."""
        try:
            # Load image
            image = face_recognition.load_image_file(io.BytesIO(image_data))
            
            # Detect faces
            face_locations = face_recognition.face_locations(image)
            face_encodings = face_recognition.face_encodings(image, face_locations)
            
            results = {
                "faces_found": len(face_locations),
                "faces": []
            }
            
            # Analyze each face
            for i, (face_encoding, face_location) in enumerate(zip(face_encodings, face_locations)):
                face_data = {
                    "location": {
                        "top": face_location[0],
                        "right": face_location[1],
                        "bottom": face_location[2],
                        "left": face_location[3]
                    }
                }
                results["faces"].append(face_data)
            
            return results
        except Exception as e:
            return {"error": str(e)}
    
    async def search_location(self, location: str) -> Dict[str, Any]:
        """Gather information about a location."""
        try:
            # Geocode the location
            location_data = self.geolocator.geocode(location, timeout=10)
            
            if not location_data:
                return {"error": "Location not found"}
            
            return {
                "address": location_data.address,
                "latitude": location_data.latitude,
                "longitude": location_data.longitude,
                "raw": location_data.raw
            }
        except GeocoderTimedOut:
            return {"error": "Geocoding service timed out"}
        except Exception as e:
            return {"error": str(e)}
    
    async def analyze_domain(self, domain: str) -> Dict[str, Any]:
        """Analyze a domain for WHOIS and other information."""
        try:
            w = whois.whois(domain)
            return {
                "registrar": w.registrar,
                "creation_date": w.creation_date,
                "expiration_date": w.expiration_date,
                "last_updated": w.updated_date,
                "status": w.status,
                "name_servers": w.name_servers
            }
        except Exception as e:
            return {"error": str(e)}