-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget_events.py
More file actions
61 lines (47 loc) · 2.15 KB
/
Copy pathget_events.py
File metadata and controls
61 lines (47 loc) · 2.15 KB
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
"""
get_events.py
Sample script to get events from Comlink using the async client
"""
import asyncio
from datetime import datetime, timezone
from typing import Union
from swgoh_comlink import SwgohComlinkAsync
def convert_time(timestamp: int | float | str) -> str:
"""
Convert unix timestamp to human-readable string
:param timestamp: integer or string representing a unix timestamp value
:return: str
"""
if not (isinstance(timestamp, (int | float | str))):
return f"Invalid argument {timestamp}, type( {type(timestamp)} ). Expecting type str or int."
if len(str(timestamp)) > 10:
# timestamp is in milliseconds
timestamp = int(timestamp)
timestamp /= 1000
return datetime.fromtimestamp(int(timestamp), tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
async def main():
# Create instance of SwgohComlinkAsync using default localhost settings
async with SwgohComlinkAsync() as comlink:
# Get all available currently available events from game servers
events = await comlink.get_events()
"""
The call above returns a single element dictionary with a key name of 'gameEvent'. The value of this element is a
list of all the available game events at that time.
"""
if "gameEvent" not in events:
print(f"Unexpected response from game server, check your internet connection. {events}")
return
# Loop through the events and print the ID, status relevant time entries for each occurrence
for event in events["gameEvent"]:
print(f'{event["id"]=}, {event["status"]=}')
instances = event["instance"]
for instance in instances:
display_start = convert_time(instance["displayStartTime"])
display_end = convert_time(instance["displayEndTime"])
start_time = convert_time(instance["startTime"])
reward_time = convert_time(instance["rewardTime"])
print(f"\t{display_start=}")
print(f"\t{display_end=}")
print(f"\t{start_time=}")
print(f"\t{reward_time=}")
asyncio.run(main())