From 6f0ad0de873ca6af1bcbdc72b34c29b93e4b6554 Mon Sep 17 00:00:00 2001 From: Matt C Date: Tue, 21 Dec 2021 21:11:26 -0500 Subject: [PATCH] working thing --- .gitignore | 1 + cogs/internet.py | 51 --------------------- cogs/rss.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++ cogs/status.py | 1 + config.txt | 29 ++++++++---- requirements.txt | 4 +- 6 files changed, 141 insertions(+), 60 deletions(-) delete mode 100644 cogs/internet.py create mode 100644 cogs/rss.py diff --git a/.gitignore b/.gitignore index eb4929e..b7f9d3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Custom system_log.txt* .cavetoken +package-cache/ # ---> Python # Byte-compiled / optimized / DLL files diff --git a/cogs/internet.py b/cogs/internet.py deleted file mode 100644 index 92e0853..0000000 --- a/cogs/internet.py +++ /dev/null @@ -1,51 +0,0 @@ -import os, json, random -import urllib.parse -import urllib - -import discord -from discord.ext import commands -import asyncio - -from util_functions import * -from global_config import configboi - -# Fun internet things -class Internet(commands.Cog): - """Useful tools on the interwebs""" - - def __init__(self, bot): - self.bot = bot - self.confmgr = configboi("config.txt", False) - - async def getasjson(self, url): - try: - data = await run_command_shell('curl "' + url + '"') - return json.loads(data) - except Exception as e: - return '{"haha":"heeho"}' - - @commands.command() - async def kernel(self, ctx): - """Get Linux kernel info for host and latest""" - try: - await ctx.send(embed=infmsg("Kernel", "Getting kernel info.")) - data = await self.getasjson("https://www.kernel.org/releases.json") - new_ver = data["latest_stable"]["version"] - mine = await run_command_shell("uname -r") - msg = ( - "I'm running: `" - + mine - + "`\nKernel.org reports stable is: `" - + new_ver - + "`" - ) - await ctx.send(embed=infmsg("Kernel", msg)) - except Exception as e: - await ctx.send( - embed=errmsg("Kernel", "Had an issue getting info: `" + str(e) + "`") - ) - syslog.log("Internet-Important", "Kernel command had error: " + str(e)) - -# End fun internet things -def setup(bot): - bot.add_cog(Internet(bot)) diff --git a/cogs/rss.py b/cogs/rss.py new file mode 100644 index 0000000..d6715e3 --- /dev/null +++ b/cogs/rss.py @@ -0,0 +1,115 @@ +import sys, datetime, os + +import discord +from discord.ext import commands, tasks +import feedparser,requests + +from global_config import configboi + +from util_functions import * + +sc = configboi("config.txt", False) +REFRESH_TIME = sc.getasint("REFRESH_TIME") + +class RSS(commands.Cog): + """This cog handles RSS stuff""" + + def __init__(self, bot): + self.bot = bot + self.confmgr = configboi("config.txt", False) + + self.package_names = self.confmgr.getaslist("WATCHED_PACKAGES") + temp = self.confmgr.getaslist("FEED_URLS") + self.gitea_urls = self.confmgr.getaslist("GITEA_URLS") + self.package_channel = self.confmgr.getasint("PACKAGE_CHANNEL") + + + self.feed_urls = [] + for feed in temp: + self.feed_urls.append("https://" + feed) + + self.chan = self.bot.get_channel(self.package_channel) + if self.chan is None: + syslog.log("RSS", "Package task failed because we couldn't find the target channel.") + sys.exit(1) + + self.root = "package-cache" + ensure(self.root) + + self.package_task.start() + + + def cog_unload(self): + self.package_task.cancel() + + @commands.Cog.listener() + async def on_ready(self): + syslog.log("RSS", "Running initial package check") + await self.check_packages() + + @tasks.loop(seconds=REFRESH_TIME) + async def package_task(self): + await self.check_packages() + + @package_task.before_loop + async def before_status_task(self): + syslog.log( + "RSS", "Waiting for bot to be ready before starting package task" + ) + await self.bot.wait_until_ready() + syslog.log("RSS", "Bot is ready. Enabling package task") + + async def sendwarn(self, msg): + await self.chan.send(embed=warnmsg("RSS",msg)) + + async def senderr(self, msg): + await self.chan.send(embed=errmsg("RSS",msg)) + + async def send(self, msg): + await self.chan.send(embed=infmsg("RSS",msg)) + + def dolog(self, msg): + syslog.log("RSS", msg) + + async def check_packages(self): + try: + for feed in self.feed_urls: + self.dolog("Checking: " + feed) + previous = "" + if check(self.root + "/" + feed.replace("/","-")): + self.dolog("Found previous cached data") + previous = open(self.root + "/" + feed.replace("/","-")).read().strip() + else: + self.dolog("No previous data") + + new = requests.get(feed).text + + if new != previous: + self.dolog("New data is different") + if check(self.root + "/" + feed.replace("/","-")): + os.remove(self.root + "/" + feed.replace("/","-")) + with open(self.root + "/" + feed.replace("/","-"), "w") as f: + f.write(new) + self.dolog("Updated cache") + + d = feedparser.parse(new) + hadNew = False + + for item in d.entries: + for tgt in self.package_names: + if tgt in str(item['title']): + await self.send("Package change: `" + str(item['title']) + "`") + hadNew = True + break + + if not hadNew: + await self.send("No package changes to report :)") + + else: + await self.send("No package changes to report :)") + except Exception as e: + syslog.log("RSS ERROR", str(e)) + + +def setup(bot): + bot.add_cog(RSS(bot)) diff --git a/cogs/status.py b/cogs/status.py index 92e629b..76cb409 100644 --- a/cogs/status.py +++ b/cogs/status.py @@ -22,6 +22,7 @@ class Status(commands.Cog): def cog_unload(self): self.status_task.cancel() + self.uptime_logger.cancel() async def setDefaultStatus(self): ac_type = None diff --git a/config.txt b/config.txt index 0109e3f..bd0f624 100644 --- a/config.txt +++ b/config.txt @@ -8,12 +8,12 @@ # General bot stuff # Types: watching, listening, playing -DEFAULT_STATUS_TYPE:playing +DEFAULT_STATUS_TYPE:watching # These are currently the only substitutions # - {number_users} # - {guild_count} -DEFAULT_STATUS_TEXT:ooga booga w/ {number_users} friends +DEFAULT_STATUS_TEXT:for ; # Any default cogs you don't want to load: # Example: UNLOAD_COGS:Internet,Speak @@ -28,11 +28,24 @@ OWNER:117445905572954121 # Note {command} is the only substitution allowed here (we do auto-mention the user) WRONG_PERMS:You're not a special snowflake, so you can't run `{command}` -# {username} is the only one here -NEW_MEMBER:Welcome, {username} +# Should the owner get a DM when the bot restarts +OWNER_DM_RESTART:True -# and {channel} is the only one here -INTRO_CHANNEL:You should go to {channel}, since it's your first time here. +###################################################################################### +# Package settings # +###################################################################################### -# Should the owner get a DM when the bot restarts -OWNER_DM_RESTART:True \ No newline at end of file +# Package names to care about +WATCHED_PACKAGES:grub,arch-install-scripts,base,filesystem,lsb-release,neofetch,pfetch + +# How often to re-query the feed(s) {in seconds} +REFRESH_TIME:3600 + +# Feed source(s) +FEED_URLS:archlinux.org/feeds/packages/ + +# Crystal URL(s) +GITEA_URLS:https://git.getcryst.al/crystal/packages-any,https://git.getcryst.al/crystal/packages-x86_64 + +# Channel to send notifications +PACKAGE_CHANNEL:900844916974977125 diff --git a/requirements.txt b/requirements.txt index ec26763..a1726b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ py-cord[voice] discord-pretty-help requests -asyncio \ No newline at end of file +asyncio +requests +feedparser \ No newline at end of file