2022-11-25 14:03:24 -05:00
|
|
|
'''
|
2022-11-27 09:09:18 -05:00
|
|
|
Command line module for calling tootapalooza to do its work
|
2022-11-25 14:03:24 -05:00
|
|
|
'''
|
|
|
|
|
2022-11-27 09:09:18 -05:00
|
|
|
__all__ = ['tootapalooza']
|
|
|
|
__author__ = 'Paco Hope <tootapalooza@filter.paco.to>'
|
2022-11-25 14:03:24 -05:00
|
|
|
__date__ = '25 November 2022'
|
|
|
|
__version__ = '1.0'
|
|
|
|
__copyright__ = 'Copyright © 2022 Paco Hope. See LICENSE for details.'
|
|
|
|
|
|
|
|
from mastodon import Mastodon
|
2022-11-25 19:11:10 -05:00
|
|
|
import toml
|
2022-11-25 14:03:24 -05:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
import sys
|
2022-11-26 00:09:23 -05:00
|
|
|
from pathlib import Path
|
|
|
|
import random
|
2022-11-27 11:29:39 -05:00
|
|
|
import re
|
|
|
|
|
|
|
|
args=None
|
2022-11-25 14:03:24 -05:00
|
|
|
|
2022-11-25 19:11:10 -05:00
|
|
|
class Tooter(Mastodon):
|
|
|
|
credentials: dict = {}
|
2022-11-27 11:29:39 -05:00
|
|
|
hostname : str = ''
|
|
|
|
files : dict = {}
|
|
|
|
client_id : str = '.tootapalooza.env'
|
2022-11-25 14:03:24 -05:00
|
|
|
|
2022-11-25 19:11:10 -05:00
|
|
|
def __init__(self, name: str):
|
2022-11-27 11:29:39 -05:00
|
|
|
cred_dict = self.credentials[name]
|
|
|
|
self.name = name
|
|
|
|
self.username = cred_dict['addr']
|
|
|
|
self.password = cred_dict['pass']
|
|
|
|
self.displayname = cred_dict['name']
|
|
|
|
self.cred_file = f'.tootapalooza-usercred-{self.name}.env'
|
2022-11-25 14:03:24 -05:00
|
|
|
|
2022-11-27 11:29:39 -05:00
|
|
|
super().__init__(client_id=self.client_id)
|
2022-11-25 14:03:24 -05:00
|
|
|
|
2022-11-25 16:06:03 -05:00
|
|
|
self.log_in(
|
|
|
|
self.username,
|
2022-11-25 19:11:10 -05:00
|
|
|
self.password,
|
|
|
|
to_file=self.cred_file
|
2022-11-25 16:06:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2022-11-25 19:11:10 -05:00
|
|
|
def load_credentials(cls, file: str) -> None:
|
|
|
|
as_dict = toml.load(file)
|
|
|
|
for username, fields in as_dict.items():
|
|
|
|
if not isinstance(fields, dict):
|
|
|
|
raise TypeError(f'{username} has no key/value pairs')
|
|
|
|
if 'addr' not in fields:
|
|
|
|
raise KeyError(f'`addr` field missing from {username}')
|
|
|
|
if 'pass' not in fields:
|
|
|
|
raise KeyError(f'`pass` field missing from {username}')
|
|
|
|
cls.credentials = as_dict
|
|
|
|
|
2022-11-26 00:09:23 -05:00
|
|
|
@classmethod
|
|
|
|
def load_src_files(cls, dir: Path) -> None:
|
|
|
|
for item in dir.iterdir():
|
|
|
|
if not item.is_file():
|
|
|
|
continue
|
|
|
|
with item.open('r') as f:
|
|
|
|
cls.files[f.name] = f.readlines()
|
|
|
|
|
2022-11-25 19:11:10 -05:00
|
|
|
def daemon_main(tooter: Tooter):
|
2022-11-25 14:03:24 -05:00
|
|
|
"""Run from a command line."""
|
|
|
|
|
2022-11-25 16:06:03 -05:00
|
|
|
while True:
|
2022-11-25 14:03:24 -05:00
|
|
|
# do a thing
|
|
|
|
time.sleep(600)
|
|
|
|
|
2022-11-25 19:11:10 -05:00
|
|
|
def once(tooter: Tooter):
|
2022-11-25 14:03:24 -05:00
|
|
|
"""Run from a command line."""
|
2022-11-25 19:58:19 -05:00
|
|
|
# message = check_public_timeline(tooter)
|
|
|
|
message = f'{tooter.name} says hi!'
|
2022-11-27 11:29:39 -05:00
|
|
|
sourcefile = random.choice(list(Tooter.files.values()))
|
|
|
|
startline = random.randint(0,len(sourcefile))
|
|
|
|
sourceline = ''
|
|
|
|
i=0
|
|
|
|
while(len(sourceline) < 400 and startline+i < len(sourcefile)):
|
|
|
|
sourceline=sourceline+sourcefile[startline+i]
|
|
|
|
i+=1
|
|
|
|
|
|
|
|
tootline = re.search( '((\s|\S){,400})\.', sourceline )
|
|
|
|
if( tootline ):
|
|
|
|
message=tootline.group(0).strip()
|
|
|
|
else:
|
|
|
|
message=sourceline.strip()
|
|
|
|
|
|
|
|
if( args.dry_run ):
|
|
|
|
print(f"toot message: \"{message}\"")
|
|
|
|
else:
|
|
|
|
tooter.toot(message)
|
2022-11-25 14:03:24 -05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
def main():
|
2022-11-27 11:29:39 -05:00
|
|
|
global args
|
2022-11-25 14:03:24 -05:00
|
|
|
parser = argparse.ArgumentParser(
|
2022-11-25 19:33:45 -05:00
|
|
|
description='Randomly interact with a Mastodon timeline.')
|
2022-11-25 14:03:24 -05:00
|
|
|
parser.add_argument( '-d', '--debug', action='store_true',
|
|
|
|
help='Enable debugging messages.')
|
|
|
|
parser.add_argument( '-o', '--once', action='store_true',
|
|
|
|
help='Run once and exit. Default is to run as a daemon.')
|
2022-11-27 11:29:39 -05:00
|
|
|
parser.add_argument( '-n', '--dry-run', action='store_true',
|
|
|
|
help='Don\'t toot. Just show what would be done.')
|
2022-11-26 00:09:23 -05:00
|
|
|
parser.add_argument( '-D', '--directory', default='text',
|
2022-11-27 11:29:39 -05:00
|
|
|
help='Directory with text source files for toot bodies.')
|
2022-11-26 00:09:23 -05:00
|
|
|
parser.add_argument( 'file', type=argparse.FileType('r'),
|
2022-11-27 11:29:39 -05:00
|
|
|
help='TOML file with user credentials (see server-util/README.md).')
|
2022-11-25 14:03:24 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-11-26 00:09:23 -05:00
|
|
|
p = Path(args.directory)
|
|
|
|
if not p.exists():
|
|
|
|
print(f'{sys.argv[0]}: {args.directory}: No such file or directory', file=sys.stderr)
|
|
|
|
return 2
|
|
|
|
if not p.is_dir():
|
|
|
|
print(f'{sys.argv[0]}: {args.directory}: Is not a directory', file=sys.stderr)
|
|
|
|
return 2
|
|
|
|
|
|
|
|
Tooter.load_src_files(p)
|
|
|
|
|
2022-11-25 19:33:45 -05:00
|
|
|
Tooter.load_credentials(args.file)
|
2022-11-25 16:06:03 -05:00
|
|
|
|
|
|
|
if args.once:
|
2022-11-25 19:58:19 -05:00
|
|
|
for name in Tooter.credentials:
|
|
|
|
t = Tooter(name)
|
|
|
|
once(t)
|
2022-11-25 19:11:10 -05:00
|
|
|
return 0
|
2022-11-25 16:06:03 -05:00
|
|
|
|
2022-11-25 19:11:10 -05:00
|
|
|
daemon_main(t)
|
2022-11-25 14:03:24 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-11-25 16:06:03 -05:00
|
|
|
sys.exit(main())
|