Skip to content

Instantly share code, notes, and snippets.

@wilfredallyn
Created October 24, 2024 14:30
Show Gist options
  • Save wilfredallyn/e711a6fe0c2a8f41208c6aa979f8831d to your computer and use it in GitHub Desktop.
Save wilfredallyn/e711a6fe0c2a8f41208c6aa979f8831d to your computer and use it in GitHub Desktop.
import requests
import json
import time
# Replace with your server IP and application name
server_ip = '<your-server-ip>'
app_name = 'hashrate_display'
# Define the GET request URL for system info and POST request URL for custom data
get_url = f'http://{server_ip}/api/system/info'
post_url = f'http://{server_ip}/api/custom?name={app_name}'
# Function to fetch system info and post the hashrate
def fetch_and_post_hashrate():
try:
response = requests.get(get_url)
if response.status_code == 200:
data = response.json()
# Extract hashrate from the response
hashrate = data.get('hashRate')
if hashrate is not None:
# Construct the JSON body for the POST request
post_data = {
"text": f"Current Hashrate: {hashrate} H/s",
"rainbow": True, # Add rainbow effect
"duration": 10, # Display for 10 seconds
"textCase": 2, # Show as it is
"center": True # Center the text
}
# Send the POST request with the JSON body
headers = {'Content-Type': 'application/json'}
post_response = requests.post(post_url, data=json.dumps(post_data), headers=headers)
if post_response.status_code == 200:
print(f"Posted hashrate: {hashrate} H/s")
else:
print(f"Failed to post hashrate. Status code: {post_response.status_code}")
else:
print("Hashrate not found in system info response.")
else:
print(f"Failed to fetch system info. Status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
# Main loop
try:
while True:
fetch_and_post_hashrate()
# Wait for 30 seconds before fetching and posting again (adjust the sleep time if needed)
time.sleep(30)
except KeyboardInterrupt:
print("Process interrupted. Exiting...")
5m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment