import argparse import json import sys import requests import config def list_projects(api_key, project_name): """Lists all projects to find specific project by name to add contributors to.""" data = { "api_token": api_key, } response = requests.post(url=config.POEDITOR_API_URL + "projects/list", data=data) response = json.loads(response.text) projects = response["result"]["projects"] for project in projects: if project_name in project["name"]: project_id = project["id"] return project_id def add_contributor(api_key, project_id, username, email, language, admin=False): """Adds contributor or administrators to a project""" if admin: data = { "api_token": api_key, "id": project_id, "name": username, "email": email, "admin": 1, } else: data = { "api_token": api_key, "id": project_id, "name": username, "email": email, "language": language, } print("Adding " + username + " to contribute to project...") response = requests.post( url=config.POEDITOR_API_URL + "contributors/add", data=data ) result = json.loads(response.text) print(result["response"]["message"]) def main(): """Main entrypoint""" parser = argparse.ArgumentParser() parser.add_argument( "-a", "--api-key", required=True, help="Specify poeditor api key to create projects", ) parser.add_argument( "-p", "--project-name", required=True, help="Name of poeditor project" ) parser.add_argument( "-u", "--user-name", required=True, help="First and last name of contributor/admin", ) parser.add_argument( "-e", "--email", required=True, help="Contributor/Admin email address" ) parser.add_argument( "-l", "--language", required=False, help="Required if adding a contributor only. Adds a language for a contributor.", ) parser.add_argument( "-x", "--admin", type=bool, required=False, help="Set it to True to add as Admin to the project", ) args = parser.parse_args() try: project_id = list_projects(args.api_key, args.project_name) if project_id is None: raise NameError add_contributor( args.api_key, project_id, args.user_name, args.email, args.language, args.admin, ) except NameError: print("Error: Project not found.") sys.exit(1) if __name__ == "__main__": main()