Скрипт для загрузки на cdn переписан на bash

This commit is contained in:
retrilzzy committed 2025-03-25 22:36:17 +05:00
1 parent 3bfc75a4eb
commit 249fa03441
2 files changed
+46 -91

No files matched your search

@@ -1,84 +0,0 @@
"""
A simple Chibisafe file uploader that uses the Chibisafe API and API key to upload files.
It is designed for Linux distros running Wayland (I use Arch Linux + Hyprland).
If it doesn't work on your system, check the commands in the functions:
`send_notification()` and `copy_to_clipboard()`
Usage:
chibisafe_uploader.py [-h] file_path
Positional arguments:
file_path Full path to the file to upload
Options:
-h, --help Show help message
"""
import os
import mimetypes
import requests
import dotenv
import logging
import argparse
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
dotenv.load_dotenv()
def send_notification(message: str) -> None:
os.system(f'notify-send "ChibisafeUploader" "{message}" -t 3000')
def copy_to_clipboard(url: str) -> None:
os.system(f'echo "{url}" | wl-copy')
def upload_file(file_path: str) -> None:
"""
Upload a file to Chibisafe.
"""
if not os.path.exists(file_path):
logger.error(f"Error. File {file_path} not found.")
send_notification(f"❌ File {file_path} not found.")
return
file_name = os.path.basename(file_path)
mime_type, _ = mimetypes.guess_type(file_path)
try:
response = requests.post(
url=f"{os.getenv("CHIBISAFE_URL")}/api/upload",
headers={
"x-api-key": os.getenv("CHIBISAFE_API_KEY"),
"user-agent": "Mozilla/5.0 Chrome/131.0.0.0 Safari/537.36",
},
files={
"file": (file_name, open(file_path, "rb"), mime_type or "application/octet-stream"),
},
)
response.raise_for_status()
logger.info(f"Successful. API response: {response.json()}")
send_notification("✅ File uploaded successfully")
copy_to_clipboard(response.json()["url"])
except requests.exceptions.RequestException as e:
logger.error(f"Error occurred while uploading file.\n{e}")
send_notification(f"❌ Error occurred while uploading file.\n{e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="A simple Chibisafe file uploader that uses the Chibisafe API to upload files."
)
parser.add_argument("file_path", help="Full path to the file to upload")
args = parser.parse_args()
upload_file(args.file_path)
+46 -7
View File
@@ -2,14 +2,53 @@
set -e
TEMP_DIR="/tmp"
FILENAME="$(date +%F_%T)-grim.png"
FILE_PATH="$TEMP_DIR/$FILENAME"
if [[ -f "$HOME/.config/hypr/scripts/.env" ]]; then
source "$HOME/.config/hypr/scripts/.env"
fi
# Take the screenshot of the region
grim -c -g "$(slurp)" "$FILE_PATH" || exit 1
if [[ -z "$CHIBISAFE_URL" || -z "$CHIBISAFE_API_KEY" ]]; then
echo "❌ CHIBISAFE_URL or CHIBISAFE_API_KEY is not set. Check the .env file."
exit 1
fi
# Upload the screenshot to ChibiSafe with Python script
"$HOME/.config/hypr/scripts/.venv/bin/python3.12" "$HOME/.config/hypr/scripts/chibisafe_uploader.py" "$FILE_PATH"
DIR="/tmp"
FILENAME="$(date +%F_%H-%M-%S)_grim+url.png"
FILE_PATH="$DIR/$FILENAME"
REGION=$(slurp) || exit 1
[ -n "$REGION" ] || exit 1
grim -g "$REGION" "$FILE_PATH" || exit 1
send_notification() {
notify-send "Chibisafe Uploader" "$1" -t 3000
}
copy_to_clipboard() {
echo -n "$1" | wl-copy
}
echo "Uploading $FILENAME..."
response=$(curl -s -X POST "$CHIBISAFE_URL/api/upload" \
-H "x-api-key: $CHIBISAFE_API_KEY" \
-H "User-Agent: Mozilla/5.0 Chrome/131.0.0.0 Safari/537.36" \
-F "file=@$FILE_PATH;type=$(file --mime-type -b "$FILE_PATH")")
url=$(echo "$response" | jq -r '.url // empty')
if [[ -n "$url" ]]; then
echo "✅ File uploaded successfully."
echo "Response: $response"
send_notification "✅ File uploaded successfully"
copy_to_clipboard "$url"
else
echo "❌ Upload failed"
echo "Response: $response"
send_notification "❌ Upload failed"
exit 1
fi
rm "$FILE_PATH"