diff --git a/.gitignore b/.gitignore index 2409001..0fd0f1d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .DS_Store /env private_token.txt -/code +/resources /account -/profiles \ No newline at end of file +/profiles +__pycache__/ \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..90fa4e9 --- /dev/null +++ b/config.py @@ -0,0 +1,6 @@ +import os.path + +PATH_ACTIVE_ACCOUNTS = "account/active/" +PATH_WAIT_ACCOUNTS = "account/wait/" +PATH_CODE_STORAGE = "resources/" +PATH_CODE_STORAGE_RANDOM = os.path.join(PATH_CODE_STORAGE, "random_code/") diff --git a/main.py b/main.py new file mode 100644 index 0000000..0b300ce --- /dev/null +++ b/main.py @@ -0,0 +1,167 @@ +import json +import os +import random +from typing import Any, Dict + +import colorama +import github +from github import BadCredentialsException +from github import GithubException +from github import UnknownObjectException + +import config, utils + +colorama.init(autoreset=True) + + +def get_list_files_accounts(): + return list( + map(lambda x: config.PATH_ACTIVE_ACCOUNTS + x, [file for file in os.listdir(config.PATH_ACTIVE_ACCOUNTS)])) + + +def parse_json_accounts(files) -> list[Any]: + list_accounts = list() + for file in files: + with open(file, 'r') as file: + data = json.load(file) + list_accounts.append(data) + return list_accounts + + +def print_info_account(token): + acc = github.Github(token) + user = acc.get_user() + print(f"acc - {user.login}") + print(f"name - {user.name}\n") + + +def get_repo_name_from_link(link): + parts = link.split('/') + username = parts[-2] + repo_name = parts[-1] + return f"{username}/{repo_name}" + + +def get_code_from_file(path_to_file): + with open(path_to_file, "r") as file_name: + code = file_name.read() + return code + + +def connect_to_account(token): + git = github.Github(token) + try: + acc = git.get_user() + print(acc.login) + return git, acc + except BadCredentialsException: + utils.print_error_connecting_with_token(token) + return None, None + + +def connect_to_repo(acc, repo_name): + try: + repo = acc.get_repo(repo_name) + return repo + except UnknownObjectException: + return None + + +def connect_to_file(repo, file_path): + try: + return repo.get_contents(file_path) + except (UnknownObjectException, GithubException): + return None + + +def push_in_repo(repo, file_to_push, code_to_push): + content_file = connect_to_file(repo, file_to_push) + if content_file: + text_for_push = content_file.decoded_content.decode('utf-8') + code_to_push + commit_for_push = f"update file {file_to_push}" + file_sha = content_file.sha + repo.update_file(path=file_to_push, message=commit_for_push, content=text_for_push, sha=file_sha) + utils.print_successful_file_push_message(file_to_push) + else: + text_for_push = code_to_push + commit_for_push = f"create file {file_to_push}" + repo.create_file(path=file_to_push, message=commit_for_push, content=text_for_push) + utils.print_successful_file_creation_message(file_to_push) + utils.print_successful_file_push_message(file_to_push) + + +def select_random_file(type): + return random.choice(os.listdir(config.PATH_CODE_STORAGE_RANDOM + type)) + + +def parse_lines(lines): + range_linas = list(map(lambda x: int(x), lines.split('-'))) + if len(range_linas) == 2: + min_lines, max_lines = range_linas + return random.randint(min_lines, max_lines) + else: + return range_linas[0] + + +def random_part_string(code, count_lines): + parts_code = code.splitlines(keepends=True) + if len(parts_code) < count_lines: + return code + else: + start = random.randint(0, len(parts_code) - count_lines) + return "".join(parts_code[start:start + count_lines]) + + +def select_random_code(type_folder, count_lines): + if os.path.isdir(os.path.join(config.PATH_CODE_STORAGE_RANDOM + type_folder)): + lines = parse_lines(count_lines) + code_from_file = get_code_from_file( + os.path.join(config.PATH_CODE_STORAGE_RANDOM, type_folder, + select_random_file(type_folder))) + code = random_part_string(code_from_file, lines) + return code + else: + return None + + +def preparing_for_a_commit(acc_dict: Dict): + token = acc_dict['token'] + git, acc = connect_to_account(token) + if not acc: + return False + for repo_link in acc_dict['repos']: + repo = connect_to_repo(acc, repo_link['name']) + if not repo: + repo = acc.create_repo(repo_link['name']) + repo.create_file("README.md", "Initial commit", f"#{repo_link['name']}", branch=repo.default_branch) + utils.print_successful_connection_to_repo_message(repo) + for file in repo_link["files"]: + if file['random']: + code_for_push = select_random_code(file['folder'], file['lines']) + if code_for_push: + utils.print_successful_read_of_random_code_from_file_message(file) + else: + utils.print_no_existence_of_folder_message(file) + continue + else: + path_to_file = os.path.join(config.PATH_CODE_STORAGE, file['output']) + if os.path.isfile(path_to_file): + code_for_push = get_code_from_file(path_to_file) + utils.print_read_of_file_result_message(file, True) + else: + utils.print_read_of_file_result_message(file, False) + continue + push_in_repo(repo, file['input'], code_for_push) + return True + + +def main(): + accounts_files = get_list_files_accounts() + accounts_dict = parse_json_accounts(accounts_files) + for acc_dict in accounts_dict: + preparing_for_a_commit(acc_dict) + print('') + + +if __name__ == "__main__": + main() diff --git a/script/main.py b/script/main.py deleted file mode 100644 index 29b391b..0000000 --- a/script/main.py +++ /dev/null @@ -1,139 +0,0 @@ -import json -import os -from typing import Any, Dict - -import colorama -import github -from github import BadCredentialsException -from github import GithubException -from github import UnknownObjectException - -PATH_PRIVATE_TOKENS = "./private_token.txt" -PATH_FOLDER_ACCOUNTS = "./account/" -PATH_FOLDER_CODE_STORAGE = "./code/" -colorama.init(autoreset=True) - - -def get_list_files_accounts(): - return list(map(lambda x: PATH_FOLDER_ACCOUNTS + x, [file for file in os.listdir(PATH_FOLDER_ACCOUNTS)])) - - -def parse_json_accounts(files) -> list[Any]: - list_accounts = list() - for file in files: - with open(file, 'r') as file: - data = json.load(file) - list_accounts.append(data) - return list_accounts - - -def read_file_private_token(): - with open(PATH_PRIVATE_TOKENS, "r") as file: - lines = list(map(lambda x: x.replace("\n", ''), file.readlines())) - return lines - - -def info_acc(token): - acc = github.Github(token) - user = acc.get_user() - print(f"acc - {user.login}") - print(f"name - {user.name}\n") - - -def get_repo_name_from_link(link): - parts = link.split('/') - username = parts[-2] - repo_name = parts[-1] - return f"{username}/{repo_name}" - - -def get_code_from_file(file_name): - full_path = f"{PATH_FOLDER_CODE_STORAGE}/{file_name}" - try: - with open(full_path, "r") as file_name: - code = file_name.read() - except FileNotFoundError: - return None, False - return code, True - - -def check_exist_file_in_repo(files_in_repo, check_file): - if check_file in [file.name for file in files_in_repo if file.type == 'file']: - return True - else: - return False - - -def connecting_to_account(token): - git = github.Github(token) - try: - acc = git.get_user() - print(acc.login) - return git, acc - except BadCredentialsException: - print(f"ОШИБКА подключения по токену {token}") - return None, None - - -def connecting_to_repo(acc, repo_name): - try: - repo = acc.get_repo(repo_name) - return repo - except UnknownObjectException: - return None - - -def connecting_to_file(repo, file_path): - try: - return repo.get_contents(file_path) - except (UnknownObjectException, GithubException): - return None - - -def push_in_repo(repo, file, code_from_file): - content_file = connecting_to_file(repo, file) - if content_file: - text_for_push = content_file.decoded_content.decode('utf-8') + code_from_file - commit_for_push = f"update file {file}" - file_sha = content_file.sha - repo.update_file(path=file, message=commit_for_push, content=text_for_push, sha=file_sha) - print(f"Файл {file} запушился {colorama.Fore.GREEN}УСПЕШНО") - else: - text_for_push = code_from_file - commit_for_push = f"create file {file}" - repo.create_file(path=file, message=commit_for_push, content=text_for_push) - print(f"Файл {file} создался и запушился {colorama.Fore.GREEN}УСПЕШНО") - - -def preparing_for_a_commit(acc_dict: Dict): - git, acc = connecting_to_account(acc_dict['token']) - if not acc: - return False - for repo_link in acc_dict['repos']: - repo = connecting_to_repo(acc, repo_link['name']) - if not repo: - repo = acc.create_repo(repo_link['name']) - repo.create_file("README.md", "Initial commit", f"#{repo_link['name']}", branch=repo.default_branch) - print(f"Подключение к репозиторию {repo.html_url} - {colorama.Fore.GREEN}УСПЕШНО") - for file in repo_link["files"]: - name_output_file = file['output'] - code_from_file, correct_file = get_code_from_file(file["output"]) - if correct_file: - print(f"Файл {name_output_file} считался {colorama.Fore.GREEN}УСПЕШНО") - else: - print(f"Файл {name_output_file} считался {colorama.Fore.RED}НЕ УСПЕШНО") - continue - name_input_file = file['input'] - push_in_repo(repo, name_input_file, code_from_file) - return True - - -def main(): - accounts_files = get_list_files_accounts() - accounts_dict = parse_json_accounts(accounts_files) - for acc_dict in accounts_dict: - preparing_for_a_commit(acc_dict) - - -if __name__ == "__main__": - main() diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..91fe014 --- /dev/null +++ b/utils.py @@ -0,0 +1,32 @@ +import colorama + + +def print_error_connecting_with_token(token): + print(f"ОШИБКА подключения по токену {token}") + + +def print_successful_connection_to_repo_message(repo): + print(f"Подключение к репозиторию {repo.html_url} - {colorama.Fore.GREEN}УСПЕШНО") + + +def print_successful_file_push_message(file_to_push): + print(f"Файл {file_to_push} запушился {colorama.Fore.GREEN}УСПЕШНО") + + +def print_successful_file_creation_message(file_to_push): + print(f"Файл {file_to_push} создался {colorama.Fore.GREEN}УСПЕШНО") + + +def print_successful_read_of_random_code_from_file_message(file): + print(f"Случайный код из файла тип {colorama.Back.BLUE}{file['folder'][:-1]}{colorama.Style.RESET_ALL} считался {colorama.Fore.GREEN}УСПЕШНО") + + +def print_no_existence_of_folder_message(file): + print(f"Папки с файлами типа {file['folder']}{colorama.Fore.RED} НЕ СУЩЕСТВУЕТ") + + +def print_read_of_file_result_message(file, isSuccessful): + if isSuccessful: + print(f"Файл {file['output']} считался {colorama.Fore.GREEN}УСПЕШНО") + else: + print(f"Файл {file['output']} считался {colorama.Fore.RED}НЕУСПЕШНО") \ No newline at end of file