Files
yankee-gnome-twitch-api/test/success/eval-failure-create-new.py

52 lines
1.8 KiB
Python
Raw Normal View History

2025-03-10 09:39:38 -04:00
import requests
import json
import socket
import netifaces # For getting external IP (install with: pip install netifaces)
def get_external_ip():
"""Gets the external IP address using a reliable service."""
try:
response = requests.get("https://api.ipify.org?format=json", timeout=5) # Use a reliable service
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
ip_data = response.json()
return ip_data["ip"]
except requests.exceptions.RequestException as e:
print(f"Error getting external IP: {e}")
return "0.0.0.0" # Or some other default value
def get_hostname():
"""Gets the local hostname."""
try:
return socket.gethostname()
except Exception as e:
print(f"Error getting hostname: {e}")
return "unknown_host"
def create_success_record(url):
"""Creates a new 'success' record in the database."""
external_ip = get_external_ip()
hostname = get_hostname()
data = {
"url": url,
"hostIP": external_ip,
"hostName": hostname
}
try:
response = requests.post(
"http://localhost:8888/channel/monthly/games/failure/add",
headers={'Content-Type': 'application/json'},
data=json.dumps(data) # Use json.dumps() to serialize the data
)
response.raise_for_status() # Check for HTTP errors
print("Successfully created record:", response.json()) # Print the response from the server
except requests.exceptions.RequestException as e:
print(f"Error creating record: {e}")
except json.JSONDecodeError as e:
print(f"Error decoding JSON response: {e}") # Handle potential JSON decode errors
if __name__ == "__main__":
url = "https://www.embanet.com/talisman.txt" # Replace with your actual URL
create_success_record(url)