File size: 9,363 Bytes
d7a5f99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""LinkedIn Data Fetching Module

Provides functions to fetch and enrich LinkedIn profile data using RapidAPI.
Includes caching to minimize API usage and respect rate limits.

Usage:
    from linkedin_data import fetch_linkedin_profile, enrich_lead
    
    profile = fetch_linkedin_profile("https://linkedin.com/in/username")
    enriched = enrich_lead(lead_data)
"""

import os
import json
import time
import requests
from pathlib import Path
from typing import Dict, Optional, Any


class LinkedInDataFetcher:
    """Handles LinkedIn profile data fetching via RapidAPI"""
    
    def __init__(self, api_key: Optional[str] = None, username: Optional[str] = None):
        """Initialize LinkedIn data fetcher
        
        Args:
            api_key: RapidAPI key (defaults to RAPIDAPI_KEY env var)
            username: Current user's username for cache directory
        """
        self.api_key = api_key or os.environ.get('RAPIDAPI_KEY', '')
        self.username = username
        self.base_url = "https://fresh-linkedin-profile-data.p.rapidapi.com"
        self.headers = {
            "x-rapidapi-key": self.api_key,
            "x-rapidapi-host": "fresh-linkedin-profile-data.p.rapidapi.com"
        }
        
        # Set up cache directory for current user
        if self.username:
            self.cache_dir = Path(f"/data/users/{self.username}/linkedin_cache")
            self.cache_dir.mkdir(parents=True, exist_ok=True)
        else:
            self.cache_dir = None
    
    def _get_cache_path(self, linkedin_url: str) -> Optional[Path]:
        """Get cache file path for a LinkedIn URL"""
        if not self.cache_dir:
            return None
        
        # Create a safe filename from the LinkedIn URL
        safe_name = linkedin_url.replace('https://', '').replace('/', '_').replace(':', '_')
        return self.cache_dir / f"{safe_name}.json"
    
    def _load_from_cache(self, linkedin_url: str) -> Optional[Dict]:
        """Load profile data from cache if available and not expired"""
        cache_path = self._get_cache_path(linkedin_url)
        if not cache_path or not cache_path.exists():
            return None
        
        try:
            with open(cache_path, 'r') as f:
                cached_data = json.load(f)
            
            # Check if cache is still valid (30 days)
            cache_time = cached_data.get('cached_at', 0)
            if time.time() - cache_time < (30 * 24 * 60 * 60):  # 30 days in seconds
                return cached_data.get('data')
        except Exception as e:
            print(f"Error loading cache: {e}")
        
        return None
    
    def _save_to_cache(self, linkedin_url: str, data: Dict) -> None:
        """Save profile data to cache"""
        cache_path = self._get_cache_path(linkedin_url)
        if not cache_path:
            return
        
        try:
            cached_data = {
                'cached_at': time.time(),
                'url': linkedin_url,
                'data': data
            }
            with open(cache_path, 'w') as f:
                json.dump(cached_data, f, indent=2)
        except Exception as e:
            print(f"Error saving cache: {e}")
    
    def fetch_profile(self, linkedin_url: str, use_cache: bool = True) -> Dict[str, Any]:
        """Fetch LinkedIn profile data
        
        Args:
            linkedin_url: Full LinkedIn profile URL
            use_cache: Whether to use cached data if available
        
        Returns:
            Dictionary containing profile data or error information
        """
        # Check cache first
        if use_cache:
            cached = self._load_from_cache(linkedin_url)
            if cached:
                return {
                    'success': True,
                    'data': cached,
                    'from_cache': True
                }
        
        # Check if API key is configured
        if not self.api_key:
            return {
                'success': False,
                'error': 'RapidAPI key not configured',
                'message': 'Please add RAPIDAPI_KEY to Space Secrets'
            }
        
        # Fetch from API
        try:
            querystring = {"linkedin_url": linkedin_url}
            response = requests.get(
                f"{self.base_url}/get-linkedin-profile",
                headers=self.headers,
                params=querystring,
                timeout=30
            )
            
            if response.status_code == 200:
                data = response.json()
                
                # Save to cache
                if use_cache:
                    self._save_to_cache(linkedin_url, data)
                
                return {
                    'success': True,
                    'data': data,
                    'from_cache': False
                }
            elif response.status_code == 429:
                return {
                    'success': False,
                    'error': 'Rate limit exceeded',
                    'message': 'You have reached your API rate limit. Consider upgrading or waiting.'
                }
            else:
                return {
                    'success': False,
                    'error': f'API error: {response.status_code}',
                    'message': response.text
                }
        
        except requests.exceptions.Timeout:
            return {
                'success': False,
                'error': 'Request timeout',
                'message': 'The API request timed out. Please try again.'
            }
        except Exception as e:
            return {
                'success': False,
                'error': 'Fetch error',
                'message': str(e)
            }
    
    def enrich_lead(self, lead_data: Dict) -> Dict:
        """Enrich lead data with LinkedIn profile information
        
        Args:
            lead_data: Dictionary containing lead information with 'linkedin_url' key
        
        Returns:
            Enriched lead data dictionary
        """
        linkedin_url = lead_data.get('linkedin_url', '')
        if not linkedin_url:
            return lead_data
        
        # Fetch LinkedIn profile
        result = self.fetch_profile(linkedin_url)
        
        if result['success']:
            profile = result['data']
            
            # Add LinkedIn data to lead
            enriched = lead_data.copy()
            enriched['linkedin_profile'] = {
                'full_name': profile.get('full_name', ''),
                'headline': profile.get('headline', ''),
                'location': profile.get('location', ''),
                'summary': profile.get('summary', ''),
                'experiences': profile.get('experiences', []),
                'education': profile.get('education', []),
                'skills': profile.get('skills', []),
                'from_cache': result.get('from_cache', False)
            }
            enriched['linkedin_enriched'] = True
            enriched['linkedin_enriched_at'] = time.time()
        else:
            # Add error info
            enriched = lead_data.copy()
            enriched['linkedin_enriched'] = False
            enriched['linkedin_error'] = result.get('error', 'Unknown error')
        
        return enriched
    
    def get_cache_stats(self) -> Dict:
        """Get cache statistics
        
        Returns:
            Dictionary with cache stats
        """
        if not self.cache_dir or not self.cache_dir.exists():
            return {'cached_profiles': 0}
        
        cached_files = list(self.cache_dir.glob('*.json'))
        return {
            'cached_profiles': len(cached_files),
            'cache_directory': str(self.cache_dir)
        }


# Convenience functions for easy import
def fetch_linkedin_profile(linkedin_url: str, api_key: Optional[str] = None, 
                          username: Optional[str] = None, use_cache: bool = True) -> Dict:
    """Fetch LinkedIn profile data (convenience function)
    
    Args:
        linkedin_url: Full LinkedIn profile URL
        api_key: RapidAPI key (optional, uses env var if not provided)
        username: Current user's username for caching
        use_cache: Whether to use cached data
    
    Returns:
        Dictionary with profile data or error information
    """
    fetcher = LinkedInDataFetcher(api_key=api_key, username=username)
    return fetcher.fetch_profile(linkedin_url, use_cache=use_cache)


def enrich_lead(lead_data: Dict, api_key: Optional[str] = None, 
               username: Optional[str] = None) -> Dict:
    """Enrich lead data with LinkedIn information (convenience function)
    
    Args:
        lead_data: Lead dictionary with 'linkedin_url' key
        api_key: RapidAPI key (optional)
        username: Current user's username for caching
    
    Returns:
        Enriched lead data dictionary
    """
    fetcher = LinkedInDataFetcher(api_key=api_key, username=username)
    return fetcher.enrich_lead(lead_data)


def get_cache_stats(username: str) -> Dict:
    """Get LinkedIn cache statistics (convenience function)
    
    Args:
        username: Username to check cache for
    
    Returns:
        Dictionary with cache statistics
    """
    fetcher = LinkedInDataFetcher(username=username)
    return fetcher.get_cache_stats()