Initial commit

This commit is contained in:
Robert Miles 2018-12-01 20:18:39 -05:00
commit 0071f83362
7 changed files with 198 additions and 0 deletions

98
.gitignore vendored Normal file
View File

@ -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

6
LICENSE Normal file
View File

@ -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.

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# cosmicbot
Handles some cosmic.voyage stuff

32
bot.py Normal file
View File

@ -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()

8
rss.py Normal file
View File

@ -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

1
state.a.json Normal file
View File

@ -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"}

50
tasks.py Normal file
View File

@ -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)