import os
from PIL import Image
import shutil
import re
import subprocess
# import chen_anh
# ----------------------------------------------------------------------------------------------------------------------
def is_image_readable(file_path):
    try:
        with Image.open(file_path) as img:
            img.verify()
        return True
    except:
        return False

def delete_unreadable_jpg_files(folder_path):
    deleted_files = 0
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.lower().endswith('.jpg'):
                file_path = os.path.join(root, file)
                if not is_image_readable(file_path):
                    try:
                        os.remove(file_path)
                        print(f"Deleted unreadable: {file_path}")
                        deleted_files += 1
                    except Exception as e:
                        print(f"Error deleting {file_path}: {e}")

    print(f"Total unreadable .jpg files deleted: {deleted_files}")

# ----------------------------------------------------------------------------------------------------------------------
def count_jpg_files(folder_path):
    return len([f for f in os.listdir(folder_path) if f.lower().endswith('.jpg')])


def delete_subfolders_with_few_images(folder_path, threshold=3):
    deleted_folders = 0
    for root, dirs, files in os.walk(folder_path, topdown=False):
        for dir in dirs:
            subfolder_path = os.path.join(root, dir)
            jpg_count = count_jpg_files(subfolder_path)
            if jpg_count <= threshold:
                try:
                    shutil.rmtree(subfolder_path)
                    print(f"Deleted subfolder: {subfolder_path} (contained {jpg_count} jpg files)")
                    deleted_folders += 1
                except Exception as e:
                    print(f"Error deleting subfolder {subfolder_path}: {e}")

    print(f"Total subfolders deleted: {deleted_folders}")

# ----------------------------------------------------------------------------------------------------------------------

def trim_jpg_files(folder_path, target_count=4):
    for root, dirs, files in os.walk(folder_path):
        jpg_files = [f for f in files if f.lower().endswith('.jpg')]
        jpg_count = len(jpg_files)

        if jpg_count <= target_count:
            continue

        jpg_files.sort(key=lambda x: (
            not x.endswith('_0.jpg'),
            int(re.search(r'_(\d+)\.jpg$', x).group(1)) if re.search(r'_(\d+)\.jpg$', x) else 0,
            x
        ), reverse=True)

        has_1_to_4 = any(file.endswith(f'_{i}.jpg') for file in jpg_files for i in range(1, 5))

        for file in jpg_files:
            if jpg_count <= target_count:
                break

            file_path = os.path.join(root, file)
            if file.endswith('_0.jpg'):
                os.remove(file_path)
                jpg_count -= 1
                print(f"Deleted: {file_path}")
            elif int(re.search(r'_(\d+)\.jpg$', file).group(1)) >= 5:
                if not has_1_to_4 and jpg_count == target_count + 1:
                    break
                os.remove(file_path)
                jpg_count -= 1
                print(f"Deleted: {file_path}")

# ----------------------------------------------------------------------------------------------------------------------
def delete_small_html_folders(folder_path, min_char_count=6000):
    for dirpath, dirnames, filenames in os.walk(folder_path, topdown=False):
        delete_folder = False

        for filename in filenames:
            if filename.lower().endswith('.html'):
                file_path = os.path.join(dirpath, filename)

                with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
                    content = file.read()

                if len(content) < min_char_count:
                    delete_folder = True
                    break

        if delete_folder:
            print(f"Deleting folder: {dirpath}")
            shutil.rmtree(dirpath)

# ----------------------------------------------------------------------------------------------------------------------
# Clear blank lines within .html
def remove_blank_lines(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    non_blank_lines = [line.strip() for line in lines if line.strip()]

    with open(file_path, 'w', encoding='utf-8') as file:
        file.write('\n'.join(non_blank_lines))

# ----------------------------------------------------------------------------------------------------------------------
def remove_img_tags(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # Remove img tags
    cleaned_content = re.sub(r'<img src="[^"]*"[^>]*>', '', content)

    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(cleaned_content)

# ----------------------------------------------------------------------------------------------------------------------
def replace_h1_tag(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    modified_lines = [line.replace('<h1 style="text-align: center;">', '<h1>') for line in lines]

    with open(file_path, 'w', encoding='utf-8') as file:
        file.writelines(modified_lines)

# ----------------------------------------------------------------------------------------------------------------------
def capitalize_first_letter(match):
    tag = match.group(1)
    content = match.group(2)
    if content:
        content = content[0].upper() + content[1:]
    return f"<{tag}>{content}</{tag}>"

# ----------------------------------------------------------------------------------------------------------------------

def process_html_files(folder_path):
    for subdir, _, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.html'):
                file_path = os.path.join(subdir, file)
                process_file(file_path)

# ----------------------------------------------------------------------------------------------------------------------
def process_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # Regular expression to match HTML tags and their content
    pattern = r'<([a-z0-9]+)(?:\s+[^>]+)?>([^<]+)</\1>'

    modified_content = re.sub(pattern, capitalize_first_letter, content)

    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(modified_content)

# ----------------------------------------------------------------------------------------------------------------------

# def proper_case_noun(match):
#     noun = match.group(0)
#     return ' '.join(word.capitalize() for word in noun.split())
#
#
# def convert_nouns_main(folder_path, noun_list):
#     for subdir, _, files in os.walk(folder_path):
#         for file in files:
#             if file.endswith('.html'):
#                 file_path = os.path.join(subdir, file)
#                 convert_nouns(file_path, noun_list)
#
# def convert_nouns(file_path, noun_list):
#     with open(file_path, 'r', encoding='utf-8') as file:
#         content = file.read()
#
#     # Create a regular expression pattern from the noun list
#     pattern = r'\b(' + '|'.join(re.escape(noun) for noun in noun_list) + r')\b'
#
#     # Replace nouns with their proper case form
#     modified_content = re.sub(pattern, proper_case_noun, content, flags=re.IGNORECASE)
#
#     with open(file_path, 'w', encoding='utf-8') as file:
#         file.write(modified_content)
#
#
# # List of nouns to be converted to proper case
# nouns_to_convert = [
#     "aave",
#     "lending",
#     "protocol",
#     "algorand",
#     "application",
#     "proof",
#     "stake",
#     "influence",
#     "investment",
#     "funds",
#     "cryptocurrency",
#     "market",
#     "whales",
#     "impact",
#     "halving",
#     "bitcoin",
#     "price",
#     "social",
#     "media",
#     "avalanche",
#     "generation",
#     "blockchain",
#     "platform",
#     "binance",
#     "coin",
#     "ecosystem",
#     "exchange",
#     "king",
#     "technology",
#     "assets",
#     "hackers",
#     "home",
#     "cost",
#     "candlestick",
#     "charts",
#     "trading",
#     "profit",
#     "differences",
#     "exchanges",
#     "beginners",
#     "analysis",
#     "interest",
#     "bots",
#     "indicators",
#     "dollar",
#     "averaging",
#     "investing",
#     "leverage",
#     "fibonacci",
#     "retracement",
#     "tokens",
#     "moving",
#     "average",
#     "technical",
#     "rsi",
#     "trends",
#     "stop",
#     "loss",
#     "take",
#     "nfts",
#     "portfolio",
#     "icos",
#     "idos",
#     "liquidity",
#     "mining",
#     "pool",
#     "staking",
#     "profits",
#     "bounty",
#     "programs",
#     "governance",
#     "launchpads",
#     "prediction",
#     "markets",
#     "yield",
#     "farming",
#     "taxes",
#     "projects",
#     "chainlink",
#     "role",
#     "oracles",
#     "smart",
#     "contracts",
#     "compound",
#     "mechanics",
#     "cosmos",
#     "blockchains",
#     "decentraland",
#     "potential",
#     "metaverse",
#     "defi",
#     "finance",
#     "revolution",
#     "enjin",
#     "gaming",
#     "ethereum",
#     "world",
#     "litecoin",
#     "competitor",
#     "benefits",
#     "risks",
#     "monero",
#     "issue",
#     "privacy",
#     "transactions",
#     "strategies",
#     "tools",
#     "investors",
#     "companies",
#     "payments",
#     "signs",
#     "bubble",
#     "wallets",
#     "mistakes",
#     "things",
#     "income",
#     "countries",
#     "legal",
#     "tender",
#     "cybersecurity",
#     "space",
#     "applications",
#     "examples",
#     "scams",
#     "factors",
#     "wallet",
#     "polygon",
#     "scaling",
#     "solution",
#     "regulations",
#     "vietnam",
#     "fiat",
#     "currency",
#     "stablecoins",
#     "volatility",
#     "stellar",
#     "payment",
#     "network",
#     "sushiswap",
#     "competition",
#     "dex",
#     "forks",
#     "value",
#     "layer",
#     "solutions",
#     "future",
#     "government",
#     "banking",
#     "system",
#     "environment",
#     "climate",
#     "change",
#     "news",
#     "importance",
#     "diversification",
#     "terra",
#     "luna",
#     "lessons",
#     "collapse",
#     "tether",
#     "tezos",
#     "self-upgrading",
#     "capability",
#     "graph",
#     "indexing",
#     "protocols",
#     "theta",
#     "streaming",
#     "video",
#     "money",
#     "laundering",
#     "cross-chain",
#     "interoperability",
#     "uniswap",
#     "vechain",
#     "supply",
#     "chain",
#     # Add more nouns here
# ]

# ----------------------------------------------------------------------------------------------------------------------
def convert_and_resize_images(folder_path):
    # Ensure the folder path is absolute
    folder_path = os.path.abspath(folder_path)

    # Command to convert all files to jpg
    convert_cmd = f'for /r /d %a in (*) do magick mogrify -format jpg "%~a\\*.*"'

    # Command to resize all jpg files to max width of 1200px
    resize_cmd = f'for /r /d %a in (*) do magick mogrify -resize 1200x "%~a\\*.jpg"'

    try:
        # Execute the conversion command
        subprocess.run(convert_cmd, shell=True, check=True, cwd=folder_path)
        print("Conversion to JPG completed successfully.")

        # Execute the resize command
        subprocess.run(resize_cmd, shell=True, check=True, cwd=folder_path)
        print("Resizing of JPG files completed successfully.")

    except subprocess.CalledProcessError as e:
        print(f"An error occurred while executing the commands: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

# ----------------------------------------------------------------------------------------------------------------------

def h1_strong_remover(folder_path):
    for subdir, _, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.html'):
                file_path = os.path.join(subdir, file)
                h1_strong_remover_process(file_path)


def h1_strong_remover_process(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    if lines:
        lines[0] = lines[0].replace('<h1><strong>', '').replace('</strong></h1>', '')

    with open(file_path, 'w', encoding='utf-8') as file:
        file.writelines(lines)

# ----------------------------------------------------------------------------------------------------------------------
def combine_blockquote_lines(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    combined_lines = []
    i = 0
    while i < len(lines):
        current_line = lines[i].strip()

        # Case 1: <blockquote> exists but </blockquote> doesn't
        if '<blockquote>' in current_line and '</blockquote>' not in current_line:
            if i + 1 < len(lines):
                next_line = lines[i + 1].strip()
                combined_line = current_line + ' ' + next_line
                combined_lines.append(combined_line + '\n')
                i += 2
            else:
                combined_lines.append(current_line + '\n')
                i += 1

        # Case 2: </blockquote> exists but <blockquote> doesn't
        elif '</blockquote>' in current_line and '<blockquote>' not in current_line:
            if i > 0:
                prev_line = combined_lines.pop().strip()
                combined_line = prev_line + ' ' + current_line
                combined_lines.append(combined_line + '\n')
            else:
                combined_lines.append(current_line + '\n')
            i += 1

        # No special case, just add the line
        else:
            combined_lines.append(current_line + '\n')
            i += 1

    with open(file_path, 'w', encoding='utf-8') as file:
        file.writelines(combined_lines)

# ----------------------------------------------------------------------------------------------------------------------
# Add the string "Xem thêm: " at the end of the article

def add_xem_them(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.html'):
                file_path = os.path.join(root, file)

                with open(file_path, 'a', encoding='utf-8') as f:
                    f.write('\n\nXem thêm: ')

                print(f"Added 'Xem thêm: ' to {file_path}")

# ----------------------------------------------------------------------------------------------------------------------
# Update the process_directory function to include both operations
def process_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.html'):
                file_path = os.path.join(root, file)
                combine_blockquote_lines(file_path)
                remove_img_tags(file_path)
                # replace_h1_tag(file_path)
                remove_blank_lines(file_path)
                print(f"Processed: {file_path}")

# ----------------------------------------------------------------------------------------------------------------------

def remove_single_quotes(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.html'):
                file_path = os.path.join(root, file)
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()

                # Remove all """ marks
                # content = content.replace(': ', ':')
                # content = content.replace(':', ': ')
                content = content.replace('"', '')
                content = content.replace('“', '')
                content = content.replace('”', '')

                with open(file_path, 'w', encoding='utf-8') as f:
                    f.write(content)

                print(f"Removed single quotes from: {file_path}")

# ----------------------------------------------------------------------------------------------------------------------
def capitalize_after_colon(folder_path):
    def capitalize_match(match):
        return match.group(1) + match.group(2).capitalize()

    pattern = r'(: )(\w)'

    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.html'):
                file_path = os.path.join(root, file)
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()

                # Capitalize first letter after ": "
                modified_content = re.sub(pattern, capitalize_match, content)

                if content != modified_content:
                    with open(file_path, 'w', encoding='utf-8') as f:
                        f.write(modified_content)
                    print(f"Capitalized text after colon in: {file_path}")

# ----------------------------------------------------------------------------------------------------------------------

# Specify the folder path here
folder_path = r'C:\Users\anhb3\Desktop\long\Game MOBA Việt - Copy (2)'

# First, delete unreadable .jpg files
delete_unreadable_jpg_files(folder_path)

# Then, delete subfolders with 3 or fewer .jpg files
delete_subfolders_with_few_images(folder_path)

# Finally, trim .jpg files in remaining subfolders
trim_jpg_files(folder_path)

# Delete short html folders
delete_small_html_folders(folder_path)

# Add the string "Xem thêm: " at the end of the article
# add_xem_them(folder_path)

# Remove <h1><strong> (remove if posting directly into wordpress server)
# h1_strong_remover(folder_path)

# Clear blank lines, remove <img> tag, replace <h1 style="text-align: center;"> into <h1> and remove blank lines within .html
process_directory(folder_path)

# remove_single_quotes(folder_path)

capitalize_after_colon(folder_path)

process_html_files(folder_path)

# Convert images to .jpg and resize images to 1200x_ based on size ratio.
# convert_and_resize_images(folder_path)

# os.system('chen_anh.py')

# convert_nouns_main(folder_path, nouns_to_convert)

improve this python script