Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d3c9780
Add WTF Python Command
Shivansh-007 Mar 2, 2021
56ea9e2
Fix grammar in docstrings, remove redundant variable, remove the use …
Shivansh-007 Mar 5, 2021
d23cc4d
Fix indentation issues and make use of triple quotes
Shivansh-007 Mar 5, 2021
4dcc2fe
Update docstrings and remove redundant list()
Shivansh-007 Mar 6, 2021
415b7a4
Change minimum certainty to 75.
Shivansh-007 Mar 8, 2021
3b8eadd
Make 'make_embed' function a non async function
Shivansh-007 Mar 12, 2021
6465862
Try to unload WTFPython Extension if max fetch requests hit i.e. 3 el…
Shivansh-007 Mar 14, 2021
a2cdfe9
Correct log messages.
Shivansh-007 Mar 14, 2021
3ae3aad
Make flake8 happy :D
Shivansh-007 Mar 14, 2021
4a18a2d
Remove redundant class attributes and async functions.
Shivansh-007 Mar 15, 2021
2a518f9
Apply requested grammar and style changes.
Shivansh-007 Mar 15, 2021
a4c494f
Fix unload and load extension logic.
Shivansh-007 Mar 15, 2021
f82169f
Fix typo in `WTF_PYTHON_RAW_URL`
Shivansh-007 Apr 7, 2021
d893d42
Changed fuzzy_wuzzy to rapidfuzz
brad90four Sep 3, 2021
ba60b8a
Merge branch 'main' of https://github.com/python-discord/sir-lancebot…
brad90four Sep 7, 2021
a702969
Merge main
brad90four Sep 7, 2021
1e7b581
Move wtf_python.py to bot/exts/utilities, flake8
brad90four Sep 7, 2021
4c0ae39
Fix trailing commas and long lines
brad90four Sep 7, 2021
62d3a81
# This is a combination of 3 commits.
brad90four Sep 7, 2021
9d59b52
Squashing commits
brad90four Sep 8, 2021
4eb7c67
Pulling from main
brad90four Sep 10, 2021
2ca1691
Add debug logs, and send embed
brad90four Sep 11, 2021
f6e9d28
Add markdown file creation
brad90four Sep 12, 2021
0eb3670
Move the list(map(str.strip , ...) to for loop
brad90four Sep 12, 2021
cc55239
Remove line
brad90four Sep 12, 2021
5dcc43f
Use StringIO for file creation
brad90four Sep 12, 2021
917de69
Update file creation with StringIO
brad90four Sep 12, 2021
d74c8f3
Remove embed file preview
brad90four Sep 17, 2021
f49c9e0
chore: update wtf_python docstring
brad90four Sep 23, 2021
3adc085
chore: change regex to search, remove file preview
brad90four Sep 24, 2021
8c1f3ec
feat: update caching as recommended
brad90four Sep 27, 2021
f3d752a
chore: remove logging statements
brad90four Sep 28, 2021
a1832be
feat: scheduled task for fetch_readme
brad90four Sep 28, 2021
761a3f7
chore: fix hyperlink, remove dead code
brad90four Sep 28, 2021
a26ca12
fix: capitalization clean up
brad90four Sep 28, 2021
c942f32
chore: remove unused code
brad90four Sep 29, 2021
170676c
chore: remove more unused code
brad90four Sep 29, 2021
32a0f9a
feat: add light grey logo image in embed
brad90four Sep 30, 2021
04d71f8
feat: add light grey image
brad90four Sep 30, 2021
bc64aa6
chore: remove debug log message
brad90four Oct 4, 2021
2197fc6
feat: add found search result header
brad90four Oct 4, 2021
f60b724
feat: limit user query to 50 characters
brad90four Oct 6, 2021
14e9d7b
cleanup: remove debug logging
brad90four Oct 7, 2021
cd58665
fix: restructure if not match statement
brad90four Oct 7, 2021
ca1e447
Merge branch 'main' into feature/wtf-python
Xithrius Oct 22, 2021
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
126 changes: 126 additions & 0 deletions bot/exts/utilities/wtf_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import logging
import random
import re
from typing import Optional

import rapidfuzz
from discord import Embed, File
from discord.ext import commands, tasks

from bot import constants
from bot.bot import Bot

log = logging.getLogger(__name__)

WTF_PYTHON_RAW_URL = "http://raw.githubusercontent.com/satwikkansal/wtfpython/master/"
BASE_URL = "https://github.com/satwikkansal/wtfpython"
LOGO_PATH = "./bot/resources/utilities/wtf_python_logo.jpg"

ERROR_MESSAGE = f"""
Unknown WTF Python Query. Please try to reformulate your query.

**Examples**:
```md
{constants.Client.prefix}wtf wild imports
{constants.Client.prefix}wtf subclass
{constants.Client.prefix}wtf del
```
If the problem persists send a message in <#{constants.Channels.dev_contrib}>
"""

MINIMUM_CERTAINTY = 55


class WTFPython(commands.Cog):
"""Cog that allows getting WTF Python entries from the WTF Python repository."""

def __init__(self, bot: Bot):
self.bot = bot
self.headers: dict[str, str] = {}
self.fetch_readme.start()

@tasks.loop(minutes=60)
async def fetch_readme(self) -> None:
"""Gets the content of README.md from the WTF Python Repository."""
async with self.bot.http_session.get(f"{WTF_PYTHON_RAW_URL}README.md") as resp:
log.trace("Fetching the latest WTF Python README.md")
if resp.status == 200:
raw = await resp.text()
self.parse_readme(raw)

def parse_readme(self, data: str) -> None:
"""
Parses the README.md into a dict.

It parses the readme into the `self.headers` dict,
where the key is the heading and the value is the
link to the heading.
"""
# Match the start of examples, until the end of the table of contents (toc)
table_of_contents = re.search(
r"\[👀 Examples\]\(#-examples\)\n([\w\W]*)<!-- tocstop -->", data
)[0].split("\n")

for header in list(map(str.strip, table_of_contents)):
match = re.search(r"\[▶ (.*)\]\((.*)\)", header)
if match:
hyper_link = match[0].split("(")[1].replace(")", "")
self.headers[match[0]] = f"{BASE_URL}/{hyper_link}"

def fuzzy_match_header(self, query: str) -> Optional[str]:
"""
Returns the fuzzy match of a query if its ratio is above "MINIMUM_CERTAINTY" else returns None.

"MINIMUM_CERTAINTY" is the lowest score at which the fuzzy match will return a result.
The certainty returned by rapidfuzz.process.extractOne is a score between 0 and 100,
with 100 being a perfect match.
"""
match, certainty, _ = rapidfuzz.process.extractOne(query, self.headers.keys())
return match if certainty > MINIMUM_CERTAINTY else None

@commands.command(aliases=("wtf", "WTF"))
async def wtf_python(self, ctx: commands.Context, *, query: str) -> None:
"""
Search WTF Python repository.

Gets the link of the fuzzy matched query from https://github.com/satwikkansal/wtfpython.
Usage:
--> .wtf wild imports
"""
if len(query) > 50:
embed = Embed(
title=random.choice(constants.ERROR_REPLIES),
description=ERROR_MESSAGE,
colour=constants.Colours.soft_red,
)
match = None
else:
match = self.fuzzy_match_header(query)

if not match:
embed = Embed(
title=random.choice(constants.ERROR_REPLIES),
description=ERROR_MESSAGE,
colour=constants.Colours.soft_red,
)
await ctx.send(embed=embed)
return

embed = Embed(
title="WTF Python?!",
colour=constants.Colours.dark_green,
description=f"""Search result for '{query}': {match.split("]")[0].replace("[", "")}
[Go to Repository Section]({self.headers[match]})""",
)
logo = File(LOGO_PATH, filename="wtf_logo.jpg")
embed.set_thumbnail(url="attachment://wtf_logo.jpg")
await ctx.send(embed=embed, file=logo)

def cog_unload(self) -> None:
"""Unload the cog and cancel the task."""
self.fetch_readme.cancel()


def setup(bot: Bot) -> None:
"""Load the WTFPython Cog."""
bot.add_cog(WTFPython(bot))
Binary file added bot/resources/utilities/wtf_python_logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.