-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
58 lines (46 loc) · 1.92 KB
/
api.py
File metadata and controls
58 lines (46 loc) · 1.92 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
from flask import Flask, jsonify, request
from flask_cors import CORS
from LS import scrape_profile
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
app = Flask(__name__)
CORS(app)
def get_driver():
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=1920,1080")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
service = Service(ChromeDriverManager().install())
return webdriver.Chrome(service=service, options=options)
@app.route('/api/profile/<profile_id>', methods=['GET'])
def get_profile(profile_id):
try:
profile_url = f"https://www.linkedin.com/in/{profile_id}/"
driver = get_driver()
# Login to LinkedIn
driver.get("https://www.linkedin.com/login")
username_field = driver.find_element_by_id("username")
password_field = driver.find_element_by_id("password")
username_field.send_keys(os.getenv('LINKEDIN_USER'))
password_field.send_keys(os.getenv('LINKEDIN_PASS'))
password_field.submit()
# Wait for login to complete
driver.implicitly_wait(10)
# Scrape the profile
profile_data = scrape_profile(driver, profile_url)
driver.quit()
if profile_data:
return jsonify(profile_data)
else:
return jsonify({"error": "Failed to scrape profile"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, port=5000)