From 58a5756f4dbd045f2e754886f7a3969cd0e37ebc Mon Sep 17 00:00:00 2001 From: sgoudham Date: Tue, 25 Aug 2020 02:24:12 +0100 Subject: [PATCH] Added commentary for instagram command --- cogs/fun.py | 110 ++++++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/cogs/fun.py b/cogs/fun.py index c4ec45d7..cfc1393a 100644 --- a/cogs/fun.py +++ b/cogs/fun.py @@ -346,56 +346,66 @@ class Fun(Cog): async def insta_info(self, ctx, *, user_name): """Retrieve a persons Instagram profile information""" - url = f"https://apis.duncte123.me/insta/{user_name}" - async with aiohttp.ClientSession() as session: - async with await session.get(url=url) as response: - if response.status == 200: - insta = await response.json() - data = insta["user"] - images = insta["images"] - private = data["is_private"] - verified = data["is_verified"] - if not private: - image_url = images[0]["url"] - image_caption = images[0]["caption"] - - full_name = data["full_name"] - username = data["username"] - pfp = data["profile_pic_url"] - - followers = data["followers"]["count"] - following = data["following"]["count"] - uploads = data["uploads"]["count"] - biography = data["biography"] - - elif response.status == 422: - await self.bot.generate_embed(ctx, desc="**Instagram Username Not Found!**") - return - - # Setting bools to ticks/cross emojis - verif = self.bot.tick if verified else self.bot.cross - priv = self.bot.tick if private else self.bot.cross - - page_url = images[0]["page_url"] if not private else f"https://www.instagram.com/{username}/" - - desc = f"**Full Name:** {full_name}" \ - f"\n**Bio:** {biography}" \ - f"\n\n**Verified?:** {verif} | **Private?:** {priv}" \ - f"\n**Following:** {following} | **Followers:** {followers}" \ - f"\n**Upload Count:** {uploads}" - embed = discord.Embed(title=f"{username}'s Instagram", - description=desc, - url=page_url, - colour=self.bot.random_colour(), - timestamp=datetime.datetime.utcnow()) - embed.set_thumbnail(url=pfp) - embed.set_footer(text=f"Requested By {ctx.author}", icon_url=ctx.author.avatar_url) - - if not private: - embed.add_field(name="My Latest Post", - value=f"**Caption:** {image_caption}", - inline=False) - embed.set_image(url=image_url) + with ctx.typing(): + # Request profile information from API + url = f"https://apis.duncte123.me/insta/{user_name}" + async with aiohttp.ClientSession() as session: + async with await session.get(url=url) as response: + + # When succesful, read data from json + if response.status == 200: + insta = await response.json() + + data = insta["user"] + images = insta["images"] + private = data["is_private"] + verified = data["is_verified"] + + full_name = data["full_name"] + username = data["username"] + pfp = data["profile_pic_url"] + + followers = data["followers"]["count"] + following = data["following"]["count"] + uploads = data["uploads"]["count"] + biography = data["biography"] + + # When profile isn't private, grab the information of their last post + if not private: + image_url = images[0]["url"] + image_caption = images[0]["caption"] + + # Send error if no instagram profile was found with given username + elif response.status == 422: + await self.bot.generate_embed(ctx, desc="**Instagram Username Not Found!**") + return + + # Setting bools to ticks/cross emojis + verif = self.bot.tick if verified else self.bot.cross + priv = self.bot.tick if private else self.bot.cross + + # Set the page url to the last post or the profile based on privacy settings + page_url = images[0]["page_url"] if not private else f"https://www.instagram.com/{username}/" + + desc = f"**Full Name:** {full_name}" \ + f"\n**Bio:** {biography}" \ + f"\n\n**Verified?:** {verif} | **Private?:** {priv}" \ + f"\n**Following:** {following} | **Followers:** {followers}" \ + f"\n**Upload Count:** {uploads}" + embed = discord.Embed(title=f"{username}'s Instagram", + description=desc, + url=page_url, + colour=self.bot.random_colour(), + timestamp=datetime.datetime.utcnow()) + embed.set_thumbnail(url=pfp) + embed.set_footer(text=f"Requested By {ctx.author}", icon_url=ctx.author.avatar_url) + + # When profile is not private, display the last post with the caption + if not private: + embed.add_field(name="My Latest Post", + value=f"**Caption:** {image_caption}", + inline=False) + embed.set_image(url=image_url) await ctx.send(embed=embed)