Last active
February 14, 2024 02:14
-
-
Save Specnr/6a030fe4cf75f8c38596e67b2c7a75b3 to your computer and use it in GitHub Desktop.
Generates text files for MCSR Ranked stats that update every 30s and can be used in OBS as text sources
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
| import os | |
| import requests | |
| import time | |
| from datetime import datetime | |
| mc_name = "specnr" | |
| RANKED_API = "https://mcsrranked.com/api/users/" | |
| MATCHES_API_EXTENSION = "/matches" | |
| def save_to_file(name, to_save): | |
| if not os.path.exists("data"): | |
| os.makedirs("data") | |
| with open(f"data/{name}.txt", "w") as out_file: | |
| out_file.write(str(to_save)) | |
| def write_session_wins_losses(time_diff_for_session=3): | |
| time_diff_for_session_s = time_diff_for_session * 3600 | |
| api = f"{RANKED_API}{mc_name}{MATCHES_API_EXTENSION}" | |
| data = requests.get(api).json()["data"] | |
| if len(data) == 0: | |
| return | |
| prev_match = None | |
| i, w, l, d, p = 0, 0, 0, 0, 0 | |
| while True: | |
| if prev_match is None: | |
| prev_match, curr_match = data[i], data[i] | |
| if datetime.now().timestamp() - prev_match["date"] > time_diff_for_session_s: | |
| break | |
| else: | |
| curr_match = data[i] | |
| if prev_match["date"] - curr_match["date"] > time_diff_for_session_s: | |
| break | |
| prev_match = curr_match | |
| if curr_match["type"] == 2: | |
| if curr_match["result"]["mc_name"] == None: | |
| d += 1 | |
| elif curr_match["result"]["mc_name"] == mc_name: | |
| w += 1 | |
| else: | |
| l += 1 | |
| if i == len(data) - 1: | |
| p += 1 | |
| api = f"{RANKED_API}{mc_name}{MATCHES_API_EXTENSION}?page={p}" | |
| data = requests.get(api).json()["data"] | |
| if len(data) == 0: | |
| break | |
| i = 0 | |
| else: | |
| i += 1 | |
| save_to_file("session-record", f"{w}W - {l}L - {d}D") | |
| save_to_file("session-record-no-draw", f"{w}W - {l}L") | |
| if (w + l > 0): | |
| win_perc = w / (w + l) | |
| win_perc *= 100 | |
| save_to_file("session-winrate-no-draw", f"{round(win_perc, 2)}%") | |
| if (w + l + d > 0): | |
| win_perc = w / (w + l + d) | |
| win_perc *= 100 | |
| save_to_file("session-winrate", f"{round(win_perc, 2)}%") | |
| while True: | |
| try: | |
| api = f"{RANKED_API}{mc_name}" | |
| data = requests.get(api) | |
| if data.status_code == 200: | |
| data = data.json()["data"] | |
| else: | |
| input( | |
| "Failed to get data, make sure that you have played at least a game of ranked.") | |
| except: | |
| input("Failed to get data, make sure that you have played at least a game of ranked.") | |
| #while True: | |
| start_time = datetime.now() | |
| print(f"[{start_time.strftime('%H:%M:%S')}] Starting update") | |
| save_to_file("elo", f"#{data['eloRank']} - {data['eloRate']}") | |
| save_to_file("rank", f"Rank: #{data['eloRank']}") | |
| save_to_file("overall-record",f"{data['statistics']['season']['wins']['ranked']}W - {data['statistics']['season']['loses']['ranked']}L") | |
| save_to_file("winstreak", data['statistics']['season']['currentWinStreak']['ranked']) | |
| w = int(data['statistics']['season']['wins']['ranked']) | |
| l = int(data['statistics']['season']['loses']['ranked']) | |
| if w + l > 0: | |
| win_perc = w / (w + l) | |
| win_perc *= 100 | |
| save_to_file("overall-winrate", f"{round(win_perc, 2)}%") | |
| write_session_wins_losses() | |
| end_time = datetime.now() | |
| time_diff = end_time - start_time | |
| print(f"[{end_time.strftime('%H:%M:%S')}] Update completed and took {round(time_diff.total_seconds())}s") | |
| time.sleep(30) # Update once every 30s |
Author
Author
Updated to work with new Ranked API (thanks Evan)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install Python and run
pip install requestsin cmd as a prerequisiteChange uuid on line 7 to be your own (find it here https://minecraftuuid.com/)