]> jfr.im git - erebus.git/commitdiff
add reddark streamer, first step
authorJohn Runyon <redacted>
Wed, 14 Jun 2023 10:49:05 +0000 (04:49 -0600)
committerJohn Runyon <redacted>
Wed, 14 Jun 2023 10:49:08 +0000 (04:49 -0600)
not implemented yet: handleState, getText

modules/reddark.py [new file with mode: 0644]

diff --git a/modules/reddark.py b/modules/reddark.py
new file mode 100644 (file)
index 0000000..911dd87
--- /dev/null
@@ -0,0 +1,111 @@
+# Reddark streaming module
+# vim: fileencoding=utf-8
+
+# module info
+modinfo = {
+       'author': 'Multiple',
+       'license': 'unknown',
+       'compatible': [0], # compatible module API versions
+       'depends': [], # other modules required to work properly?
+       'softdeps': ['help'], # modules which are preferred but not required
+}
+# note: softdeps will be loaded before this module, IF not disabled in the configuration (autoload.module = 0) (and if it exists)
+# however, if it is disabled it will be silently ignored, and if it is unloaded at runtime it won't cause this one to unload.
+#
+# basically, softdeps are things this module will use if available, but does not require (no errors will occur if it's not loaded)
+# for example, @lib.help() will attempt to use the help module, but swallow errors if it is not loaded
+
+
+# global variables
+wants_to_stop = False
+runner = None
+
+
+# preamble
+import modlib
+lib = modlib.modlib(__name__)
+def modstart(parent, *args, **kwargs):
+       gotParent(parent)
+       return lib.modstart(parent, *args, **kwargs)
+def modstop(*args, **kwargs):
+       global wants_to_stop, runner
+       if runner:
+               debug("Stopping runner")
+               wants_to_stop = True
+               runner.join()
+               debug("runner stopped")
+               wants_to_stop = False
+       return lib.modstop(*args, **kwargs)
+
+# module code
+from modules.contrib.sseclient import SSEClient
+import threading
+import json
+
+def chan():
+       return lib.parent.channel(lib.parent.cfg.get('reddark', 'channel', default="##.test"))
+
+def bot():
+       return chan().bot
+
+def debug(message, send_to_owner=True):
+       if lib.parent is None:
+               print(message)
+               return
+       if lib.parent.cfg.getboolean('reddark', 'debug', True):
+               lib.parent.log('Reddark', 'R', message)
+       if send_to_owner and lib.parent.cfg.get('debug', 'owner'):
+               bot().msg(lib.parent.cfg.get('debug', 'owner'), message)
+
+def getText(subreddit):
+       pass
+
+def handleDelta(message):
+       message['state'] = message['state'].lower()
+       message['previous_state'] = message['previous_state'].lower()
+       if message['state'] == 'private':
+               message['text'] = getText(message['name'])
+               chan().msg('[%(section)s] %(name)s went %(state)s (was: %(previous_state)s) (https://old.reddit.com/%(name)s) %(text)s' % message)
+       elif message['state'] == 'restricted':
+               chan().msg('[%(section)s] %(name)s went %(state)s (was: %(previous_state)s) (https://old.reddit.com/%(name)s)' % message)
+       else:
+               chan().msg('[%(section)s] %(name)s went \x02%(state)s\x02 (was: %(previous_state)s) (https://old.reddit.com/%(name)s)' % message)
+
+def handleState(message):
+       pass
+
+def gotParent(parent):
+       global runner
+       def loop_messages():
+               global wants_to_stop
+               messages = SSEClient(parent.cfg.get('reddark', 'sse', default="https://reddark.rewby.archivete.am/sse"), timeout=60)
+               debug("Connected to SSE", False)
+               for msg in messages:
+                       if wants_to_stop:
+                               return
+                       if len(msg.data) == 0:
+                               continue
+                       data = json.loads(msg.data)
+                       if data['type'] == 'Delta':
+                               debug(repr(data), False)
+                               handleDelta(data['content'])
+                       elif data['type'] == 'CurrentStateUpdate':
+                               handleState(data['content'])
+                       else:
+                               debug("Unknown event: " + msg, False)
+#{"type":"Delta","content":{"name":"r/TrainCrashSeries","section":"1k+","previous_state":"PRIVATE","state":"RESTRICTED"}}
+#{"type":"CurrentStateUpdate","content":{"sections":["40+ million","30+ million","20+ million","10+ million","5+ million","1+ million","500k+","250k+","100k+","50k+","5k+","1k+","1k and below"],"subreddits":[{"name":"r/032r4r","section":"1k+","state":"PRIVATE"},{"name":"r/0sanitymemes","section":"5k+","state":"PRIVATE"},{"name":"r/1022","section":"5k+","state":"PRIVATE"},{"name":"r/11foot8","section":"100k+","state":"PRIVATE"},{"name":"r/1200isjerky","section":"50k+","state":"PRIVATE"},{"name":"r
+
+       runner = threading.Thread(target=loop_messages)
+       runner.daemon = True
+       runner.start()
+
+@lib.hook(needchan=False, glevel=50)
+@lib.help(None, 'sets reddark topic suffix')
+def topicsuffix(bot, user, chan, realtarget, *args):
+       if chan is not None: replyto = chan
+       else: replyto = user
+
+       lib.parent.cfg.set('reddark', 'topicsuffix', ' '.join(args))
+
+       bot.msg(replyto, "Updated topic suffix")