From cb86ea391cd2df3b4f225fb86c36de55ed252b71 Mon Sep 17 00:00:00 2001 From: DasMoorhuhn Date: Fri, 3 Feb 2023 19:48:22 +0100 Subject: [PATCH] xkcd --- .gitignore | 3 ++- src/addons.py | 6 +++++- src/main.py | 10 +++++++--- src/models/response.json | 13 +++++++++++++ src/models/xkcdComic.py | 13 +++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/models/response.json create mode 100644 src/models/xkcdComic.py diff --git a/.gitignore b/.gitignore index c395bde..345bdbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -secret.txt \ No newline at end of file +secret.txt +data diff --git a/src/addons.py b/src/addons.py index 91874dd..e7095e5 100644 --- a/src/addons.py +++ b/src/addons.py @@ -1,4 +1,6 @@ import requests +from models.xkcdComic import Comic +import json class ProgrammerExcuses: def __init__(self) -> None: @@ -16,9 +18,11 @@ class ProgrammerExcuses: class XKCD: def __init__(self) -> None: self.__url = "https://xkcd.com" + self.__api = "info.0.json" def getRandomComic(self): pass def getLastComic(self): - pass \ No newline at end of file + response = requests.api.get(f"{self.__url}/{self.__api}") + return Comic(json.loads(response.text)) \ No newline at end of file diff --git a/src/main.py b/src/main.py index 1c4652a..fda199b 100644 --- a/src/main.py +++ b/src/main.py @@ -2,11 +2,12 @@ import time as t import asyncio import logging import datetime -from unicodedata import category import secretHandler from addons import ProgrammerExcuses from addons import XKCD +from models.xkcdComic import Comic + import discord from discord import Status from discord import app_commands @@ -86,9 +87,12 @@ async def on_guild_join(guild: Guild): async def slash(interaction: Interaction): await interaction.response.send_message(programmerExcuses.getExcuse()) +@tree.command(name='get-latest-comic', description='Get latest comic from XKCD') +async def slash(interaction: Interaction): + comic = xkcd.getLastComic() - - + embed = discord.Embed(title=comic.title, description=comic.alt, color=Color.blue(), url=comic.img) + await interaction.response.send_message(embed=embed) @tree.command(name="info", description="get info about this server") async def slash(interaction: Interaction): diff --git a/src/models/response.json b/src/models/response.json new file mode 100644 index 0000000..5e88fd9 --- /dev/null +++ b/src/models/response.json @@ -0,0 +1,13 @@ +{ + "month": "2", + "num": 2732, + "link": "", + "year": "2023", + "news": "", + "safe_title": "Bursa of Fabricius", + "transcript": "", + "alt": "If an anatomical structure is named for a person, it means they were the only person to have it. Pierre Paul Broca had a special area of his brain that created powerful magnetic fields, enabling him to do 19th century fMRI research.", + "img": "https://imgs.xkcd.com/comics/bursa_of_fabricius.png", + "title": "Bursa of Fabricius", + "day": "1" +} \ No newline at end of file diff --git a/src/models/xkcdComic.py b/src/models/xkcdComic.py new file mode 100644 index 0000000..893f8de --- /dev/null +++ b/src/models/xkcdComic.py @@ -0,0 +1,13 @@ +class Comic: + def __init__(self, data) -> None: + self.day = data['day'] + self.month = data['month'] + self.year = data['year'] + self.num = data['num'] + self.link = data['link'] + self.news = data['news'] + self.safe_title = data['safe_title'] + self.transcript = data['transcript'] + self.alt = data['alt'] + self.img = data['img'] + self.title = data['title']