Skip to content

Instantly share code, notes, and snippets.

@songwutk
Created January 15, 2025 04:14
Show Gist options
  • Save songwutk/0509ad56d1547516c592313fe3cc8ed2 to your computer and use it in GitHub Desktop.
Save songwutk/0509ad56d1547516c592313fe3cc8ed2 to your computer and use it in GitHub Desktop.
Capture Screenshot to LineNotify
from playwright.sync_api import sync_playwright
from PIL import Image
import requests
# System : Ubuntu 20 Desktop
# Python 3.10 with PlayWright
# pip3 install playwright
# playwright install
# crontab commandline python3 ssgafana.py
# Line Notify Token
#token = "3wKyKg8BpNHCUp3BJw6ovuQWJURJFbVxwFHDYtnWQ5K"
# MophRefer
token ="6pE24zLW5eAyvxIUZXLfXvcZ7sNU8AmMdUEdBUhEtBX"
# URL to capture
url = "http://192.168.4.11:8087/d/de99cwqj5lla8c/752e9da?orgId=1&from=now-6h&to=now&timezone=browser"
# Path to save the screenshot
screenshot_path = "dashboard_full.png"
# Use Playwright to capture the screenshot
with sync_playwright() as p:
#no headless and maximize windows
browser = p.chromium.launch(args=['--start-maximized'], headless=True)
#browser = p.chromium.launch(headless=True)
#maximize windows
context = browser.new_context(no_viewport=True)
#context = browser.new_context(viewport={"width": 1920, "height": 1080})
page = context.new_page()
try:
# Open the URL
page.goto(url)
page.wait_for_timeout(2000) # Wait for the page to load fully
# Take a screenshot of the entire page
#page.screenshot(path=screenshot_path, full_page=True)
# Take screen shot at 10,100,500,450
page.screenshot(path=screenshot_path, clip={"x": 10, "y": 150, "width": 500, "height": 380})
# Crop the screenshot to the specified area
#with Image.open(screenshot_path) as img:
# cropped_img = img.crop((10, 100, 500, 450))
# cropped_img.save(cropped_path)
# Send the cropped screenshot to Line Notify
headers = {
"Authorization": f"Bearer {token}"
}
files = {
"message": (None, "ยอด CA ล่าสุด http://192.168.4.11:8087/d/de99cwqj5lla8c/752e9da?orgId=1&from=now-6h&to=now&timezone=browser"),
#"imageFile": ("dashboard.png", open(cropped_path, "rb"), "image/png")
"imageFile": ("dashboard.png", open(screenshot_path, "rb"), "image/png")
}
response = requests.post("https://notify-api.line.me/api/notify", headers=headers, files=files)
if response.status_code == 200:
print("Notification sent successfully!")
else:
print(f"Failed to send notification: {response.status_code}, {response.text}")
finally:
browser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment