From 9399919b0836759b5776555d34da18e3c892ce89 Mon Sep 17 00:00:00 2001 From: corbz Date: Sun, 18 Feb 2024 21:43:28 +0000 Subject: [PATCH] Removed unused and commented code --- src/extensions/rss.py | 686 +----------------------------------------- 1 file changed, 3 insertions(+), 683 deletions(-) diff --git a/src/extensions/rss.py b/src/extensions/rss.py index 0e69f3d..6dfcfa6 100644 --- a/src/extensions/rss.py +++ b/src/extensions/rss.py @@ -5,35 +5,20 @@ Loading this file via `commands.Bot.load_extension` will add `FeedCog` to the bo import logging from typing import Tuple -from dataclasses import asdict import aiohttp import validators from feedparser import FeedParserDict, parse from discord.ext import commands -from discord import Interaction, Embed, Colour, TextChannel, Permissions -from discord.app_commands import Choice, Group, autocomplete, choices, rename, command -from sqlalchemy import insert, select, and_, delete -from sqlalchemy.exc import NoResultFound, IntegrityError +from discord import Interaction, TextChannel +from discord.app_commands import Choice, Group, autocomplete, rename from api import API -from feed import Source, RSSFeed, Subscription, SubscriptionChannel, TrackedContent -from errors import IllegalFeed -from db import ( - DatabaseManager, - SentArticleModel, - RssSourceModel, - FeedChannelModel, - AuditModel -) +from feed import Subscription, SubscriptionChannel, TrackedContent from utils import ( Followup, PaginationView, get_rss_data, - followup, - audit, - extract_error_info, - get_unparsed_feed ) log = logging.getLogger(__name__) @@ -91,24 +76,6 @@ async def validate_rss_source(nickname: str, url: str) -> Tuple[str | None, Feed return None, feed -async def set_all_articles_as_sent(inter, channel: TextChannel, feed_id: int, rss_url: str): - unparsed_feed = await get_unparsed_feed(rss_url) - source = Source.from_parsed(parse(unparsed_feed)) - articles = source.get_latest_articles() - - async with DatabaseManager() as database: - query = insert(SentArticleModel).values([ - { - "discord_server_id": inter.guild_id, - "discord_channel_id": channel.id, - "discord_message_id": -1, - "article_url": article.url, - "feed_channel_id": feed_id - } - for article in articles - ]) - await database.session.execute(query) - class FeedCog(commands.Cog): """ @@ -125,27 +92,6 @@ class FeedCog(commands.Cog): log.info("%s cog is ready", self.__class__.__name__) - # async def autocomplete_channels(self, inter: Interaction, name: str) -> list[Choice]: - # """""" - - # log.debug("autocompleting channels '%s'", name) - - # try: - # async with aiohttp.ClientSession() as session: - # api = API(self.bot.api_token, session) - # results, _ = await api.get_channel(server=inter.guild_id) - - # except Exception as exc: - # log.error(exc) - # return [] - - # channels = Channel.from_list(results) - - # return [ - # Choice(name=channel.get_textchannel(self.bot).name, value=channel.id) - # for channel in channels - # ] - async def autocomplete_subscriptions(self, inter: Interaction, name: str) -> list[Choice]: """""" @@ -192,101 +138,6 @@ class FeedCog(commands.Cog): for link in subscription_channels ] - - # channel_group = Group( - # name="channels", - # description="channel commands", - # guild_only=True - # ) - - # @channel_group.command(name="add") - # @autocomplete(sub_uuid=autocomplete_subscriptions) - # @rename(sub_uuid="subscription", textchannel="channel") - # async def new_channel(self, inter: Interaction, sub_uuid: str, textchannel: TextChannel): - # """Create a new channel.""" - - # await inter.response.defer() - - # try: - # async with aiohttp.ClientSession() as session: - # api = API(self.bot.api_token, session) - # await api.create_channel(textchannel.id) - # # sub = await api.get_subscription(sub_uuid) - # # sub["channels"].append(textchannel.id) - # # await api.put_subscription(sub_uuid, sub) - - # except Exception as exc: - # return await ( - # Followup(exc.__class__.__name__, str(exc)) - # .error() - # .send(inter) - # ) - - # await ( - # Followup("Channel Assigned!") - # .fields( - # subscription=sub_uuid, - # channel=textchannel.mention - # ) - # .added() - # .send(inter) - # ) - - # @channel_group.command(name="remove") - # @autocomplete(id=autocomplete_channels) - # @rename(id="choice") - # async def remove_channel(self, inter: Interaction, id: int): - # """Remove a channel.""" - - # await inter.response.defer() - - # try: - # async with aiohttp.ClientSession() as session: - # api = API(self.bot.api_token, session) - # await api.delete_channel(id) - - # except Exception as exc: - # return await ( - # Followup(exc.__class__.__name__, str(exc)) - # .error() - # .send(inter) - # ) - - # await ( - # Followup("Channel Removed!", str(id)) - # .trash() - # .send(inter) - # ) - - # @channel_group.command(name="list") - # async def list_channels(self, inter: Interaction): - - # log.debug("Listing all subscription channels with this server.") - - # await inter.response.defer() - - # page = 1 - # pagesize = 10 - - # def formatdata(index, item): - # item = Channel.from_dict(item) - # text_channel = item.get_textchannel(self.bot) - - # key = f"{index}. {text_channel.mention}" - # value = f"[RSS]({item.rss_url}) · [API]({API.CHANNEL_ENDPOINT}{item.uuid}/)" - # return key, value - - # async def getdata(page): - # async with aiohttp.ClientSession() as session: - # api = API(self.bot.api_token, session) - # return await api.get_subscriptions( - # server=inter.guild.id, page=page, page_size=pagesize - # ) - - # embed = Followup(f"Subscriptions in {inter.guild.name}").info()._embed - # pagination = PaginationView(self.bot, inter, embed, getdata, formatdata, pagesize, page) - # await pagination.send() - subscription_group = Group( name="subscriptions", description="subscription commands", @@ -494,537 +345,6 @@ class FeedCog(commands.Cog): await pagination.send() - # # All RSS commands belong to this group. - # feed_group = Group( - # name="feed", - # description="Commands for RSS sources.", - # default_permissions=Permissions.elevated(), - # guild_only=True # We store guild IDs in the database, so guild only = True - # ) - - # @feed_group.command(name="new") - # async def add_rssfeed(self, inter: Interaction, name: str, url: str): - # """Add a new RSS Feed for this server. - - # Args: - # inter (Interaction): Represents the discord command interaction. - # name (str): A nickname used to refer to this RSS Feed. - # url (str): The URL of the RSS Feed. - # """ - - # await inter.response.defer() - - # try: - # rssfeed = await self.bot.functions.create_new_rssfeed(name, url, inter.guild_id) - # except Exception as exc: - # await ( - # Followup(exc.__class__.__name__, str(exc)) - # .error() - # .send(inter) - # ) - # else: - # await ( - # Followup("New RSS Feed") - # .image(rssfeed.image) - # .fields(uuid=rssfeed.uuid, name=name, url=url) - # .added() - # .send(inter) - # ) - - # @feed_group.command(name="delete") - # @autocomplete(uuid=autocomplete_rssfeed) - # @rename(uuid="rssfeed") - # async def delete_rssfeed(self, inter: Interaction, uuid: str): - # """Delete an existing RSS Feed for this server. - - # Args: - # inter (Interaction): Represents the discord command interaction. - # uuid (str): The UUID of the - # """ - - # await inter.response.defer() - - # try: - # rssfeed = await self.bot.functions.delete_rssfeed(uuid) - # except NoResultFound: - # await ( - # Followup( - # "Feed Not Found Error", - # "A Feed with these parameters could not be found." - # ) - # .error() - # .send(inter) - # ) - # else: - # await ( - # Followup("Feed Deleted") - # .image(rssfeed.image) - # .fields(uuid=rssfeed.uuid, name=rssfeed.name, url=rssfeed.url) - # .trash() - # .send(inter) - # ) - - # @feed_group.command(name="list") - # async def list_rssfeeds(self, inter: Interaction): - # """Provides a list of all RSS Feeds - - # Args: - # inter (Interaction): Represents the discord command interaction. - # """ - - # await inter.response.defer() - - # page = 1 - # pagesize = 10 - - # try: - # def formatdata(index, item): - # key = f"{index}. {item.name}" - # value = f"[RSS]({item.url}) · [API]({API.RSS_FEED_ENDPOINT}{item.uuid}/)" - # return key, value - - # async def getdata(page): - # data, count = await self.bot.functions.get_rssfeeds(inter.guild_id, page, pagesize) - # return data, count - - # embed = Followup(f"Available RSS Feeds in {inter.guild.name}").info()._embed - # pagination = PaginationView(self.bot, inter, embed, getdata, formatdata, pagesize, 1) - # await pagination.send() - - # except Exception as exc: - # await ( - # Followup(exc.__class__.__name__, str(exc)) - # .error() - # .send(inter) - # ) - - # # @feed_group.command(name="fetch") - # # @rename(max_="max") - # # @autocomplete(rss=source_autocomplete) - # async def fetch_rss(self, inter: Interaction, rss: str, max_: int=1): - # """Fetch an item from the specified RSS feed. - - # Parameters - # ---------- - # inter : Interaction - # Represents an app command interaction. - # rss : str - # The RSS feed to fetch from. - # max_ : int, optional - # Maximum number of items to fetch, by default 1, limits at 5. - # """ - - # await inter.response.defer() - - # if max_ > 5: - # followup(inter, "It looks like you have requested too many articles.\nThe limit is 5") - # return - - # invalid_message, feed = await validate_rss_source("", rss) - # if invalid_message: - # await followup(inter, invalid_message) - # return - - # source = Source.from_parsed(feed) - # articles = source.get_latest_articles(max_) - - # if not articles: - # await followup(inter, "Sorry, I couldn't find any articles from this feed.") - # return - - # async with aiohttp.ClientSession() as session: - # embeds = [await article.to_embed(session) for article in articles] - - # async with DatabaseManager() as database: - # query = insert(SentArticleModel).values([ - # { - # "discord_server_id": inter.guild_id, - # "discord_channel_id": inter.channel_id, - # "discord_message_id": inter.id, - # "article_url": article.url, - # } - # for article in articles - # ]) - # await database.session.execute(query) - # await audit(self, - # f"User is requesting {max_} articles from {source.name}", - # inter.user.id, database=database - # ) - - # await followup(inter, embeds=embeds) - - # # Help ---- ---- ---- - - # @feed_group.command(name="help") - # async def get_help(self, inter: Interaction): - # """Get help on how to use my commands. - - # Parameters - # ---------- - # inter : Interaction - # Represents an app command interaction. - # """ - - # await inter.response.defer() - - # description = ( - # "`/feed add ` \n\n" - # "Save a new RSS feed to the bot. This can be referred to later, when assigning " - # "channels to receive content from these RSS feeds." - - # "\n\n\n`/feed remove