diff --git a/~extractpython.py b/~extractpython.py index dd9377a..397b58a 100644 --- a/~extractpython.py +++ b/~extractpython.py @@ -1,6 +1,6 @@ +import re import os -# Try to read the AI's response from 'aianswer.txt' try: with open('aianswer.txt', 'r', encoding='utf-8') as file: ai_response = file.read() @@ -13,13 +13,17 @@ if ai_response is not None: start_markers = ["```python", "```"] end_marker = "```" start_index = -1 + for marker in start_markers: start_index = ai_response.find(marker) if start_index != -1: start_marker = marker break - end_index = ai_response.find(end_marker, start_index + len(start_marker)) + if start_index != -1: + end_index = ai_response.find(end_marker, start_index + len(start_marker)) + else: + end_index = -1 if start_index != -1 and end_index != -1: python_content = ai_response[start_index + len(start_marker):end_index] @@ -31,18 +35,19 @@ if ai_response is not None: try: with open(aipythonanswer_path, 'r', encoding='utf-8') as aipython_file: existing_content = aipython_file.read() - + if existing_content != python_content: # If python_content is different, delete 'aipythonanswer.txt' and create a new one try: os.remove(aipythonanswer_path) - with open(aipythonanswer_path, 'w', encoding='utf-8') as python_file: - python_file.write(python_content) - with open(utf8_path, 'w', encoding='utf-8') as utf8_file: - utf8_file.write(python_content) - print("Python content updated in 'aipythonanswer.txt' and saved to 'utf8.txt'") except FileNotFoundError: - print("Error: 'aipythonanswer.txt' not found") + pass # Ignore if the file doesn't exist + + with open(aipythonanswer_path, 'w', encoding='utf-8') as python_file: + python_file.write(python_content) + with open(utf8_path, 'w', encoding='utf-8') as utf8_file: + utf8_file.write(python_content) + print("Python content updated in 'aipythonanswer.txt' and saved to 'utf8.txt'") else: print("Python content in 'aipythonanswer.txt' is the same as extracted content.") except FileNotFoundError: @@ -57,3 +62,43 @@ if ai_response is not None: print("No Python code found between triple backticks in 'aianswer.txt'") else: print("No AI response found in 'aianswer.txt'") + + + +# Function to extract code block between "```python" or "```" +def extract_code_block(text): + code_block = "" + in_code_block = False + for line in text: + if line.strip() == "```python" or line.strip() == "```": + if in_code_block: + in_code_block = False + else: + in_code_block = True + elif in_code_block: + code_block += line + return code_block.strip() + +# Function to search for "pip install" and extract the package name +def extract_pip_package_name(text): + matches = re.findall(r'pip install ([^\s]+)', text) + if matches: + # Remove non-English characters + package_name = re.sub(r'[^a-zA-Z]', '', matches[0]) + return package_name + return None + +# Read the content of 'aianswer.txt' in UTF-8 +with open('aianswer.txt', 'r', encoding='utf-8') as file: + content = file.readlines() + +# Check if the content is not empty +if content: + # Extract code block + code_block = extract_code_block(content) + + # Extract and save pip package name to 'pip.txt' + pip_package_name = extract_pip_package_name(''.join(content)) + if pip_package_name: + with open('pip.txt', 'w', encoding='utf-8') as pip_file: + pip_file.write(pip_package_name)