-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.py
115 lines (90 loc) · 2.95 KB
/
status.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
import os
import time
import discord
import requests
from discord import Embed
from discord.ext import tasks
from lmdbm import Lmdb
client = discord.Client(intents=discord.Intents.all())
class JsonLmdb(Lmdb):
def _pre_key(self, value):
return value.encode("utf-8")
def _post_key(self, value):
return value.decode("utf-8")
def _pre_value(self, value):
return json.dumps(value).encode("utf-8")
def _post_value(self, value):
return json.loads(value.decode("utf-8"))
def get_time(sec):
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(sec))
def get_server_status():
res = requests.get("http://launcher.startrekonline.com/launcher_server_status")
if res.status_code != 200:
return None
if res.json()["result"] != "success":
return None
return res.json()
def get_news():
params = {
"field[]": [
"images.img_microsite_thumbnail",
"platforms",
"updated",
],
"limit": "10",
}
res = requests.get(
"https://api.arcgames.com/v1.0/games/sto/news",
params=params,
)
if res.status_code != 200:
return None
return res.json()
def get_news_footer(item):
return "platforms: " + ", ".join(item["platforms"])
async def send_news(channel, item):
url = f"https://www.playstartrekonline.com/en/news/article/{item['id']}"
embed = Embed(
title=item["title"],
url=url,
description=item["summary"],
)
embed.set_footer(text=get_news_footer(item))
embed.set_thumbnail(url=item["images"]["img_microsite_thumbnail"]["url"])
msg = await channel.send(
embed=embed,
)
if msg.channel.type == discord.ChannelType.news:
await msg.publish()
@tasks.loop(seconds=60)
async def fetch_news():
with JsonLmdb.open("sto-server-status.db", "c") as db:
res = get_news()
if res is None:
print("Failed to get Star Trek Online news")
return
if res.get("news") is None:
print(res)
return
for item in reversed(res["news"]):
key = f"news.{item['id']}"
if not db.get(key):
for guild in client.guilds:
for channel in guild.channels:
if channel.name in os.environ.get(
"STO_SERVER_STATUS_CHANNELS"
).split(","):
try:
await send_news(channel, item)
except Exception as e:
print(
f"client={client} guild={guild} channel={channel} exception={e}"
)
db[key] = True
@client.event
async def on_ready():
if not fetch_news.is_running():
fetch_news.start()
if __name__ == "__main__":
client.run(os.environ.get("STO_SERVER_STATUS_TOKEN"))