]> jfr.im git - erebus.git/commitdiff
reddark - more resilience
authorJohn Runyon <redacted>
Thu, 15 Jun 2023 01:09:51 +0000 (19:09 -0600)
committerJohn Runyon <redacted>
Thu, 15 Jun 2023 01:09:51 +0000 (19:09 -0600)
modules/reddark.py

index ad0a30fcba0836ba85dcc8967ac9f8bfa6f42767..fbba1e13bd9aca761ee0aeb8834846c4848a68d6 100644 (file)
@@ -74,7 +74,7 @@ def getText(subreddit):
                return
        soup = BeautifulSoup(r.text, 'html.parser')
        elements = soup.find_all(class_='interstitial-subreddit-description')
-       if elements[0]:
+       if len(elements):
                text = elements[0].get_text()
                text = re.sub(re.escape(subreddit), '', text, re.IGNORECASE).replace("\n", " ")
                return text
@@ -124,19 +124,21 @@ def gotParent(parent):
                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':
+                       try:
+                               if wants_to_stop:
+                                       return
+                               if len(msg.data) == 0:
+                                       continue
+                               data = json.loads(msg.data)
                                debug(repr(data)[0:500], False)
-                               handleState(data['content'])
-                       else:
-                               debug("Unknown event: " + msg, False)
+                               if data['type'] == 'Delta':
+                                       handleDelta(data['content'])
+                               elif data['type'] == 'CurrentStateUpdate':
+                                       handleState(data['content'])
+                               else:
+                                       debug("Unknown event: " + msg, False)
+                       except Exception as e:
+                               debug("Failed to parse an event: " + e)
 #{"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