diff --git a/.gitignore b/.gitignore index 392ce65..9848f17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +cli-tool/.env +frontend/package-lock.json media_files/encoded/ media_files/original/ media_files/hls/ @@ -14,4 +16,4 @@ static/mptt/ static/rest_framework/ static/drf-yasg cms/local_settings.py -deploy/docker/local_settings.py +deploy/docker/local_settings.py \ No newline at end of file diff --git a/cli-tool/README.md b/cli-tool/README.md new file mode 100644 index 0000000..87f493b --- /dev/null +++ b/cli-tool/README.md @@ -0,0 +1,10 @@ +## MediaCMS CLI Tool +This is the CLI tool to interact with the API of your installation/instance of MediaCMS. + +### How to configure and use the tools +- Make sure that you have all the required installations (`cli-tool/requirements.txt`)installed. To install it - + - Create a new virtualenv using any python virtualenv manager. + - Then activate the virtualenv and enter `pip install -r requirements.txt`. +- Create an .env file in this folder (`mediacms/cli-tool/`) +- Run the cli tool using the command `python cli.py login`. This will authenticate you and store necessary creds for further authentications. +- To check the credentials and necessary setup, run `python cli.py whoami`. This will show your details. \ No newline at end of file diff --git a/cli-tool/cli.py b/cli-tool/cli.py new file mode 100644 index 0000000..f3abef3 --- /dev/null +++ b/cli-tool/cli.py @@ -0,0 +1,167 @@ +import json +import os + +import click +import requests +from decouple import config +from rich import print +from rich.console import Console +from rich.table import Table + +console = Console() + +print("Welcome to the CLI Tool of [bold blue]MediaCMS![/bold blue]", ":thumbs_up:") + + +BASE_URL = 'https://demo.mediacms.io/api/v1' +AUTH_KEY = '' +USERNAME = '' +EMAIL = '' + + +def set_envs(): + with open('.env', 'r') as file: + if not file.read(1): + print("Use the Login command to set your credential environment variables") + else: + global AUTH_KEY, USERNAME, EMAIL + AUTH_KEY = config('AUTH_KEY') + USERNAME = config('USERNAME') + EMAIL = config('EMAIL') + + +set_envs() + + +@click.group() +def apis(): + """A CLI wrapper for the MediaCMS API endpoints.""" + + +@apis.command() +def login(): + """Login to your account.""" + + email = input('Enter your email address: ') + password = input('Enter your password: ') + + data = { + "email": f"{email}", + "password": f"{password}", + } + + response = requests.post(url=f'{BASE_URL}/login', data=data) + if response.status_code == 200: + username = json.loads(response.text)["username"] + with open(".env", "w") as file: + file.writelines(f'AUTH_KEY={json.loads(response.text)["token"]}\n') + file.writelines(f'EMAIL={json.loads(response.text)["email"]}\n') + file.writelines(f'USERNAME={json.loads(response.text)["username"]}\n') + print(f"Welcome to MediaCMS [bold blue]{username}[/bold blue]. Your auth creds have been suceesfully stored in the .env file", ":v:") + else: + print(f'Error: {"non_field_errors":["User not found."]}') + + +@apis.command() +def upload_media(): + """Upload media to the server""" + + headers = {'authorization': f'Token {AUTH_KEY}'} + + path = input('Enter the location of the file or directory where multiple files are present: ') + + if os.path.isdir(path): + for filename in os.listdir(path): + files = {} + abs = os.path.abspath("{path}/{filename}") + files['media_file'] = open(f'{abs}', 'rb') + response = requests.post(url=f'{BASE_URL}/media', headers=headers, files=files) + if response.status_code == 201: + print("[bold blue]{filename}[/bold blue] successfully uploaded!") + else: + print(f'Error: {response.text}') + + else: + files = {} + files['media_file'] = open(f'{os.path.abspath(path)}', 'rb') + response = requests.post(url=f'{BASE_URL}/media', headers=headers, files=files) + if response.status_code == 201: + print("[bold blue]{filename}[/bold blue] successfully uploaded!") + else: + print(f'Error: {response.text}') + + +@apis.command() +def my_media(): + """List all my media""" + + headers = {'authorization': f'Token {AUTH_KEY}'} + response = requests.get(url=f'{BASE_URL}/media?author={USERNAME}', headers=headers) + + if response.status_code == 200: + data_json = json.loads(response.text) + + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Name of the media") + table.add_column("Media Type") + table.add_column("State") + + for data in data_json['results']: + table.add_row(data['title'], data['media_type'], data['state']) + console.print(table) + + else: + print(f'Could not get the media: {response.text}') + + +@apis.command() +def whoami(): + """Shows the details of the authorized user""" + headers = {'authorization': f'Token {AUTH_KEY}'} + response = requests.get(url=f'{BASE_URL}/whoami', headers=headers) + for data, value in json.loads(response.text).items(): + print(data, ' : ', value) + + +@apis.command() +def categories(): + """List all categories.""" + response = requests.get(url=f'{BASE_URL}/categories') + if response.status_code == 200: + data_json = json.loads(response.text) + + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Category") + table.add_column("Description") + + for data in data_json: + table.add_row(data['title'], data['description']) + + console.print(table) + else: + print(f'Could not get the categories: {response.text}') + + +@apis.command() +def encodings(): + """List all encoding profiles""" + response = requests.get(url=f'{BASE_URL}/encode_profiles/') + if response.status_code == 200: + data_json = json.loads(response.text) + + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Name") + table.add_column("Extension") + table.add_column("Resolution") + table.add_column("Codec") + table.add_column("Description") + + for data in data_json: + table.add_row(data['name'], data['extension'], str(data['resolution']), data['codec'], data['description']) + console.print(table) + else: + print(f'Could not get the encodings: {response.text}') + + +if __name__ == '__main__': + apis() diff --git a/cli-tool/requirements.txt b/cli-tool/requirements.txt new file mode 100644 index 0000000..9ad9be5 --- /dev/null +++ b/cli-tool/requirements.txt @@ -0,0 +1,4 @@ +click +python-decouple +requests +rich \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt index dfde72c..d4768e1 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -13,4 +13,4 @@ pytest-django pytest-factoryboy Faker selenium -webdriver-manager \ No newline at end of file +webdriver-manager