-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (116 loc) · 4.85 KB
/
Copy pathmain.py
File metadata and controls
138 lines (116 loc) · 4.85 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
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
import os
import yaml
from flask import Flask, render_template, redirect, url_for, request, flash
from platforms.stepstone import StepStonePlatform
from platforms.xing import XingPlatform
from loguru import logger
app = Flask(__name__)
app.secret_key = "supersecretkey" # Required for flashing messages
# Load configuration
def load_config():
"""Load configuration from the config.yaml file."""
with open("user_data/config.yaml", "r") as file:
return yaml.safe_load(file)
config = load_config()
logger.add(config["logging"]["log_file"], rotation="1 MB")
# Global variables
applications = []
automation_running = False # Track if automation is running
@app.route("/")
def home():
"""Home page: Show login status, preferences, and automation options."""
platforms = ['xing', 'stepstone']
logged_in = {}
for platform in platforms:
cookies_file = f"cookies/{platform}.pkl"
logged_in[platform] = os.path.exists(cookies_file)
preferences = config.get("job_preferences", {})
return render_template(
"index.html",
applications=applications,
logged_in=logged_in,
preferences=preferences,
platforms=platforms,
automation_running=automation_running
)
@app.route("/preferences", methods=["GET", "POST"])
def preferences():
"""Page to set or update job preferences."""
if request.method == "POST":
# Save preferences to config
config["job_preferences"]["job_title"] = request.form.get("job_title")
config["job_preferences"]["location"] = request.form.get("location")
config["job_preferences"]["radius"] = int(request.form.get("radius"))
config["job_preferences"]["xing_remote_option"] = request.form.getlist("xing_remote_option")
config["job_preferences"]["xing_employment_type"] = request.form.getlist("xing_employment_type")
config["job_preferences"]["xing_career_level"] = request.form.getlist("xing_career_level")
config["job_preferences"]["stepstone_wfh"] = request.form.get("stepstone_wfh", "0")
with open("user_data/config.yaml", "w") as file:
yaml.safe_dump(config, file)
flash("Preferences saved successfully!", "success")
return redirect(url_for("home"))
# Render preferences form
preferences = config.get("job_preferences", {})
return render_template("preferences.html", preferences=preferences)
@app.route("/login_platform")
def login_platform():
"""Page to choose a platform for login."""
platforms = ['xing', 'stepstone']
return render_template("login_platform.html", platforms=platforms)
@app.route("/login/<platform>")
def login(platform):
"""Login route for the specified platform."""
if platform == 'xing':
platform_instance = XingPlatform(config)
elif platform == 'stepstone':
platform_instance = StepStonePlatform(config)
else:
return "Platform not supported.", 400
try:
# Start the browser before performing any actions
platform_instance.start_browser(headless=False)
success = platform_instance.login()
if success:
flash(f"Logged in successfully to {platform.capitalize()}!", "success")
return redirect(url_for("home"))
else:
flash(f"Login failed for {platform.capitalize()}. Please try again.", "error")
return redirect(url_for("login_platform"))
except Exception as e:
logger.error(f"Error during login: {e}")
flash("An error occurred during login. Please try again.", "error")
return redirect(url_for("login_platform"))
finally:
platform_instance.quit_browser() # Ensure the browser is closed
@app.route("/start_automation")
def start_automation():
"""Start the automation process."""
global automation_running
# Check if the user is logged in to any platform
platforms = ['xing', 'stepstone']
logged_in = any(os.path.exists(f"cookies/{platform}.pkl") for platform in platforms)
if not logged_in:
flash("Please log in to a platform to start automation.", "warning")
return redirect(url_for("home"))
if not automation_running:
automation_running = True
flash("Automation started!", "success")
else:
flash("Automation is already running!", "warning")
return redirect(url_for("home"))
@app.route("/stop_automation")
def stop_automation():
"""Stop the automation process."""
global automation_running
if automation_running:
automation_running = False
flash("Automation stopped!", "success")
else:
flash("No automation process is running!", "warning")
return redirect(url_for("home"))
if __name__ == "__main__":
# Ensure required directories exist
os.makedirs("cookies", exist_ok=True)
os.makedirs("user_data", exist_ok=True)
# Run the app
app.run(debug=True, host="0.0.0.0", port=5001)