From 0071f83362158d805988557b825e30716e9b3324 Mon Sep 17 00:00:00 2001 From: khuxkm fbexl Date: Sat, 1 Dec 2018 20:18:39 -0500 Subject: [PATCH] Initial commit --- .gitignore | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 6 ++++ README.md | 3 ++ bot.py | 32 +++++++++++++++++ rss.py | 8 +++++ state.a.json | 1 + tasks.py | 50 +++++++++++++++++++++++++++ 7 files changed, 198 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 bot.py create mode 100644 rss.py create mode 100644 state.a.json create mode 100644 tasks.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c71ff7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,98 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + +# configuration +channels.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a950386 --- /dev/null +++ b/LICENSE @@ -0,0 +1,6 @@ +MIT License +Copyright (c) 2018 khuxkm fbexl + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2c9fadc --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cosmicbot + +Handles some cosmic.voyage stuff diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..3386bcf --- /dev/null +++ b/bot.py @@ -0,0 +1,32 @@ +import teambot,tasks,rss,time + +class CosmicBot(teambot.Handler): + def __init__(self,bot): + super(CosmicBot,self).__init__(bot) + self.tasks = tasks.TaskPool() + self.tasks.add_coroutine(self.check_rss,60,dict(url="https://cosmic.voyage/rss.xml",known=[],channel="#cosmic")) + self.tasks.load_state(0) + def on_connection_established(self): + self.tasks.run() + def check_rss(self,state,base_state): + memory = state["known"] + newtrans = rss.fetchNew(state["url"],memory) + if newtrans: + memory.extend(x.guid for x in newtrans) + for trans in newtrans: + self.say(state["channel"],"Transmission recieved: {transmission.title} ({transmission.link})".format(transmission=trans)) + time.sleep(1) + state["known"]=memory + return state + def on_pubmsg(self,channel,nick,text): + if self.event.source.userhost == "khuxkm@sudoers.tilde.team" and text.strip() == "!down": + self.tasks.stop() + self.tasks.save_state(0) + self._bot.die("Stopping...") + elif self.event.source.userhost == "khuxkm@sudoers.tilde.team" and text.strip() == "!check": + self.tasks.states[0]=self.check_rss(self.tasks.states[0],self.tasks.base_state) + +if __name__=="__main__": + channels = "#cosmic".split() + bot = teambot.TeamBot(channels,"cosmicbot","localhost",chandler=CosmicBot) + bot.start() diff --git a/rss.py b/rss.py new file mode 100644 index 0000000..f227f2d --- /dev/null +++ b/rss.py @@ -0,0 +1,8 @@ +import time, feedparser + +def fetchNew(url,old): + ret = [] + for entry in feedparser.parse(url).entries: + if entry.guid not in old: + ret.append(entry) + return ret diff --git a/state.a.json b/state.a.json new file mode 100644 index 0000000..006be34 --- /dev/null +++ b/state.a.json @@ -0,0 +1 @@ +{"url": "https://cosmic.voyage/rss.xml", "known": ["https://cosmic.voyage/Melchizedek/004.html", "https://cosmic.voyage/Hoffnung/002a.html", "https://cosmic.voyage/Hoffnung/002.html", "https://cosmic.voyage/Garnet%20Star/001.html", "https://cosmic.voyage/Voortrekker/3-listen-im-really-sorry-but.html", "https://cosmic.voyage/Voortrekker/2-were-you-going-to-tell-me.html", "https://cosmic.voyage/Hoffnung/001.html", "https://cosmic.voyage/Franciscus/navlog001.html", "https://cosmic.voyage/Voortrekker/1-made-it.html", "https://cosmic.voyage/Melchizedek/003.html", "https://cosmic.voyage/Franciscus/log001.html", "https://cosmic.voyage/Garnet%20Star/000.html", "https://cosmic.voyage/Excelsior/001.html", "https://cosmic.voyage/Melchizedek/002.html", "https://cosmic.voyage/Starbloom/001.html", "https://cosmic.voyage/adrestia/hello.html", "https://cosmic.voyage/Melchizedek/001.html"], "channel": "#cosmic"} \ No newline at end of file diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..8d62f10 --- /dev/null +++ b/tasks.py @@ -0,0 +1,50 @@ +import sched,time,string,json +from threading import Thread +from sys import exit +class TaskPool: + def __init__(self,**kwargs): + self.base_state = kwargs + self.base_state["task_pool"] = self + self.scheduler = sched.scheduler(time.time,time.sleep) + self.thread = Thread(target=self.worker,args=(self,)) + self.coroutines = [] + self.states = {} + self.killswitch = False + + def periodical(self,scheduler,interval,action,index,state=dict()): + if self.killswitch: + return + + self.states[index] = action(state,self.base_state) + if not self.killswitch: + scheduler.enter(interval,1,self.periodical,(scheduler,interval,action,index,self.states[index])) + + def worker(self,tasks): + for c,coro in enumerate(tasks.coroutines): + interval = coro["interval"] + action = coro["action"] + state = coro.get("state",dict()) + tasks.periodical(tasks.scheduler,interval,action,c,state) + tasks.scheduler.run() + exit(0) + + def run(self): + self.thread.daemon = True + self.thread.start() + + def stop(self): + list(map(self.scheduler.cancel, self.scheduler.queue)) + self.killswitch = True # kill any lingering tasks + + def add_coroutine(self,action,interval,state=dict(),name=None): + if name is None: + name = string.ascii_letters[len(self.coroutines)] + self.coroutines.append(dict(action=action,interval=interval,state=state,name=name)) + + def save_state(self, index): + with open("state.{}.json".format(self.coroutines[index]["name"]),"w") as f: + json.dump(self.states[index],f) + + def load_state(self, index): + with open("state.{}.json".format(self.coroutines[index]["name"])) as f: + self.states[index] = json.load(f)