Files
NijiHolo_EN_ID_Bot/src/main.py
T

92 lines
2.9 KiB
Python
Raw Normal View History

2022-09-24 17:56:58 -07:00
import sys
import asyncio
2022-09-24 17:56:58 -07:00
import argparse
from argparse import RawTextHelpFormatter
2022-09-27 15:09:09 -07:00
import code
2022-09-24 17:56:58 -07:00
import nest_asyncio
2022-09-24 17:56:58 -07:00
import talent_lists
import api_secrets
import catchup
import listen
2022-09-25 18:31:50 -07:00
from twapi import TwAPI
2022-09-24 17:56:58 -07:00
2022-09-27 02:49:03 -07:00
PROGRAM_ARGS = None
2022-09-24 17:56:58 -07:00
MODES_HELP_STR = '''mode to run the bot at:
2022-09-27 15:09:09 -07:00
l,listen: listen for new tweets from all accounts; will not terminate unless error occurs
c,catchup: scan all tweets from all accounts; will terminate when done
d,delete-all: delete all tweets on account provided by secrets.ini; make sure the function is uncommented in twapi.py'''
2022-09-24 17:56:58 -07:00
def init_argparse():
p = argparse.ArgumentParser(description='Twitter bot that follows interactions between Nijisanji EN/ID and hololive EN/ID members.', formatter_class=RawTextHelpFormatter)
p.add_argument('mode', nargs='?', \
help=MODES_HELP_STR)
p.add_argument('--show-tokens', action='store_true', help='[DO NOT USE IN PUBLIC SETTING] print stored tokens from secrets.ini')
2022-09-27 02:49:03 -07:00
p.add_argument('--announce-catchup', action='store_true', help='In catch-up mode, post a tweet announcing catch-up mode.')
2022-09-27 15:09:09 -07:00
p.add_argument('--no-delay', action='store_true', help='In self-destruct mode, clear tweets without safety waiting.')
2022-09-24 17:56:58 -07:00
return p
def command_line():
2022-09-27 02:49:03 -07:00
# TODO: implement command line mode for manually controlling the bot
2022-09-27 15:09:09 -07:00
print('Shell coming soon. For now, here\'s a Python interpretor.')
code.interact(local=globals())
2022-09-24 17:56:58 -07:00
pass
2022-09-27 15:09:09 -07:00
async def self_destruct():
if not PROGRAM_ARGS.no_delay:
print('\033[31;6m-----DELETING ALL TWEETS IN 10 SECONDS!! PRESS CTRL+C TO CANCEL.-----\033[0m')
await asyncio.sleep(10)
await TwAPI.instance.nuke_tweets()
2022-09-27 02:49:03 -07:00
async def async_main():
global PROGRAM_ARGS
## Determine running mode
match PROGRAM_ARGS.mode.lower():
case 'l' | 'listen':
print('RUNNING IN LISTEN MODE\n')
2022-09-27 17:40:48 -07:00
await listen.run()
2022-09-27 02:49:03 -07:00
case 'c' | 'catchup':
print('RUNNING IN CATCH-UP MODE\n')
await catchup.run(PROGRAM_ARGS)
2022-09-27 15:09:09 -07:00
case 'd' | 'delete-all':
print('WARNING: SELF-DESTRUCT MODE')
await self_destruct()
case 'cmd':
2022-09-27 02:49:03 -07:00
command_line()
2022-09-27 15:09:09 -07:00
case _:
2022-09-27 02:49:03 -07:00
print('\ninvalid mode. run with no arguments or "-h" for help page, including mode list.')
return
def main():
global PROGRAM_ARGS
2022-09-24 17:56:58 -07:00
parser = init_argparse()
if len(sys.argv) < 2:
parser.print_help()
return
2022-09-27 02:49:03 -07:00
PROGRAM_ARGS = parser.parse_args()
2022-09-24 17:56:58 -07:00
2022-09-27 02:49:03 -07:00
if PROGRAM_ARGS.show_tokens:
2022-09-24 17:56:58 -07:00
print(api_secrets.get_all_secrets())
2022-09-27 02:49:03 -07:00
if PROGRAM_ARGS.mode is None: return
2022-09-24 17:56:58 -07:00
## We expect to run in some mode now.
# Initialize shared API instance
twApi = TwAPI.instance = TwAPI()
# Initialize talent account lists
talent_lists.init()
2022-09-27 02:49:03 -07:00
## Asynchronous execution
nest_asyncio.apply()
asyncio.run(async_main())
2022-09-24 17:56:58 -07:00
if __name__ == "__main__":
2022-09-27 02:49:03 -07:00
main()