Skip to content

Commit 479a994

Browse files
author
Bharatesh Kharvi
committed
feat: Add core screenshot automation bot
This commit introduces the initial version of `screenshot_automation_bot.py`. It provides a CLI tool using Playwright to: - Periodically take full-page screenshots of a specified URL. - Allow configuration of interval, output directory, and headless mode via CLI arguments. - Save files with a timestamped naming format for easy archiving. The tool is ideal for visual monitoring and archiving web page changes over time.
1 parent a131e88 commit 479a994

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
````markdown
2+
# 📸 Screenshot Automation Bot
3+
4+
`screenshot_automation_bot.py` is a Python CLI tool that **automates taking periodic full-page screenshots** of any website using [Playwright](https://playwright.dev/).
5+
6+
Ideal for monitoring visual changes, archiving web content, or debugging dynamic pages over time.
7+
8+
---
9+
10+
## 🚀 How to Run
11+
12+
After installing the required dependencies (see below), run the script as follows:
13+
14+
```bash
15+
python screenshot_automation_bot.py <url> [options]
16+
````
17+
18+
### ⚙️ Command-Line Options
19+
20+
| Option | Description | Default |
21+
| :--- | :--- | :--- |
22+
| `<url>` | (Required) The target website URL to screenshot | `None` |
23+
| `-i`, `--interval` | Interval (in seconds) between each screenshot | `300` (5 minutes) |
24+
| `-o`, `--output` | Output directory to save screenshots | `output/` |
25+
| `--headless` | Run the browser in headless mode (no GUI) | `False` (GUI is shown by default) |
26+
27+
### 📂 Example Usage
28+
29+
1. **Basic example (every 5 minutes):**
30+
31+
```bash
32+
python screenshot_automation_bot.py [https://example.com](https://example.com)
33+
```
34+
35+
2. **Custom interval and output directory:**
36+
37+
```bash
38+
python screenshot_automation_bot.py [https://example.com](https://example.com) -i 600 -o snapshots
39+
```
40+
41+
3. **Run in headless mode:**
42+
43+
```bash
44+
python screenshot_automation_bot.py [https://example.com](https://example.com) --headless
45+
```
46+
47+
## 🖼 Output
48+
49+
Screenshots are saved in the specified output folder using a timestamped filename format:
50+
51+
```
52+
output/
53+
├── screenshot_20251009_141500.png
54+
├── screenshot_20251009_142000.png
55+
...
56+
```
57+
58+
## 🛠 Requirements
59+
60+
* Python 3.7 or later
61+
* Playwright (Python)
62+
63+
### Install Requirements
64+
65+
```bash
66+
pip install -r requirements.txt
67+
playwright install
68+
```
69+
70+
## 🧯 Stop the Script
71+
72+
Use `Ctrl + C` to safely stop the script when running in a terminal.
73+
74+
```
75+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
playwright
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import os
3+
from datetime import datetime
4+
import argparse
5+
from playwright.async_api import async_playwright
6+
7+
async def take_screenshot(browser, url, output_dir, count):
8+
context = await browser.new_context()
9+
page = await context.new_page()
10+
await page.goto(url)
11+
12+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
13+
filename = f"screenshot_{timestamp}.png"
14+
filepath = os.path.join(output_dir, filename)
15+
16+
await page.screenshot(path=filepath, full_page=True)
17+
print(f"[{count}] Saved screenshot: {filepath}")
18+
19+
await context.close()
20+
21+
async def main():
22+
parser = argparse.ArgumentParser(description="Periodic Website Screenshot Tool using Playwright")
23+
parser.add_argument("url", help="Website URL to take screenshots of")
24+
parser.add_argument("-i", "--interval", type=int, default=300, help="Interval between screenshots in seconds (default: 300)")
25+
parser.add_argument("-o", "--output", default="output", help="Directory to save screenshots (default: ./output)")
26+
parser.add_argument("--headless", action="store_true", help="Run browser in headless mode")
27+
28+
args = parser.parse_args()
29+
30+
os.makedirs(args.output, exist_ok=True)
31+
32+
async with async_playwright() as playwright:
33+
browser = await playwright.chromium.launch(headless=args.headless)
34+
35+
count = 1
36+
try:
37+
while True:
38+
await take_screenshot(browser, args.url, args.output, count)
39+
count += 1
40+
await asyncio.sleep(args.interval)
41+
except KeyboardInterrupt:
42+
print("Stopped by user.")
43+
finally:
44+
await browser.close()
45+
46+
if __name__ == "__main__":
47+
asyncio.run(main())
48+

0 commit comments

Comments
 (0)