Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ flake8-bugbear = "==24.8.19"
flake8-docstrings-complete = "==1.4.1"
flake8-modern-annotations = "==1.6.0"
flake8-variables-names = "==0.0.6"
flickrapi = "==2.4.0"
gino = "==1.0.1"
gitpython = "==3.1.43"
hypothesis = "==6.122.4"
Expand Down
152 changes: 148 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ api:
api_keys:
cat:
dumpdbg:
flickr_api_key:
flicker_api_secret:
giphy:
google:
google_cse:
Expand Down
112 changes: 112 additions & 0 deletions techsupport_bot/commands/flickr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""Module for the flickr extension for the discord bot."""

from __future__ import annotations

import random
from typing import TYPE_CHECKING, Self

import flickrapi
from core import auxiliary, cogs
from discord.ext import commands

if TYPE_CHECKING:
import bot


async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the flickr plugin into the bot

Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
"""

await bot.add_cog(Flickr(bot=bot))


class Flickr(cogs.BaseCog):
"""The class for the flickr commands"""

@auxiliary.with_typing
@commands.command(
name="flickr",
brief="Gets an animal",
description="Gets an animal",
aliases=["animal"],
)
@commands.cooldown(5, 60, commands.BucketType.user)
async def flickr(
self: Self, ctx: commands.Context, *, animal: str = "animal"
) -> None:
"""Fetches and displays an image of the specified animal from Flickr.

Args:
ctx (commands.Context): The context in which the command was run.
animal (str, optional): The animal to search for. Defaults to "animal".
"""
if ctx.author.guild_permissions.administrator:
Flickr.flickr.reset_cooldown(ctx)

flickrAPI = flickrapi.FlickrAPI(
ctx.bot.file_config.api.api_keys.flickr_api_key.encode("utf-8"),
ctx.bot.file_config.api.api_keys.flicker_api_secret.encode("utf-8"),
cache=True,
)

try:
initial_search = flickrAPI.photos.search(
text=animal,
tags=f"{animal}, -person, -people, -portrait, -selfie, -human",
tag_mode="all",
extras="url_c",
per_page=1,
format="parsed-json",
content_type=1,
safe_search=1,
sort="relevance",
)
total_pages = initial_search["photos"]["pages"]
max_pages_to_check = min(total_pages, 1000)

for _ in range(10):
random_page = (
random.randint(1, max_pages_to_check)
if max_pages_to_check > 1
else 1
)
photos = flickrAPI.photos.search(
text=animal,
tags=f"{animal}, -person, -people, -portrait, -selfie, -human",
tag_mode="all",
extras="url_c",
per_page=1,
page=random_page,
format="parsed-json",
content_type=1,
safe_search=1,
sort="relevance",
license="4,6,7,9",
media="photos",
)

photo_list = photos.get("photos", {}).get("photo", [])
photo_with_url = next(
(photo for photo in photo_list if "url_c" in photo), None
)
if photo_with_url:
await ctx.send(photo_with_url["url_c"])
return

await auxiliary.send_deny_embed(
message=f"No suitable {animal} photos found.",
channel=ctx.channel,
)

except flickrapi.exceptions.FlickrError as e:
await ctx.send(
f"An error occurred while fetching data from Flickr: {str(e)}. Please try later."
)

except Exception as e:
await ctx.send(
f"An unexpected error occurred: {str(e)}. The issue has been logged."
)
Loading