Skip to content

Instantly share code, notes, and snippets.

@mralext20
Last active February 12, 2021 05:59
Show Gist options
  • Save mralext20/65a23bf48a2a71a678ec98633de0ef55 to your computer and use it in GitHub Desktop.
Save mralext20/65a23bf48a2a71a678ec98633de0ef55 to your computer and use it in GitHub Desktop.
neosTZ
import json
import pytz
import csv
import requests
from datetime import datetime
from time import sleep
from requests.models import HTTPError
JSON_FILE = "members.json"
OUTPUT_FOLDER = "."
data = json.load(open(JSON_FILE))
groups = data["groups"]
zones = {}
user_cache = {}
def get_user(username: str):
try:
user_cache[username]
print(f"retrive'd {user} from cache")
except KeyError:
print(f"fetching {user}...")
req = requests.get(f"https://www.neosvr-api.com/api/users/{user}")
req.raise_for_status()
user_cache[username] = req.json()
return user_cache[username]
for group in groups:
for zone in data["zones"]:
zones[zone] = []
for user, tztext in group["users"].items():
sleep(1)
tz = pytz.timezone(tztext)
try:
neos_data = get_user(user)
except HTTPError:
continue
display_name = neos_data["username"]
for tag, users in group["tags"].items():
if user in users:
display_name += f"<sprite name={tag}>"
try:
icon = neos_data["profile"]["iconUrl"]
except KeyError:
icon = group["default_icon"]
hours_offset = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(tz).utcoffset().total_seconds() / 60 / 60
zone = tztext.split("/")[0]
zones[zone].append([user, display_name, icon, hours_offset])
with open(f"{OUTPUT_FOLDER}/{group['name']}.csv", "w", newline="") as f:
writer = csv.writer(f, lineterminator=",\n")
for zone, members in zones.items():
if len(members) == 0:
continue
f.write(f'TZ-{zone},{data["zones"][zone]},,,\n')
writer.writerows(members)
{
"zones": {
"America": "https://neos.alext.duckdns.org/icons/us.png",
"Europe": "https://neos.alext.duckdns.org/icons/eu.png",
"Australia": "https://neos.alext.duckdns.org/icons/aus.png"
},
"groups": [
{
"name": "vibez",
"tags": {
"zzz": [
"U-Rukio",
"U-princess",
"U-Shorty0w0",
"U-QueenHidi",
"U-Elektrospy"
]
},
"default_icon": "neosdb:///607d206861c11b37b9722d9d5d45adba7b3b9aa23943058b5c5975c617048157.webp",
"users": {
"U-alex-from-alaska": "America/Anchorage",
"U-Toxic-Cookie": "America/Los_Angeles",
"U-Arcticgamez": "America/Los_Angeles",
"U-HeyItsNate": "America/Los_Angeles",
"U-chiii": "America/Chicago",
"U-Shorty0w0": "America/Chicago",
"U-princess": "America/Chicago",
"U-QueenHidi": "America/New_York",
"U-Rukio": "America/New_York",
"U-Zyzyl": "Europe/Amsterdam",
"U-Beaned": "Europe/Amsterdam",
"U-jeana": "Europe/Amsterdam",
"U-Lazhannya": "Europe/Berlin",
"U-Elektrospy": "Europe/Berlin",
"U-TheInDoorAussie": "Australia/Brisbane"
}
},
{
"name": "viarsys",
"tags": {},
"default_icon": "neosdb:///607d206861c11b37b9722d9d5d45adba7b3b9aa23943058b5c5975c617048157.webp",
"users": {
"U-sirkitree": "America/Los_Angeles"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment