import nextcord as nc from nextcord.ext import commands from typing import Optional import asyncio import os import requests as req from bs4 import BeautifulSoup as soup import pdftotext bot = commands.Bot() async def search(txt): url = "https://www.sci-hub.st/" resp = req.post(url, data={ 'request':txt }) doc = soup(resp.text, 'html.parser') if 'not found' in doc.find('title').get_text(): return None ref = doc.find('div', {'id': 'citation'}).get_text() pdf = doc.find('embed', {'id': 'pdf'})['src'] return { 'ref': ref, 'pdf': pdf[2:] } async def getPDF(url): resp = req.get(url, stream=True) pdf = resp.content pages = pdftotext.PDF(pdf) return "\n\n".join(pages) @bot.event async def on_ready(): print(f'We have logged in as {bot.user}') @bot.slash_command(name="summarize", description="Summarize all or part of an article", dm_permission=True) async def summarize( interaction: nc.Interaction, article: str, part: Optional[str] = nc.SlashOption(required=False) ): await interaction.response.defer(ephemeral=False, with_message=True) resp = await search(article) if resp is None: await interaction.followup.send(f"Unable to find article: {article}") return await interaction.followup.send(f"Article Found: {resp['ref']} Parsing PDF...") await interaction.followup.edit_message(f"""Article Found: {resp['ref']} ``` {getPDF(resp['pdf'])[:1000]} ```""") def main(): # TODO: Import bot token from env bot.run(os.environ["DISCORD_TOKEN"]) if __name__ == "__main__": print("Handy Helper has Begun!") main()