TelegramPoster/modules/cli.py

78 lines
2.7 KiB
Python

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.\nIf 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:
result = await create_user(username, email, password)
# 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()
async def cli_create_album() -> None:
print(
"To use Photos API your user needs to have an album to store its data.\nThis 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:
result = await create_album(name, title)
# 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()
if args.create_user or args.create_album:
loop = asyncio.get_event_loop()
tasks = []
if args.create_user:
tasks.append(loop.create_task(cli_create_user()))
if args.create_album:
tasks.append(loop.create_task(cli_create_album()))
loop.run_until_complete(asyncio.wait(tasks))
loop.close()