|
| 1 | +import json |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import Crypto |
| 6 | +import Crypto.Random |
| 7 | +import requests |
| 8 | +from Crypto.Cipher import AES |
| 9 | +from dotenv import load_dotenv |
| 10 | +from git import Repo |
| 11 | + |
| 12 | +load_dotenv() |
| 13 | +BASE_URL = os.environ.get("BASE_URL") |
| 14 | +INITIALIZATION_VECTOR = os.environ.get("INITIALIZATION_VECTOR") |
| 15 | +ENCRYPTION_KEY = os.environ.get("ENCRYPTION_KEY") |
| 16 | + |
| 17 | +def make_api_request(request_data): |
| 18 | + # Define the API endpoint and the request payload (in JSON format) |
| 19 | + print (str.encode(ENCRYPTION_KEY)) |
| 20 | + |
| 21 | + # Encrypt the request_data as needed with the INITIALIZATION_VECTOR and ENCRYPTION_KEY |
| 22 | + cipher = AES.new(str.encode(ENCRYPTION_KEY), AES.MODE_CBC, iv=str.encode(INITIALIZATION_VECTOR)) |
| 23 | + encrypted = cipher.encrypt(request_data) |
| 24 | + |
| 25 | + |
| 26 | + # Make an HTTP POST request to the API |
| 27 | + url = f"{BASE_URL}/api/serverless" |
| 28 | + try: |
| 29 | + response = requests.post(url, data=encrypted) |
| 30 | + response.raise_for_status() # Raise an exception for HTTP errors (4xx and 5xx) |
| 31 | + |
| 32 | + # Handle the API response here |
| 33 | + response_data = response.json() |
| 34 | + return response_data |
| 35 | + except requests.exceptions.RequestException as e: |
| 36 | + print(f"Error making the API request: {e}") |
| 37 | + return None |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +def load_problems(): |
| 43 | + with tempfile.TemporaryDirectory() as base_path: |
| 44 | + Repo.clone_from("https://github.com/kyle8998/Practice-Coding-Questions.git", base_path) |
| 45 | + |
| 46 | + # Define the path to the "repo/leetcode" directory |
| 47 | + base_path += "/leetcode" |
| 48 | + problems = [] |
| 49 | + |
| 50 | + # Iterate over the folders in the base directory |
| 51 | + for folder_name in os.listdir(base_path): |
| 52 | + folder_path = os.path.join(base_path, folder_name) |
| 53 | + |
| 54 | + # Check if the item is a directory |
| 55 | + if os.path.isdir(folder_path): |
| 56 | + problem_md_path = os.path.join(folder_path, "problem.md") |
| 57 | + |
| 58 | + # Check if "problem.md" exists in the folder |
| 59 | + if os.path.exists(problem_md_path): |
| 60 | + # Open and read the contents of "problem.md" |
| 61 | + with open(problem_md_path, "r", encoding="utf-8") as problem_file: |
| 62 | + problem_contents = problem_file.read() |
| 63 | + problems.append(problem_contents) |
| 64 | + else: |
| 65 | + print(f"No problem.md found in {folder_name}") |
| 66 | + print(f"Total number of questions found: {len(problems)}") |
| 67 | + return problems |
| 68 | + |
| 69 | +def parse(qn): |
| 70 | + try: |
| 71 | + qn = qn.split("\n") |
| 72 | + title = qn[0][2:] |
| 73 | + qn.pop(0) |
| 74 | + while qn[0].strip() == "": |
| 75 | + qn.pop(0) |
| 76 | + difficulty = qn[0].split(" ")[2].strip().upper() |
| 77 | + qn.pop(0) |
| 78 | + while qn[0].strip() == "": |
| 79 | + qn.pop(0) |
| 80 | + |
| 81 | + if difficulty not in ["EASY", "MEDIUM", "HARD"]: |
| 82 | + return None |
| 83 | + |
| 84 | + if title.split(" ")[0] != "Problem" and title.split(" ")[1][-1] != ":": |
| 85 | + return None |
| 86 | + else: |
| 87 | + title = " ".join(title.split(" ")[2:]) |
| 88 | + |
| 89 | + return { "title": title, "difficulty": difficulty, "question": qn} |
| 90 | + except: |
| 91 | + return None |
| 92 | + |
| 93 | +def send_to_questions_service(qn): |
| 94 | + data = {} |
| 95 | + data['title'] = qn["title"] |
| 96 | + data['difficulty'] = qn["difficulty"] |
| 97 | + data['question'] = qn["question"] |
| 98 | + |
| 99 | + json_data = json.dumps(data) |
| 100 | + print(json_data) |
| 101 | + response = make_api_request(json_data) |
| 102 | + |
| 103 | + print(f"How send ah...") |
| 104 | + |
| 105 | +problems = load_problems() |
| 106 | +problems = [parse(qn) for qn in problems] |
| 107 | +problems = [qn for qn in problems if qn is not None] |
| 108 | +print(f"Total number of questions parsed: {len(problems)}") |
| 109 | + |
| 110 | +for qn in [problems[0]]: |
| 111 | + send_to_questions_service(qn) |
0 commit comments