forked from rejnronuz/muzovkant-cogdump
Picture Of The Day cog
This commit is contained in:
9
cogs/potd/config.json
Normal file
9
cogs/potd/config.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"description": "Picture Of The Day",
|
||||||
|
"usages": [
|
||||||
|
"potd",
|
||||||
|
"potd <YYYY-MM-DD>"
|
||||||
|
],
|
||||||
|
"hidden": false,
|
||||||
|
"user_agent": "muzovkantBot/1.0 (https://git.rejnronuz.1337.cx/rejnronuz/muzovkantv2)"
|
||||||
|
}
|
||||||
89
cogs/potd/potd.py
Normal file
89
cogs/potd/potd.py
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import logging
|
||||||
|
import aiohttp
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from datetime import date
|
||||||
|
from discord.ext import commands
|
||||||
|
import discord
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_config_path = os.path.join(os.path.dirname(__file__), "config.json")
|
||||||
|
with open(_config_path, encoding="utf-8") as f:
|
||||||
|
_config = json.load(f)
|
||||||
|
|
||||||
|
HEADERS = {
|
||||||
|
"User-Agent": _config.get("user_agent", "muzovkantBot/1.0"),
|
||||||
|
"Accept": "application/json",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_potd(target_date: str):
|
||||||
|
year, month, day = target_date.split("-")
|
||||||
|
url = f"https://en.wikipedia.org/api/rest_v1/feed/featured/{year}/{month}/{day}"
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession(headers=HEADERS) as session:
|
||||||
|
async with session.get(url) as r:
|
||||||
|
if r.status == 404:
|
||||||
|
logger.warning(f"No featured content for {target_date}")
|
||||||
|
return None
|
||||||
|
if r.status != 200:
|
||||||
|
logger.error(f"Wikimedia REST API returned {r.status}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
data = await r.json()
|
||||||
|
|
||||||
|
if "image" not in data:
|
||||||
|
logger.warning(f"No image in featured content for {target_date}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
image = data["image"]
|
||||||
|
return {
|
||||||
|
"title": image.get("title", ""),
|
||||||
|
"description": image.get("description", {}).get("text", ""),
|
||||||
|
"url": image["image"]["source"],
|
||||||
|
"date": target_date,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class PotdCog(commands.Cog):
|
||||||
|
def __init__(self, bot: commands.Bot):
|
||||||
|
self.bot = bot
|
||||||
|
logger.info("POTD cog loaded successfully")
|
||||||
|
|
||||||
|
@commands.command(name="potd")
|
||||||
|
async def potd(self, ctx: commands.Context, target_date: str = None):
|
||||||
|
if target_date is None:
|
||||||
|
target_date = date.today().isoformat()
|
||||||
|
|
||||||
|
try:
|
||||||
|
date.fromisoformat(target_date)
|
||||||
|
except ValueError:
|
||||||
|
await ctx.send("Invalid date format. Preferred format is YYYY-MM-DD")
|
||||||
|
return
|
||||||
|
|
||||||
|
async with ctx.typing():
|
||||||
|
try:
|
||||||
|
potd = await fetch_potd(target_date)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error occurred while fetching POTD: {e}")
|
||||||
|
await ctx.send("Failed to retrieve data from Wikipedia")
|
||||||
|
return
|
||||||
|
|
||||||
|
if potd is None:
|
||||||
|
await ctx.send(f"Picture of the day for `{target_date}` not found")
|
||||||
|
return
|
||||||
|
|
||||||
|
embed = discord.Embed(
|
||||||
|
title=f"Picture of the Day ({potd['date']})",
|
||||||
|
description=potd["description"] or potd["title"],
|
||||||
|
color=0x3498db,
|
||||||
|
)
|
||||||
|
embed.set_image(url=potd["url"])
|
||||||
|
embed.set_footer(text="Source: Wikipedia")
|
||||||
|
|
||||||
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
|
|
||||||
|
async def setup(bot: commands.Bot):
|
||||||
|
await bot.add_cog(PotdCog(bot))
|
||||||
Reference in New Issue
Block a user