flake/scripts/py/spectacle-screenshot.py

117 lines
3.5 KiB
Python
Raw Normal View History

2024-11-22 09:50:05 -05:00
#! /usr/bin/env nix-shell
#! nix-shell /etc/nixos/scripts/nix/python.nix -i python
2024-11-22 09:50:05 -05:00
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
from shutil import which
2024-11-29 06:35:15 -05:00
from common.common import notify # type: ignore
2024-11-22 09:50:05 -05:00
def spectacle_screenshot(
url: str | None = None, record: bool = False, file_path: Path | None = None
) -> None:
try:
if not which("spectacle"):
raise FileNotFoundError("spectacle is not installed.")
if file_path and Path(file_path).exists():
raise FileExistsError(
'File already exists. Please provide a different file path, or use the zipline function to upload the file.\nExample: zipline "{file_path}"'
)
if not file_path:
use_temp_file = True
suffix = ".webm" if record else ".png"
2024-11-22 09:50:05 -05:00
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
2024-11-29 06:35:15 -05:00
file_path = Path(temp_file.name)
2024-11-22 09:50:05 -05:00
temp_file.close()
else:
use_temp_file = False
command = [
"spectacle",
"--nonotify",
"--background",
"--pointer",
"--copy-image",
"--output",
file_path,
]
if record:
command.append("--record=region")
else:
command.append("--region")
try:
2024-11-29 06:35:15 -05:00
subprocess.run(command, check=True) # type: ignore
2024-11-22 09:50:05 -05:00
except subprocess.CalledProcessError as e:
if Path(file_path).exists() and use_temp_file:
os.remove(file_path)
raise e
if not Path(file_path).stat().st_size:
os.remove(file_path)
raise FileNotFoundError("The file was not created properly.")
try:
opts = [
2024-12-02 20:50:24 -05:00
"/etc/nixos/scripts/py/zipline.py",
2024-11-22 09:50:05 -05:00
file_path,
"--application-name",
"Spectacle",
"--desktop-entry",
"org.kde.spectacle",
]
if url:
opts.extend(["--url", url])
2024-11-29 06:35:15 -05:00
subprocess.run(opts) # type: ignore
2024-11-22 09:50:05 -05:00
finally:
if Path(file_path).exists() and use_temp_file:
os.remove(file_path)
except BaseException as e:
2024-11-22 09:50:05 -05:00
notify(
application_name="Spectacle",
title="An error occurred",
message=str(e),
2024-11-29 06:35:15 -05:00
urgency="normal",
2024-11-22 09:50:05 -05:00
category="transfer.error",
desktop_entry="org.kde.spectacle",
)
raise e
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="spectacle-screenshot.py",
2024-11-22 09:50:05 -05:00
description="Take a screenshot or recording with Spectacle and automatically upload it to a Zipline instance.",
epilog="Example usage: spectacle-screenshot.py",
2024-11-22 09:50:05 -05:00
)
parser.add_argument(
"--url",
help="The URL of the Zipline instance to upload the screenshot or recording to. Defaults to 'https://csw.im'.",
default="https://csw.im",
)
parser.add_argument(
"--record",
help="If this is set, Spectacle will record the region instead of taking a screenshot.",
action="store_true",
)
parser.add_argument(
"--file-path",
help="The path to save the screenshot or recording to. If not provided, the screenshot or recording will be saved to a temporary file.",
default=None,
)
args = parser.parse_args()
spectacle_screenshot(
url=args.url,
record=args.record,
file_path=args.file_path,
)