Browse Source

Update welcome bot to support more general code

merge-requests/3/head
KemoNine 5 years ago
parent
commit
1aa7ff2d73
  1. 48
      botstreamlisteners.py
  2. 3
      configs/example_welcome_config.yaml
  3. 2
      toot_bot.py

48
botstreamlisteners.py

@ -23,7 +23,7 @@ class PublicTimelineBot(StreamListener):
StreamListener.__init__(self)
self.bot_config = bot_config
# Get access to cache
self.conn = sqlite3.connect(self.bot_config['welcome']['cache_file'])
self.conn = sqlite3.connect(self.bot_config['stream']['cache_file'])
self.cursor = self.conn.cursor()
self.cursor.execute("""\
@ -187,3 +187,49 @@ CREATE TABLE IF NOT EXISTS welcome_cache (
self.cursor.execute('insert into welcome_cache values (?, ?)',
(username, timestamp))
self.conn.commit()
class FollowBot(PublicTimelineBot):
"""Public timeline bot that follows newly seen users"""
def setup_cache(self):
"""Setup the local cache"""
# Ensure cache table has been created
self.cursor.execute("""\
CREATE TABLE IF NOT EXISTS follow_cache (
username VARCHAR(2048) PRIMARY KEY,
seen_timestamp TIMESTAMP
)
""")
self.conn.commit()
def process_toot(self, status):
"""Method that follows unseen users."""
federated = '@' in status['account']['acct']
username = status['account']['acct']
timestamp = status['created_at']
visibility = status['visibility']
# Welcome any user who's posted publicly
if visibility == 'public' and federated: #TODO: Incorporate black lists here
# Check if username has been seen for welcome
self.cursor.execute(
'select count(1) as found from follow_cache where username = ?',
(username, ))
if self.cursor.fetchone()[0] > 0:
return
mastodon = Mastodon(
client_id=self.bot_config['config']['client_cred_file'],
access_token=self.bot_config['config']['user_cred_file'],
api_base_url=self.bot_config['config']['api_base_url']
)
mastodon.account_follow(status['account']['id'])
# Cache user to avoid duping welcome messages
self.cursor.execute('insert into follow_cache values (?, ?)',
(username, timestamp))
self.conn.commit()

3
configs/example_welcome_config.yaml

@ -7,8 +7,9 @@ config:
user_cred_file: /home/mastodon/bot/user_cred.secret
# Setup the cache file used for ensuring users aren't welcomed more than once
welcome:
stream:
cache_file: /home/mastodon/bot/welcome_cache.db
type: welcome # One of welcome, follow
# Setup the toots that will be sent
# These static toots will ALWAYS be sent

2
toot_bot.py

@ -235,7 +235,7 @@ def handle_command_line():
# Deal with welcome command
if args.command == 'welcome':
# Verify toot cache file folder exists
cache_file = pathlib.Path(config['welcome']['cache_file'])
cache_file = pathlib.Path(config['stream']['cache_file'])
if not cache_file.parent.exists(): # pylint: disable=no-member
# Warn if welcome cache file doesn't exist
sys.exit('welcome cache_file directory does not exist')

Loading…
Cancel
Save