DinoPLayZ commited on
Commit
e9c421c
·
verified ·
1 Parent(s): 39719aa

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +131 -0
README.md ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BIP Cloud Event Notifier
2
+
3
+ A standalone, single-file lightweight Python application that continuously monitors the BIP Event Portal for new events and instantly sends an email notification along with direct links to apply. The notifier runs as a background process alongside a FastAPI microservice that provides health endpoints.
4
+
5
+ ## Features
6
+ - **Zero Database Required**: Uses memory/local file state to track the latest event IDs instead of a demanding database.
7
+ - **Dynamic Polling Schedule**: Supports dynamic polling speeds via `NORMAL_INTERVAL` and `FAST_INTERVAL`. It automatically speeds up checking when new events are detected (for `FAST_DURATION` seconds) and completely pauses polling during inactive hours (5 PM to 8 AM IST) to conserve resources.
8
+ - **Email Notifications**: Secure email dispatch via the modern Brevo API (recommended for cloud) or standard local SMTP fallback. Separate email recipients for new events (`EVENT_EMAIL_RECIPIENT`) and system warnings (`WARNING_EMAIL_RECIPIENT`) can be configured.
9
+ - **Background Polling with Exponential Backoff**: Efficiently pings the server (default every 60 seconds). Network errors trigger an exponentially increasing sleep timer to gracefully handle short connection drops.
10
+ - **FastAPI Health Server**: Hosts lightweight JSON endpoints (`/health`, `/latest`, `/assistant`) to quickly check monitoring status, cookie expiration, and the latest event info from an external dashboard.
11
+ - **Silent Background Operation**: Maintains a clean terminal, only printing event IDs upon checking and logging errors strictly as needed.
12
+
13
+ ---
14
+
15
+ ## 🛠 Prerequisites
16
+
17
+ 1. **Python 3.x**
18
+ 2. **Pip Requirements**
19
+ ```bash
20
+ pip install -r requirements.txt
21
+ ```
22
+ 3. **Brevo API Key (Recommended)**: Create a free account at [Brevo](https://www.brevo.com/) to easily send reliable API-based emails from cloud environments like Hugging Face.
23
+ 4. **App Password (Fallback)**: A Gmail App Password if preferring local SMTP.
24
+
25
+ ---
26
+
27
+ ## ⚙️ Initial Setup
28
+
29
+ 1. Open this directory in your terminal.
30
+ 2. Create a file named `.env` in the exact same directory as `main.py`.
31
+ 3. Paste the following configuration, replacing the values with your actual portal tokens and email settings:
32
+
33
+ ```ini
34
+ # BIP Auth
35
+ XSRF_TOKEN="your_xsrf_token_here"
36
+ BIP_SESSION="your_bip_session_here"
37
+
38
+ # Email Delivery
39
+ EMAIL_ADDRESS="your_sender_address@gmail.com"
40
+ EMAIL_PASSWORD="your_app_password"
41
+ EVENT_EMAIL_RECIPIENT="recipient_for_new_events@gmail.com"
42
+ WARNING_EMAIL_RECIPIENT="recipient_for_errors@gmail.com"
43
+ BREVO_API_KEY="your_brevo_api_key"
44
+
45
+ # Polling Intervals (in seconds)
46
+ NORMAL_INTERVAL=120
47
+ FAST_INTERVAL=30
48
+ FAST_DURATION=120
49
+ ```
50
+
51
+ ---
52
+
53
+ ## 💻 CLI Commands & Workflow
54
+
55
+ This script operates seamlessly as a Command Line Interface (CLI) application. Open your terminal in the script's directory.
56
+
57
+ ### 1. Test your Email Integration
58
+ Before trusting the loop, manually trigger a test email to ensure Brevo/SMTP is working.
59
+ ```bash
60
+ python main.py --test-alert
61
+ ```
62
+
63
+ ### 2. Test Real Event Delivery
64
+ Fetch the actual latest event from BIP and trigger the full email template rendering and delivery.
65
+ ```bash
66
+ python main.py --test-real-event
67
+ ```
68
+
69
+ ### 3. View the Latest Event
70
+ Quickly pull and print the single most recent event on the portal in the terminal.
71
+ ```bash
72
+ python main.py --latest
73
+ ```
74
+
75
+ ### 4. List All Recent Events
76
+ Fetch the first page of recent events (up to 10) to get an overview of what is currently on the server.
77
+ ```bash
78
+ python main.py --list-all
79
+ ```
80
+
81
+ ### 5. Start the Monitoring Server
82
+ This will launch the primary application: the FastAPI web server on port 7860 and the background polling thread.
83
+ ```bash
84
+ python main.py --run
85
+ ```
86
+ *(Note: Running `python main.py` by itself with no arguments will default to `--run`.)*
87
+
88
+ ---
89
+
90
+ ## 🔄 The "Session / Scraper Error" Workflow
91
+
92
+ The BIP portal cookies (`XSRF_TOKEN` and `BIP_SESSION`) expire periodically. When this happens:
93
+
94
+ 1. The script catches the `401 Unauthorized` or invalid cookie response.
95
+ 2. It sends a critical alert to your Warning Email: **⚠️ Scraper Error!**
96
+ 3. **The background thread completely PAUSES.** It safely halts to prevent spamming the network or creating an endless retry loop.
97
+ 4. The FastAPI `/health` endpoint updates to reflect `session_expired: true`.
98
+ 5. You must update your `XSRF_TOKEN` and `BIP_SESSION` environment variables (either in your `.env` file or in your Hugging Face Space secrets) with new, valid cookies and successfully **restart** the script/Space.
99
+
100
+ ---
101
+
102
+ ## ☁️ Deployment (100% Free via Hugging Face Spaces)
103
+
104
+ The absolute best way to run this script 24/7 for free is using **Hugging Face Spaces**. We have included a completely pre-configured `Dockerfile` alongside the `requirements.txt`.
105
+
106
+ ### How to deploy to Hugging Face:
107
+
108
+ 1. Go to [huggingface.co/spaces](https://huggingface.co/spaces) and click **Create new Space**.
109
+ 2. **Setup your space:**
110
+ * Provide a space name.
111
+ * License: Choose `MIT` (or whatever you prefer).
112
+ * **Select the Space SDK:** Choose **`Docker`** (Blank).
113
+ * **Space Hardware:** Choose the free `CPU basic` tier.
114
+ * Click Create Space.
115
+ 3. Click "Files" and upload the contents of your local folder (`main.py`, `Dockerfile`, `requirements.txt`, etc.) directly into the Space.
116
+ 4. **Setup your Secrets:**
117
+ Go to the Space's **Settings** tab -> **Variables and secrets** -> **New secret**.
118
+ You must add the exact same variables from your local `.env`:
119
+ * `XSRF_TOKEN`
120
+ * `BIP_SESSION`
121
+ * `EMAIL_ADDRESS`
122
+ * `EMAIL_PASSWORD`
123
+ * `EVENT_EMAIL_RECIPIENT`
124
+ * `WARNING_EMAIL_RECIPIENT`
125
+ * `BREVO_API_KEY`
126
+ * `NORMAL_INTERVAL`
127
+ * `FAST_INTERVAL`
128
+ * `FAST_DURATION`
129
+ 5. **Start your App!** Go back to the App tab. The container will automatically build the image and your script will launch. The active web UI defaults to the base `/` endpoint. Note that updates to interval variables happen immediately on the next loop tick, but you must manually restart the Space to supply a new `XSRF_TOKEN`.
130
+
131
+ *(Note: Hugging Face Spaces will go to sleep after ~48 hours of inactivity. Using an automated cron job from a separate small server to hit your `/health` endpoint once a day can occasionally keep the container awake.)*