From ea21f8bb69120505fa0d2519e2c855bdd3892fd8 Mon Sep 17 00:00:00 2001 From: "DavidCrompton1192@gmail.com" Date: Fri, 17 Mar 2023 22:16:34 -0400 Subject: [PATCH] Soup! --- flake.nix | 1 + handyhelper/__init__.py | 22 ++++++++++++++++------ poetry.lock | 33 ++++++++++++++++++++++++++++++++- pyproject.toml | 1 + 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index c18d6bc..4540ab3 100644 --- a/flake.nix +++ b/flake.nix @@ -65,6 +65,7 @@ DOC }; devShells.default = pkgs.mkShell { + PYPROJECT_FILE = ./pyproject.toml; packages = with pkgs; [ poetry2nix.packages.${system}.poetry (mkPoetryEnv { diff --git a/handyhelper/__init__.py b/handyhelper/__init__.py index a53bd7a..3156644 100644 --- a/handyhelper/__init__.py +++ b/handyhelper/__init__.py @@ -4,6 +4,7 @@ from typing import Optional import asyncio import os import requests as req +from bs4 import BeautifulSoup as soup bot = commands.Bot() @@ -12,7 +13,16 @@ async def search(txt): 'request':txt }) - return resp + doc = soup(resp.text, 'html_parser') + + if 'not found' in doc.title: + return None + + ref = doc.find("div", {'id': 'citation'}).get_text() + + return { + 'ref': ref + } @bot.event async def on_ready(): @@ -28,11 +38,11 @@ async def summarize( resp = await search(article) - await interaction.followup.send(f"""Search Response: - ``` - {resp.text[:1000]} - ``` - """) + if resp is None: + await interaction.followup.send(f"Unable to find article: {article}") + return + + await interaction.followup.send(f"Article Found: \n{resp['ref']}") def main(): # TODO: Import bot token from env diff --git a/poetry.lock b/poetry.lock index 3d6cd74..df4077f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -197,6 +197,25 @@ files = [ {file = "backcall-0.2.0.tar.gz", hash = "sha256:5cbdbf27be5e7cfadb448baf0aa95508f91f2bbc6c6437cd9cd06e2a4c215e1e"}, ] +[[package]] +name = "beautifulsoup4" +version = "4.11.2" +description = "Screen-scraping library" +category = "main" +optional = false +python-versions = ">=3.6.0" +files = [ + {file = "beautifulsoup4-4.11.2-py3-none-any.whl", hash = "sha256:0e79446b10b3ecb499c1556f7e228a53e64a2bfcebd455f370d8927cb5b59e39"}, + {file = "beautifulsoup4-4.11.2.tar.gz", hash = "sha256:bc4bdda6717de5a2987436fb8d72f45dc90dd856bdfd512a1314ce90349a0106"}, +] + +[package.dependencies] +soupsieve = ">1.2" + +[package.extras] +html5lib = ["html5lib"] +lxml = ["lxml"] + [[package]] name = "certifi" version = "2022.12.7" @@ -861,6 +880,18 @@ files = [ {file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, ] +[[package]] +name = "soupsieve" +version = "2.4" +description = "A modern CSS selector implementation for Beautiful Soup." +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "soupsieve-2.4-py3-none-any.whl", hash = "sha256:49e5368c2cda80ee7e84da9dbe3e110b70a4575f196efb74e51b94549d921955"}, + {file = "soupsieve-2.4.tar.gz", hash = "sha256:e28dba9ca6c7c00173e34e4ba57448f0688bb681b7c5e8bf4971daafc093d69a"}, +] + [[package]] name = "stack-data" version = "0.6.2" @@ -1125,4 +1156,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "0430177a1a511ae2bf15ad52cb128b5f18ec59dd2462e0a6e9bf907426f45aa1" +content-hash = "270b70e2eeff44e2b0bb07902f333dcbc45bb8c453a16769f9fd45b5a70c4993" diff --git a/pyproject.toml b/pyproject.toml index 2204bf4..be96483 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ python = "^3.10" requests = "^2.28.2" nextcord = "^2.4.1" openai = "^0.27.2" +beautifulsoup4 = "^4.11.2" [tool.poetry.group.dev.dependencies] ipython = "^8.11.0"