"get_device_auth.py"
Code: Select all
import requests
# Replace with the IP address of your HDHomeRun device (set a static ip in your router)
device_ip = "XXX.XXX.X.XX"
def get_device_auth():
# Construct the URL to access discover.json
url = f"http://{device_ip}/discover.json"
try:
# Send the GET request
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
data = response.json() # Parse the JSON response
# Extract the DeviceAuth value from the JSON data
device_auth = data.get("DeviceAuth")
if device_auth:
print(f"DeviceAuth: {device_auth}")
else:
print("DeviceAuth not found in the response.")
else:
print(f"Failed to fetch data: Status code {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
# Call the function to get and display DeviceAuth
if __name__ == "__main__":
get_device_auth()Code: Select all
import requests
import os
import time
# Replace with the IP address of your HDHomeRun device
device_ip = "XXX.XXX.X.XX"
# Log file location (choose your location)
log_file_path = r"C:\XMLGUIDE\download_log.txt"
# Guide folder path (Point Plex here for Plex DVR Guide Automation)
guide_folder_path = r"C:\XMLGUIDE\PlexGuide" # Folder where the Guide.xml will be saved
def get_device_auth():
# Construct the URL to access discover.json
url = f"http://{device_ip}/discover.json"
try:
# Send the GET request
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
data = response.json() # Parse the JSON response
# Extract the DeviceAuth value from the JSON data
device_auth = data.get("DeviceAuth")
if device_auth:
return device_auth
else:
print("DeviceAuth not found in the response.")
else:
print(f"Failed to fetch data: Status code {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
def download_guide(device_auth):
# Construct the URL to access the XMLTV guide data
url = f"https://api.hdhomerun.com/api/xmltv?DeviceAuth={device_auth}"
try:
# Send the GET request to fetch the guide data
response = requests.get(url)
if response.status_code == 200:
# Ensure the guide folder exists
if not os.path.exists(guide_folder_path):
os.makedirs(guide_folder_path)
# Save the guide data as Guide.xml
guide_file_path = os.path.join(guide_folder_path, "Guide.xml")
with open(guide_file_path, "wb") as file:
file.write(response.content)
print(f"Guide downloaded successfully to {guide_file_path}")
# Log the DeviceAuth and current time
log_download(device_auth)
else:
print(f"Failed to fetch guide data: Status code {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
def log_download(device_auth):
# Get the current timestamp
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
# Ensure the log directory exists
log_dir = os.path.dirname(log_file_path)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Append the log entry
with open(log_file_path, "a") as log_file:
log_file.write(f"{current_time} - DeviceAuth: {device_auth}\n")
print(f"Download log updated at {log_file_path}")
def main():
device_auth = get_device_auth()
if device_auth:
download_guide(device_auth)
else:
print("DeviceAuth not found, cannot proceed with the download.")
if __name__ == "__main__":
main()