2022-09-24 17:56:58 -07:00
import sys
2022-09-25 03:39:15 -07:00
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
2022-09-25 03:39:15 -07:00
import nest_asyncio
2022-09-24 17:56:58 -07:00
import talent_lists
2022-10-02 04:57:24 -07:00
import ttweetqueue as ttq
2022-09-24 17:56:58 -07:00
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 )
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 22:12:36 -07:00
# TODO (extra): 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
2023-05-16 18:57:07 -07:00
if PROGRAM_ARGS . mode == None :
await catchup . run ( )
return
2022-09-29 07:44:21 +01:00
mode = PROGRAM_ARGS . mode . lower ( )
2023-05-16 18:57:07 -07:00
if mode in [ ' d ' , ' delete-all ' ] :
2022-09-29 07:44:21 +01:00
print ( ' WARNING: SELF-DESTRUCT MODE ' )
await self_destruct ( )
elif mode == ' cmd ' :
command_line ( )
2023-08-17 02:28:29 -07:00
elif mode in [ ' l ' , ' listen ' ] :
listen . run ( )
2022-09-29 07:44:21 +01:00
else :
2023-05-16 18:57:07 -07:00
print ( ' \n unknown mode. run with no arguments or -h for help and modes ' )
2022-09-27 02:49:03 -07:00
def main ( ) :
global PROGRAM_ARGS
2022-09-24 17:56:58 -07:00
parser = init_argparse ( )
2023-08-17 02:28:29 -07:00
if len ( sys . argv ) < 2 :
parser . print_help ( )
return
2022-09-24 17:56:58 -07:00
2022-09-27 02:49:03 -07:00
PROGRAM_ARGS = parser . parse_args ( )
2022-09-24 17:56:58 -07:00
## We expect to run in some mode now.
# Initialize shared API instance
2022-10-02 04:57:24 -07:00
TwAPI ( )
2022-09-24 17:56:58 -07:00
# Initialize talent account lists
talent_lists . init ( )
2022-10-02 04:57:24 -07:00
# Initialize queue files system
ttq . TalentTweetQueue ( )
2022-09-27 02:49:03 -07:00
## Asynchronous execution
2023-08-17 02:28:29 -07:00
print ( ' beginning async main ' )
2022-09-27 02:49:03 -07:00
nest_asyncio . apply ( )
asyncio . run ( async_main ( ) )
2022-09-24 17:56:58 -07:00
if __name__ == " __main__ " :
2022-09-29 07:44:21 +01:00
main ( )