Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed Mar 21, 2024
1 parent ab72382 commit 8c6febc
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions extensions/free_games.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,48 @@

import aiohttp
import discord
from abc import ABC, abstractmethod
from discord.ext import commands

from core.bot import Substiify
import core


logger = logging.getLogger(__name__)

EPIC_STORE_FREE_GAMES_API = "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions"
EPIC_GAMES_LOGO_URL = (
"https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Epic_Games_logo.svg/50px-Epic_Games_logo.svg.png"
)

class Game(ABC):
title: str
start_date: datetime
end_date: datetime
original_price: str
discount_price: str
cover_image_url: str
store_link: str
platform_logo: str


class Platform(ABC):

API_URL: str
LOGO_URL: str

@staticmethod
@abstractmethod
async def get_free_games(self) -> list[Game]:
pass


class Game:
class EpicGamesGame(Game):
def __init__(self, game_info_json: str) -> None:
self.title: str = game_info_json["title"]
self.start_date: datetime = self._create_start_date(game_info_json)
self.end_date: datetime = self._create_end_date(game_info_json)
self.original_price: str = game_info_json["price"]["totalPrice"]["fmtPrice"]["originalPrice"]
self.discount_price: str = self._create_discount_price(game_info_json["price"])
self.cover_image_url: str = self._create_thumbnail(game_info_json["keyImages"])
self.epic_store_link: str = self._create_store_link(game_info_json)
self.store_link: str = self._create_store_link(game_info_json)
self.platform_logo: str = EpicGames.LOGO_URL


def _create_store_link(self, game_info_json: str) -> str:
offer_mappings = game_info_json["offerMappings"]
Expand Down Expand Up @@ -55,22 +76,22 @@ def _create_thumbnail(self, key_images: str) -> str:
return key_images[0]["url"]


class FreeGames(commands.Cog):
COG_EMOJI = "🕹️"
class EpicGames(Platform):

def __init__(self, bot: Substiify):
self.bot = bot
API_URL: str = "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions"
LOGO_URL: str = (
"https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Epic_Games_logo.svg/50px-Epic_Games_logo.svg.png"
)

@commands.cooldown(3, 30)
@commands.command()
async def epic(self, ctx: commands.Context):
@staticmethod
async def get_free_games(self) -> list[Game]:
"""
Show all free games from Epic Games that are currently available.
Get all free games from Epic Games
"""
all_games = ""
try:
async with aiohttp.ClientSession() as session:
async with session.get(EPIC_STORE_FREE_GAMES_API) as response:
async with session.get(EpicGames.API_URL) as response:
json_response = await response.json()
all_games = json_response["data"]["Catalog"]["searchStore"]["elements"]
except Exception as ex:
Expand All @@ -84,23 +105,38 @@ async def epic(self, ctx: commands.Context):
# Check if game is free
if game["price"]["totalPrice"]["fmtPrice"]["discountPrice"] != "0":
continue
# Check if the game is currently free
# Check if the game is _currently_ free
if game["status"] != "ACTIVE":
continue
try:
current_free_games.append(Game(game))
except Exception as ex:
logger.error(f"Error while creating 'Game' object: {ex}")


class FreeGames(commands.Cog):
COG_EMOJI = "🕹️"

def __init__(self, bot: core.Substiify):
self.bot = bot

@commands.cooldown(3, 30)
@commands.command(aliases=["fg"], usage="fg [platform]")
async def freegames(self, ctx: commands.Context):
"""
Show all free games that are currently available.
"""
current_free_games: list[Game] = await EpicGames.get_free_games()

if not current_free_games:
embed = discord.Embed(color=0x000000)
embed = discord.Embed(color=discord.Color.red())
embed.description = "Could not find any currently free games"
await ctx.send(embed=embed)

for game in current_free_games:
try:
embed = discord.Embed(title=game.title, url=game.epic_store_link, color=0x000000)
embed.set_thumbnail(url=f"{EPIC_GAMES_LOGO_URL}")
embed = discord.Embed(title=game.title, url=game.store_link, color=core.constants.PRIMARY_COLOR)
embed.set_thumbnail(url=game.platform_logo)
available_string = (
f"started <t:{int(game.start_date.timestamp())}:R>, ends <t:{int(game.end_date.timestamp())}:R>"
)
Expand All @@ -118,5 +154,5 @@ async def epic(self, ctx: commands.Context):
logger.error(f"Fail while sending free game: {ex}")


async def setup(bot: Substiify):
async def setup(bot: core.Substiify):
await bot.add_cog(FreeGames(bot))

0 comments on commit 8c6febc

Please sign in to comment.