2023-03-12 23:14:03 +02:00
import asyncio
from sys import exit
from traceback import print_exc
from modules . api_client import create_album , create_user , http_session
from argparse import ArgumentParser
from modules . utils import configSet
parser = ArgumentParser (
prog = " Telegram Poster " ,
description = " Bot for posting some of your stuff and also receiving submissions. " ,
)
parser . add_argument ( " --create-user " , action = " store_true " )
parser . add_argument ( " --create-album " , action = " store_true " )
args = parser . parse_args ( )
async def cli_create_user ( ) - > None :
print (
" To set up Photos API connection you need to create a new user. \n If you have email confirmation enabled in your Photos API config - you need to use a real email that will get a confirmation code afterwards. " ,
flush = True ,
)
username = input ( " Choose username for new Photos API user: " ) . strip ( )
email = input ( f " Choose email for user ' { username } ' : " ) . strip ( )
password = input ( f " Choose password for user ' { username } ' : " ) . strip ( )
try :
2023-03-12 23:58:55 +02:00
result_1 = await create_user ( username , email , password )
2023-03-12 23:14:03 +02:00
# asyncio.run(create_user(username, email, password))
configSet ( " username " , username , " posting " , " api " )
configSet ( " password " , password , " posting " , " api " )
none = input (
" Alright. If you have email confirmation enabled - please confirm registration by using the link in your email. After that press Enter. Otherwise just press Enter. "
)
except Exception as exp :
print ( f " Could not create a user due to { exp } " , flush = True )
print_exc ( )
exit ( )
if not args . create_album :
print ( " You ' re done! " , flush = True )
await http_session . close ( )
exit ( )
2023-03-12 23:58:55 +02:00
return None
2023-03-12 23:14:03 +02:00
async def cli_create_album ( ) - > None :
print (
" To use Photos API your user needs to have an album to store its data. \n This wizard will help you to create a new album with its name and title. " ,
flush = True ,
)
name = input ( " Choose a name for your album: " ) . strip ( )
title = input ( f " Choose a title for album ' { name } ' : " ) . strip ( )
try :
2023-03-12 23:58:55 +02:00
result_2 = await create_album ( name , title )
2023-03-12 23:14:03 +02:00
# asyncio.run(create_album(name, title))
configSet ( " album " , name , " posting " , " api " )
except Exception as exp :
print ( f " Could not create an album due to { exp } " , flush = True )
print_exc ( )
exit ( )
print ( " You ' re done! " , flush = True )
await http_session . close ( )
exit ( )
2023-03-12 23:58:55 +02:00
return None
2023-03-12 23:14:03 +02:00
if args . create_user or args . create_album :
loop = asyncio . get_event_loop ( )
tasks = [ ]
if args . create_user :
2023-03-12 23:58:55 +02:00
loop . run_until_complete ( asyncio . wait ( [ loop . create_task ( cli_create_user ( ) ) ] ) )
2023-03-12 23:14:03 +02:00
if args . create_album :
2023-03-12 23:58:55 +02:00
loop . run_until_complete ( asyncio . wait ( [ loop . create_task ( cli_create_album ( ) ) ] ) )
2023-03-12 23:14:03 +02:00
loop . close ( )