From 0acd82f06f20679bbd2dfc4688f072679b0df7ea Mon Sep 17 00:00:00 2001 From: rekerok Date: Mon, 16 Jan 2023 00:21:55 +0300 Subject: [PATCH 1/4] move main.py on root directory --- script/main.py => main.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename script/main.py => main.py (100%) diff --git a/script/main.py b/main.py similarity index 100% rename from script/main.py rename to main.py From 5bb522ded1c04c527609e57844acc364e2dfef13 Mon Sep 17 00:00:00 2001 From: rekerok Date: Mon, 16 Jan 2023 22:45:43 +0300 Subject: [PATCH 2/4] move the config to a separate file and update gitignore --- .gitignore | 5 +++-- config.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 config.py 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/") From 9a96d6e7acf5b0fdda98d5b39d9faf1435f56f93 Mon Sep 17 00:00:00 2001 From: rekerok Date: Wed, 18 Jan 2023 00:29:50 +0300 Subject: [PATCH 3/4] added the ability to randomly select a file and a random number of lines in this file --- main.py | 96 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/main.py b/main.py index 29b391b..d32f4b9 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import json import os +import random from typing import Any, Dict import colorama @@ -8,14 +9,14 @@ from github import GithubException from github import UnknownObjectException -PATH_PRIVATE_TOKENS = "./private_token.txt" -PATH_FOLDER_ACCOUNTS = "./account/" -PATH_FOLDER_CODE_STORAGE = "./code/" +import config + 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)])) + 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]: @@ -27,12 +28,6 @@ def parse_json_accounts(files) -> list[Any]: 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() @@ -47,21 +42,10 @@ def get_repo_name_from_link(link): 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 get_code_from_file(path_to_file): + with open(path_to_file, "r") as file_name: + code = file_name.read() + return code def connecting_to_account(token): @@ -105,8 +89,43 @@ def push_in_repo(repo, file, code_from_file): print(f"Файл {file} создался и запушился {colorama.Fore.GREEN}УСПЕШНО") +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): - git, acc = connecting_to_account(acc_dict['token']) + token = acc_dict['token'] + git, acc = connecting_to_account(token) if not acc: return False for repo_link in acc_dict['repos']: @@ -116,15 +135,23 @@ def preparing_for_a_commit(acc_dict: Dict): 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}УСПЕШНО") + if file['random']: + code_for_push = select_random_code(file['folder'], file['lines']) + if code_for_push: + print( + f"Случайный код из файла тип {colorama.Back.BLUE}{file['folder'][:-1]}{colorama.Style.RESET_ALL} считался {colorama.Fore.GREEN}УСПЕШНО") + else: + print(f"Папки с файлами типа {file['folder']}{colorama.Fore.RED} НЕ СУЩЕСТВУЕТ") + continue 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) + 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) + print(f"Файл {file['output']} считался {colorama.Fore.GREEN}УСПЕШНО") + else: + print(f"Файл {file['output']} считался {colorama.Fore.RED}НЕУСПЕШНО") + continue + push_in_repo(repo, file['input'], code_for_push) return True @@ -133,6 +160,7 @@ def main(): accounts_dict = parse_json_accounts(accounts_files) for acc_dict in accounts_dict: preparing_for_a_commit(acc_dict) + print('') if __name__ == "__main__": From 1df5ef3da2509b6670449b0a0f6049f27680ab39 Mon Sep 17 00:00:00 2001 From: Milana Antonova Date: Sat, 21 Jan 2023 18:35:26 +0300 Subject: [PATCH 4/4] refactor some code --- main.py | 48 ++++++++++++++++++++++++------------------------ utils.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 utils.py diff --git a/main.py b/main.py index d32f4b9..0b300ce 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from github import GithubException from github import UnknownObjectException -import config +import config, utils colorama.init(autoreset=True) @@ -28,7 +28,7 @@ def parse_json_accounts(files) -> list[Any]: return list_accounts -def info_acc(token): +def print_info_account(token): acc = github.Github(token) user = acc.get_user() print(f"acc - {user.login}") @@ -48,18 +48,18 @@ def get_code_from_file(path_to_file): return code -def connecting_to_account(token): +def connect_to_account(token): git = github.Github(token) try: acc = git.get_user() print(acc.login) return git, acc except BadCredentialsException: - print(f"ОШИБКА подключения по токену {token}") + utils.print_error_connecting_with_token(token) return None, None -def connecting_to_repo(acc, repo_name): +def connect_to_repo(acc, repo_name): try: repo = acc.get_repo(repo_name) return repo @@ -67,26 +67,27 @@ def connecting_to_repo(acc, repo_name): return None -def connecting_to_file(repo, file_path): +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, code_from_file): - content_file = connecting_to_file(repo, file) +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_from_file - commit_for_push = f"update file {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, message=commit_for_push, content=text_for_push, sha=file_sha) - print(f"Файл {file} запушился {colorama.Fore.GREEN}УСПЕШНО") + 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_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}УСПЕШНО") + 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): @@ -125,31 +126,30 @@ def select_random_code(type_folder, count_lines): def preparing_for_a_commit(acc_dict: Dict): token = acc_dict['token'] - git, acc = connecting_to_account(token) + git, acc = connect_to_account(token) if not acc: return False for repo_link in acc_dict['repos']: - repo = connecting_to_repo(acc, repo_link['name']) + 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) - print(f"Подключение к репозиторию {repo.html_url} - {colorama.Fore.GREEN}УСПЕШНО") + 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: - print( - f"Случайный код из файла тип {colorama.Back.BLUE}{file['folder'][:-1]}{colorama.Style.RESET_ALL} считался {colorama.Fore.GREEN}УСПЕШНО") + utils.print_successful_read_of_random_code_from_file_message(file) else: - print(f"Папки с файлами типа {file['folder']}{colorama.Fore.RED} НЕ СУЩЕСТВУЕТ") + 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) - print(f"Файл {file['output']} считался {colorama.Fore.GREEN}УСПЕШНО") + utils.print_read_of_file_result_message(file, True) else: - print(f"Файл {file['output']} считался {colorama.Fore.RED}НЕУСПЕШНО") + utils.print_read_of_file_result_message(file, False) continue push_in_repo(repo, file['input'], code_for_push) return True 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