105 lines
3.4 KiB
D
105 lines
3.4 KiB
D
module commands.apod;
|
|
|
|
import util, api, api_data;
|
|
import commands.util;
|
|
import main: db;
|
|
|
|
struct APOD {
|
|
string url;
|
|
string hdurl;
|
|
string title;
|
|
string explanation;
|
|
}
|
|
|
|
struct APOD_internal {
|
|
string subscribed_room;
|
|
string mxc, title, body, mimetype;
|
|
int untill, size, width, height;
|
|
}
|
|
|
|
auto apod_get() {
|
|
import asdf, api: upload;
|
|
import gamut: Image;
|
|
|
|
MSG msg;
|
|
Image img;
|
|
auto data = mkrqst("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY", null, null, true)
|
|
.body.deserialize!APOD;
|
|
auto image = mkrqst(data.hdurl, null, null, true);
|
|
|
|
img.loadFromMemory(image.body);
|
|
msg.info.w = img.width;
|
|
msg.info.h = img.height;
|
|
msg.info.size = image.body.length;
|
|
msg.info.mimetype = image.getHeader("Content-Type");
|
|
|
|
msg.url = upload(image.body);
|
|
msg.msgtype = "m.image";
|
|
msg.filename = data.hdurl;
|
|
msg.body = data.title;
|
|
msg.formatted_body = "<details><summary><b>"~data.title~"</b></summary><p>"~data.explanation~"</p></details>";
|
|
return msg;
|
|
}
|
|
|
|
// 🇮🇳🇮🇳🇮🇳 यह बकवास का एक बुरा टुकड़ा है, लेकिन मैं इसे ठीक करने के लिए बहुत आलसी हूं ।
|
|
MSG cached_apod() {
|
|
import core.stdc.time: time;
|
|
|
|
auto now = time(null);
|
|
auto q = db.query("select * from apod where subscribed_room = 'CACHE'");
|
|
|
|
if (q.step) {
|
|
APOD_internal data = q.get!APOD_internal;
|
|
if (data.untill > now) {
|
|
MSG msg = MSG(data.title, data.body);
|
|
msg.url = data.mxc;
|
|
msg.info.w = data.width;
|
|
msg.info.h = data.height;
|
|
msg.info.size = data.size;
|
|
msg.info.mimetype = data.mimetype;
|
|
msg.filename = data.title ~ ".jpg";
|
|
msg.msgtype = "m.image";
|
|
return msg;
|
|
}
|
|
}
|
|
|
|
MSG msg = apod_get;
|
|
db.exec("insert into apod
|
|
(subscribed_room, width, height, size, mxc, mimetype, title, body, untill)
|
|
values ('CACHE',?,?,?,?,?,?,?,?)
|
|
on conflict (mxc) do update set
|
|
mxc = excluded.mxc,
|
|
body = excluded.body,
|
|
size = excluded.size,
|
|
title = excluded.title,
|
|
width = excluded.width,
|
|
height = excluded.height,
|
|
mimetype = excluded.mimetype",
|
|
msg.info.w, msg.info.h, msg.info.size, msg.url, msg.info.mimetype,
|
|
msg.body, msg.formatted_body, now + Time.day);
|
|
|
|
return msg;
|
|
}
|
|
|
|
@Command("Astronomy Picture Of the Day")
|
|
auto apod(Arguments arg, EventWithoutRoomID* evt) {
|
|
if (arg.parsed.length == 0) return cached_apod;
|
|
|
|
auto powerlevel = getUsrPL(evt.room, evt.sender);
|
|
bool subscribed = db.query("select * from apod where subscribed_room = ?", evt.room).step;
|
|
|
|
if (powerlevel < 50) return MSG("Unauthorized");
|
|
if (arg.parsed[0] == "subscribe") {
|
|
if (subscribed)
|
|
return MSG("Room is already subscribed to APOD.");
|
|
db.exec("insert into apod (subscribed_room) values (?)", evt.room);
|
|
return MSG("Room has been subcribed to APOD!");
|
|
} else if (arg.parsed[0] == "unsubscribe") {
|
|
if (subscribed) {
|
|
db.exec("delete from apod where subscribed_room = ?", evt.room);
|
|
return MSG("Room has been unsubscribed from APOD.");
|
|
} else return MSG("Room is not subscribed to APOD.");
|
|
}
|
|
|
|
return MSG("Unknown argument");
|
|
} |