-
Notifications
You must be signed in to change notification settings - Fork 0
/
mojang_uuid_to_username_history.py
65 lines (54 loc) · 2.23 KB
/
mojang_uuid_to_username_history.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# ------------------------------------------------------------------------------------
# Mojang API Python scripts made by CZghost/Polda18
# Script name: Mojang Status
#
# Usage: Simply run python3 mojang_uuid_to_username_history.py <uuid>
# ------------------------------------------------------------------------------------
# Import necessary libraries
import json
from requests import get
from termcolor import colored as color
import sys
import uuid
import time
# Create payload
def main():
# Check given arguments (only 1 accepted, ignore rest)
if(len(sys.argv) < 2):
print("No argument given.")
exit(1)
# Validate UUID format and convert it to the correct display format
useruuid = sys.argv[1]
try:
useruuid = uuid.UUID(useruuid)
except:
print(color('Invalid entry. Try again.', 'red')) # Error, invalid entry
exit(2)
# Convert UUID to variant without dashes for data fetch
requestuuid = str(useruuid).replace('-', '')
request_url = f"https://api.mojang.com/user/profiles/{requestuuid}/names"
# Fetch data and check response code
response = get(request_url)
if(response.status_code != 200):
print(color('User profile not found', 'red')) # Error, profile not found
exit(3)
# Deserialize response JSON
response = response.json()
# Display data
print(f"UUID: {useruuid}", end='\n\n')
print('Username'.ljust(30), end='Change Timestamp\n')
#print('Change Timestamp')
print('-------------------------------------------------------------------')
for row in response:
print(row['name'].ljust(30), end='')
if 'changedToAt' in row.keys():
ts = row['changedToAt']//1000 # Convert milliseconds into seconds
ts = time.strftime("%dth %B %Y %H:%M:%S", time.gmtime(ts)) # Convert UNIX timestamp to readable date&time format in UTC timezone
print(f"{ts} UTC")
# Credit: Iselink, Sasi
else:
print('n/a') # No timestamp found
# Run only if fetched through main payload
if __name__ == '__main__':
main()