feat: redis integration
This commit is contained in:
+11
-5
@@ -1,7 +1,13 @@
|
||||
EDU_LOGIN=login
|
||||
EDU_PASSWORD=password
|
||||
EDU_LOGIN=your_edu_login_here
|
||||
EDU_PASSWORD=your_edu_password_here
|
||||
EDU_URL_LOGIN=https://edu.edu.vn.ua/user/login
|
||||
EDU_URL_VERIFY=https://edu.edu.vn.ua/course/userlist
|
||||
EDU_INTERVAL=10
|
||||
EDU_USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
|
||||
EDU_PHPSESSID=
|
||||
PHPSESSID_INTERVAL=10
|
||||
USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
|
||||
WEBINAR_URL=https://edu.edu.vn.ua/webinar/useractive
|
||||
WEBINAR_CHECK_INTERVAL=60
|
||||
REDIS_HOST=redis
|
||||
REDIS_PORT=6379
|
||||
PLAYWRIGHT_WS=ws://playwright-service:3000/ws
|
||||
WEBINAR_TELEGRAM_TOKEN=your_telegram_bot_token_here
|
||||
WEBINAR_TELEGRAM_CHAT_ID=your_telegram_chat_id_here
|
||||
|
||||
+42
-3
@@ -1,10 +1,49 @@
|
||||
services:
|
||||
redis:
|
||||
image: redis:alpine
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
ports:
|
||||
- "6379:6379"
|
||||
healthcheck:
|
||||
test: [ "CMD", "redis-cli", "ping" ]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
|
||||
playwright-service:
|
||||
image: mcr.microsoft.com/playwright:v1.56.0-noble
|
||||
restart: unless-stopped
|
||||
command: npx -y playwright run-server --port 3000 --path /ws
|
||||
ports:
|
||||
- "3000:3000"
|
||||
|
||||
session-keeper:
|
||||
build: ./phpsessid-bot
|
||||
env_file: .env
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "test $(find /tmp/last_success -mmin -15)"]
|
||||
interval: 1m
|
||||
test: [ "CMD-SHELL", "redis-cli -h redis EXISTS EDU_PHPSESSID | grep -q 1" ]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
retries: 10
|
||||
start_period: 60s
|
||||
|
||||
webinar-checker:
|
||||
build: ./webinar-checker
|
||||
env_file: .env
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
session-keeper:
|
||||
condition: service_healthy
|
||||
playwright-service:
|
||||
condition: service_started
|
||||
|
||||
volumes:
|
||||
redis-data:
|
||||
|
||||
@@ -2,8 +2,11 @@ FROM python:3.11-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install system dependencies
|
||||
RUN apt-get update && apt-get install -y redis-tools && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Install dependencies
|
||||
RUN pip install requests
|
||||
RUN pip install requests redis
|
||||
|
||||
# Copy application code
|
||||
COPY . .
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
import time
|
||||
import requests
|
||||
import logging
|
||||
import redis
|
||||
from datetime import datetime
|
||||
|
||||
# Configure logging
|
||||
@@ -16,9 +17,10 @@ LOGIN = os.getenv('EDU_LOGIN')
|
||||
PASSWORD = os.getenv('EDU_PASSWORD')
|
||||
URL_LOGIN = os.getenv('EDU_URL_LOGIN', 'https://edu.edu.vn.ua/user/login')
|
||||
URL_VERIFY = os.getenv('EDU_URL_VERIFY', 'https://edu.edu.vn.ua/course/userlist')
|
||||
INTERVAL = int(os.getenv('EDU_INTERVAL', 10))
|
||||
USER_AGENT = os.getenv('EDU_USER_AGENT', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36')
|
||||
INITIAL_PHPSESSID = os.getenv('EDU_PHPSESSID')
|
||||
INTERVAL = int(os.getenv('PHPSESSID_INTERVAL', 10))
|
||||
USER_AGENT = os.getenv('USER_AGENT', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36')
|
||||
REDIS_HOST = os.getenv('REDIS_HOST', 'redis')
|
||||
REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
|
||||
|
||||
SUCCESS_FILE = '/tmp/last_success'
|
||||
|
||||
@@ -33,6 +35,15 @@ def touch_success_file():
|
||||
def main():
|
||||
logger.info("Starting Session Keeper Bot")
|
||||
|
||||
# Connect to Redis
|
||||
try:
|
||||
redis_client = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)
|
||||
redis_client.ping()
|
||||
logger.info(f"Connected to Redis at {REDIS_HOST}:{REDIS_PORT}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to connect to Redis: {e}")
|
||||
return
|
||||
|
||||
session = requests.Session()
|
||||
|
||||
# Set headers
|
||||
@@ -54,11 +65,6 @@ def main():
|
||||
}
|
||||
session.headers.update(headers)
|
||||
|
||||
# If an initial PHPSESSID is provided, set it in the session
|
||||
if INITIAL_PHPSESSID:
|
||||
logger.info(f"Setting initial PHPSESSID: {INITIAL_PHPSESSID}")
|
||||
session.cookies.set('PHPSESSID', INITIAL_PHPSESSID, domain='edu.edu.vn.ua')
|
||||
|
||||
while True:
|
||||
try:
|
||||
logger.info("Attempting login...")
|
||||
@@ -88,6 +94,15 @@ def main():
|
||||
if verify_response.status_code == 200:
|
||||
logger.info("Session verification SUCCESS (200 OK).")
|
||||
touch_success_file()
|
||||
|
||||
# Save PHPSESSID to Redis
|
||||
phpsessid = session.cookies.get('PHPSESSID')
|
||||
if phpsessid:
|
||||
try:
|
||||
redis_client.set('EDU_PHPSESSID', phpsessid)
|
||||
logger.info(f"Saved PHPSESSID to Redis: {phpsessid}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save PHPSESSID to Redis: {e}")
|
||||
elif verify_response.status_code == 302:
|
||||
logger.warning("Session verification FAILED (302 Redirect). Session might be invalid.")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user