kopia lustrzana https://github.com/saubury/mastodon-stream
Add watchdog code
rodzic
7b6254dc0b
commit
d466189a8e
|
@ -3,6 +3,8 @@ from mastodon import Mastodon
|
|||
from bs4 import BeautifulSoup
|
||||
import argparse
|
||||
import datetime
|
||||
from threading import Timer
|
||||
import os
|
||||
|
||||
from kafkaproducer import kafka_producer
|
||||
|
||||
|
@ -10,15 +12,22 @@ from kafkaproducer import kafka_producer
|
|||
base_url = ''
|
||||
enable_kafka = False
|
||||
quiet = False
|
||||
watchdog = False
|
||||
|
||||
# if enable_kafka:
|
||||
topic_name, producer = kafka_producer()
|
||||
if enable_kafka:
|
||||
topic_name, producer = kafka_producer()
|
||||
else:
|
||||
topic_name, producer = '' , ''
|
||||
|
||||
#### Listener for Mastodon events
|
||||
|
||||
# Listener for Mastodon events
|
||||
class Listener(mastodon.StreamListener):
|
||||
|
||||
def on_update(self, status):
|
||||
if watchdog:
|
||||
# reset watchdog timer
|
||||
watchdog.reset()
|
||||
|
||||
m_text = BeautifulSoup(status.content, 'html.parser').text
|
||||
num_tags = len(status.tags)
|
||||
num_chars = len(m_text)
|
||||
|
@ -52,14 +61,43 @@ class Listener(mastodon.StreamListener):
|
|||
if not quiet:
|
||||
print(f'{m_user} {m_lang}', m_text[:30])
|
||||
|
||||
|
||||
if enable_kafka:
|
||||
producer.produce(topic = topic_name, value = value_dict)
|
||||
producer.flush()
|
||||
|
||||
|
||||
class Watchdog:
|
||||
def __init__(self, timeout, userHandler=None): # timeout in seconds
|
||||
self.timeout = timeout
|
||||
if userHandler != None:
|
||||
self.timer = Timer(self.timeout, userHandler)
|
||||
else:
|
||||
self.timer = Timer(self.timeout, self.handler)
|
||||
|
||||
def reset(self):
|
||||
self.timer.cancel()
|
||||
self.timer = Timer(self.timeout, watchExpired)
|
||||
self.timer.start()
|
||||
|
||||
def stop(self):
|
||||
self.timer.cancel()
|
||||
|
||||
def handler(self):
|
||||
raise self;
|
||||
|
||||
|
||||
def watchExpired():
|
||||
print('Watchdog expired')
|
||||
# ugly, but expected method for a child process to terminate a fork
|
||||
os._exit(1)
|
||||
|
||||
|
||||
def main():
|
||||
global base_url
|
||||
global enable_kafka
|
||||
global quiet
|
||||
global watchdog
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
|
@ -78,6 +116,12 @@ def main():
|
|||
required=False,
|
||||
default=False)
|
||||
|
||||
parser.add_argument(
|
||||
'--watchdog',
|
||||
help='enable watchdog timer of n seconds',
|
||||
type=int,
|
||||
required=False)
|
||||
|
||||
parser.add_argument(
|
||||
'--quiet',
|
||||
help='Do not echo a summary of the toot',
|
||||
|
@ -97,6 +141,10 @@ def main():
|
|||
enable_kafka=args.enableKafka
|
||||
mastodon = Mastodon(api_base_url = base_url)
|
||||
|
||||
if args.watchdog:
|
||||
watchdog = Watchdog(args.watchdog, watchExpired)
|
||||
watchdog.timer.start()
|
||||
|
||||
if args.public:
|
||||
mastodon.stream_public(Listener())
|
||||
else:
|
||||
|
|
Ładowanie…
Reference in New Issue