Last active
October 13, 2024 10:21
-
-
Save theoparis/da4abda3929c1f8d65b7fd6acd5a16e1 to your computer and use it in GitHub Desktop.
Send emerge output from gentoo portage to a custom discord status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sh import tail | |
import json | |
import requests | |
import os | |
import re | |
import time | |
token = os.environ.get("DISCORD_TOKEN") | |
for text in tail("-f", "/var/log/emerge.log", _iter=True): | |
# Extract package name assuming it's separated by '/' | |
package_name_match = re.search(r"\(([\w-]+/[\w\.-]+)\::", text) | |
if package_name_match: | |
package_name = package_name_match.group() | |
else: | |
package_name = None | |
# Extract current and max jobs (assuming format "(current of max)") | |
jobs_match = re.search(r"\((\d+) of (\d+)\)", text) | |
if jobs_match: | |
current_job = jobs_match.group(1) | |
max_jobs = jobs_match.group(2) | |
else: | |
current_job = None | |
max_jobs = None | |
if package_name_match and jobs_match: | |
# Discord Webhook URL | |
discord_webhook_url = "https://discord.com/api/v8/users/@me/settings" | |
# Set the headers | |
headers = { | |
"Authorization": token, | |
"User-Agent": | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36", | |
"Content-Type": "application/json", | |
"Accept": "*/*", | |
} | |
# Create JSON payload | |
payload = { | |
"status": "online", | |
"custom_status": { | |
"text": f"[{current_job}/{max_jobs}] Emerging {package_name}" | |
}, | |
} | |
# Send the payload to Discord using a PATCH request | |
response = requests.patch(discord_webhook_url, | |
headers=headers, | |
data=json.dumps(payload)) | |
print(f"discord: {response.status_code} - {response.text}") | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment