I am trying to make a Python program to track users status (online-offline-ingame).
The program uses the Request module for Python to request user’s status and saves it into an Excel file every 2 minutes, where I can review the data.
However, I also want to add the function to see which game a user is in, but API output does not include the lastLocation object.
Currently, example graph generated looks like this:
Graph spacing is a little off because of size constraint.
Key 0-Offline, 1-Online, 2-Ingame
API output from the program is:
{"userPresences":[{"userPresenceType":2,"lastLocation":"","placeId":null,"rootPlaceId":null,"gameId":null,"universeId":null,"userId":##########}]}
API output from documentation with same user ID is:
{
"userPresences": [
{
"userPresenceType": 2,
"lastLocation": "VR Hands v3.1 🎙️",
"placeId": 4832438542,
"rootPlaceId": 4832438542,
"gameId": "325a0256-2a3d-44ed-a139-22f484adf871",
"universeId": 1638323641,
"userId": ##########
}
]
}
Program code is: (in Python 3.9)
import time
import requests
import json
from datetime import datetime
from openpyxl import Workbook, load_workbook
import os
def check_status(user_id):
""" Checks status of player using
Roblox API endpoint Presence V1"""
payload = {'userIds': [user_id]}
presence = requests.post("https://presence.roblox.com/v1/presence/users", json=payload)
presence_data = json.loads(presence.text)
print(presence.text)
if presence is None:
return "ServerError"
if presence_data["userPresences"][0]["userPresenceType"] == 1:
return "Online"
elif presence_data["userPresences"][0]["userPresenceType"] in (2, 3):
if presence_data["userPresences"][0]["userPresenceType"] == 2:
return "InGame"
else:
return "InStudio"
elif presence_data["userPresences"][0]["userPresenceType"] == 0:
return "Offline"
excel_file = "status_log.xlsx"
# Check if file exists, if not create it
print("Enter User ID to track:")
c_user_id = input()
if not os.path.exists(excel_file):
wb = Workbook()
ws = wb.active
ws.append(["Timestamp", "Status", "Status Code"]) # Add header
wb.save(excel_file)
while True:
# Get current timestamp and status
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
status = check_status(c_user_id)
status_code = 5
# Get current status code from previous
if status == "Offline":
status_code = 0
elif status == "Online":
status_code = 1
elif "InGame" in status:
status_code = 2
elif status == "InStudio":
status_code = 3
if status is None:
status_code = 5
# Put new row into file
wb = load_workbook(excel_file)
ws = wb.active
ws.append([timestamp, status, status_code])
wb.save(excel_file)
print("["+timestamp+"]"+" Status recorded as "+status+" Code "+str(status_code))
time.sleep(120)
I am wondering if I should use a Roblox API wrapper such as Ro.py to avoid problems, but I think it is not necessary for a simple project such as this.
Thank you for your advice and support.