Update app.py
Browse files
app.py
CHANGED
|
@@ -19,17 +19,21 @@ def get_secret(secret_name):
|
|
| 19 |
|
| 20 |
@sleep_and_retry
|
| 21 |
@limits(calls=5, period=60) # Adjust these values based on API rate limits
|
| 22 |
-
def rate_limited_api_call(url, headers, data):
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
| 26 |
-
def resilient_api_call(url, headers, data):
|
| 27 |
try:
|
| 28 |
-
response = rate_limited_api_call(url, headers, data)
|
| 29 |
response.raise_for_status()
|
| 30 |
return response.json()
|
| 31 |
except RequestException as e:
|
| 32 |
logging.error(f"API call failed: {str(e)}")
|
|
|
|
| 33 |
raise
|
| 34 |
|
| 35 |
def scrape_linkedin_profile(url):
|
|
@@ -43,14 +47,17 @@ def scrape_linkedin_profile(url):
|
|
| 43 |
'Authorization': f'Bearer {api_key}',
|
| 44 |
'Content-Type': 'application/json'
|
| 45 |
}
|
| 46 |
-
|
| 47 |
"url": url
|
| 48 |
}
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
if result.get('message') == 'ok':
|
| 53 |
-
logging.info(f"LinkedIn profile
|
| 54 |
return result.get('data')
|
| 55 |
else:
|
| 56 |
logging.error(f"API error for LinkedIn profile {url}: {result.get('message')}")
|
|
@@ -75,8 +82,12 @@ def scrape_facebook_profile(url):
|
|
| 75 |
}
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
if 'error' not in result:
|
|
|
|
| 80 |
profile_data = {
|
| 81 |
"Name": result.get('name', ''),
|
| 82 |
"Profile Link": url,
|
|
|
|
| 19 |
|
| 20 |
@sleep_and_retry
|
| 21 |
@limits(calls=5, period=60) # Adjust these values based on API rate limits
|
| 22 |
+
def rate_limited_api_call(url, headers, params=None, data=None, method='GET'):
|
| 23 |
+
if method == 'GET':
|
| 24 |
+
return requests.get(url, headers=headers, params=params)
|
| 25 |
+
elif method == 'POST':
|
| 26 |
+
return requests.post(url, headers=headers, json=data)
|
| 27 |
|
| 28 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
| 29 |
+
def resilient_api_call(url, headers, params=None, data=None, method='GET'):
|
| 30 |
try:
|
| 31 |
+
response = rate_limited_api_call(url, headers, params, data, method)
|
| 32 |
response.raise_for_status()
|
| 33 |
return response.json()
|
| 34 |
except RequestException as e:
|
| 35 |
logging.error(f"API call failed: {str(e)}")
|
| 36 |
+
logging.error(f"Response content: {response.text}")
|
| 37 |
raise
|
| 38 |
|
| 39 |
def scrape_linkedin_profile(url):
|
|
|
|
| 47 |
'Authorization': f'Bearer {api_key}',
|
| 48 |
'Content-Type': 'application/json'
|
| 49 |
}
|
| 50 |
+
params = {
|
| 51 |
"url": url
|
| 52 |
}
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
logging.info(f"Attempting to scrape LinkedIn profile: {url}")
|
| 56 |
+
logging.info(f"Using LinkedIn API endpoint: {api_endpoint}")
|
| 57 |
+
result = resilient_api_call(api_endpoint, headers, params=params, method='GET')
|
| 58 |
+
logging.info(f"LinkedIn API response: {json.dumps(result, indent=2)}")
|
| 59 |
if result.get('message') == 'ok':
|
| 60 |
+
logging.info(f"Successfully scraped LinkedIn profile: {url}")
|
| 61 |
return result.get('data')
|
| 62 |
else:
|
| 63 |
logging.error(f"API error for LinkedIn profile {url}: {result.get('message')}")
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
logging.info(f"Attempting to scrape Facebook profile: {url}")
|
| 86 |
+
logging.info(f"Using Facebook API endpoint: {api_endpoint}")
|
| 87 |
+
result = resilient_api_call(api_endpoint, headers, data=data, method='POST')
|
| 88 |
+
logging.info(f"Facebook API response: {json.dumps(result, indent=2)}")
|
| 89 |
if 'error' not in result:
|
| 90 |
+
logging.info(f"Successfully scraped Facebook profile: {url}")
|
| 91 |
profile_data = {
|
| 92 |
"Name": result.get('name', ''),
|
| 93 |
"Profile Link": url,
|