Skip to content

Commit 3ff5f66

Browse files
committed
chore: update project infrastructure and dependencies
- Update run.sh with comprehensive Python 3.12 and Poetry setup - Update dependencies in poetry.lock and pyproject.toml - Add LICENSE file modifications This commit improves the project setup process and ensures consistent Python 3.12 environment across different systems.
1 parent df294b0 commit 3ff5f66

14 files changed

+2115
-2058
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AGENTQL_API_KEY=""
2-
OPENAI_API_KEY=""
3-
LEETCODE_USERNAME=""
1+
AGENTQL_API_KEY=""
2+
OPENAI_API_KEY=""
3+
LEETCODE_USERNAME=""
44
LEETCODE_PASSWORD=""

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
parameters.py
2-
.env
1+
parameters.py
2+
.env
3+
leetcode_login.json

LICENSE

Lines changed: 674 additions & 674 deletions
Large diffs are not rendered by default.

poetry.lock

Lines changed: 1245 additions & 1245 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
[tool.poetry]
2-
name = "leetcode-solver"
3-
version = "0.1.0"
4-
description = "A Leetcode bot"
5-
authors = ["Lorenzo Scaturchio"]
6-
license = "GNU General Public License"
7-
readme = "README.md"
8-
9-
[tool.poetry.dependencies]
10-
python = ">=3.12,<=3.13"
11-
selenium = "^4.27.1"
12-
beautifulsoup4 = "^4.12.3"
13-
openai = "^1.57.0"
14-
python-dotenv = "^1.0.1"
15-
playwright = "^1.41.2"
16-
agentql = "^1.6.1"
17-
18-
[build-system]
19-
requires = ["poetry-core"]
20-
build-backend = "poetry.core.masonry.api"
1+
[tool.poetry]
2+
name = "leetcode-solver"
3+
version = "0.1.0"
4+
description = "A Leetcode bot"
5+
authors = ["Lorenzo Scaturchio"]
6+
license = "GNU General Public License"
7+
readme = "README.md"
8+
9+
[tool.poetry.dependencies]
10+
python = ">=3.12,<=3.13"
11+
selenium = "^4.27.1"
12+
beautifulsoup4 = "^4.12.3"
13+
openai = "^1.57.0"
14+
python-dotenv = "^1.0.1"
15+
playwright = "^1.41.2"
16+
agentql = "^1.6.1"
17+
18+
[build-system]
19+
requires = ["poetry-core"]
20+
build-backend = "poetry.core.masonry.api"

src/bot.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
from utils.select_random import select_random
1111
from utils.solve_problem import solve_problem
1212

13+
load_dotenv()
14+
os.environ["AGENTQL_API_KEY"] = os.getenv('AGENTQL_API_KEY')
15+
INITIAL_URL = "https://leetcode.com/accounts/login/"
16+
1317
# Setup logging
1418
logging.basicConfig(
1519
level=logging.DEBUG,
1620
format='%(asctime)s - %(levelname)s - %(message)s',
1721
handlers=[
18-
logging.FileHandler('leetcode_solver.log', encoding='utf-8'),
22+
logging.FileHandler('logs/leetcode_solver.log', encoding='utf-8'),
1923
logging.StreamHandler()
2024
]
2125
)
@@ -29,10 +33,21 @@ def main():
2933
page = agentql.wrap(browser.new_page())
3034
time_start = time.time()
3135

32-
try:
33-
# Login
34-
if not login(page):
36+
state_path = os.path.join(os.getcwd(), "leetcode_login.json")
37+
38+
# Login
39+
if not os.path.exists(state_path):
40+
logging.info("No login state found. Logging in...")
41+
if not login(page, INITIAL_URL, browser):
3542
raise Exception("Failed to login")
43+
logging.info("Successfully logged in to Leetcode")
44+
45+
try:
46+
# Create new context with saved login state
47+
context = browser.new_context(storage_state=state_path)
48+
page = agentql.wrap(context.new_page())
49+
page.goto("https://leetcode.com")
50+
page.wait_for_load_state("networkidle")
3651

3752
# Select random problem
3853
if not select_random(page):
@@ -46,13 +61,17 @@ def main():
4661

4762
time_elapsed = time.time() - time_start
4863
logging.info(f"Total time elapsed: {time_elapsed:.2f} seconds")
49-
50-
finally:
51-
browser.close()
64+
65+
except Exception as e:
66+
logging.error(f"Error during execution: {str(e)}")
67+
if os.path.exists(state_path):
68+
logging.info("Removing invalid login state file")
69+
os.remove(state_path)
70+
raise e
5271

5372
except Exception as e:
5473
logging.error(f"Error encountered: {str(e)}")
55-
raise
74+
raise e
5675

5776
if __name__ == '__main__':
5877
main()

src/run.sh

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
echo "Running the bot..."
2-
3-
# Update package list and install prerequisites
4-
sudo apt update
5-
sudo apt install -y software-properties-common
6-
7-
# Add deadsnakes PPA and install Python 3.12
8-
sudo add-apt-repository ppa:deadsnakes/ppa
9-
sudo apt update
10-
sudo apt install -y python3.12 python3.12-venv python3.12-dev
11-
12-
# Ensure pip is installed for Python 3.12
13-
curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3.12
14-
15-
# Install pipx
16-
python3.12 -m pip install --user pipx
17-
python3.12 -m pipx ensurepath
18-
19-
# Install Poetry using pipx
20-
pipx install poetry
21-
22-
# Install project dependencies using Poetry
23-
poetry install
24-
25-
# Run the bot
1+
echo "Running the bot..."
2+
3+
# Update package list and install prerequisites
4+
sudo apt update
5+
sudo apt install -y software-properties-common
6+
7+
# Add deadsnakes PPA and install Python 3.12
8+
sudo add-apt-repository ppa:deadsnakes/ppa
9+
sudo apt update
10+
sudo apt install -y python3.12 python3.12-venv python3.12-dev
11+
12+
# Ensure pip is installed for Python 3.12
13+
curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3.12
14+
15+
# Install pipx
16+
python3.12 -m pip install --user pipx
17+
python3.12 -m pipx ensurepath
18+
19+
# Install Poetry using pipx
20+
pipx install poetry
21+
22+
# Install project dependencies using Poetry
23+
poetry install
24+
25+
# Run the bot
2626
poetry run python bot.py
15 Bytes
Binary file not shown.
1.17 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)