import boto3


def list_s3_objects(bucket_name, prefix):
    s3 = boto3.client("s3")

    objects = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix)

    if "Contents" in objects:
        for obj in objects["Contents"]:
            print(obj["Key"])
    else:
        print("No objects found.")


import boto3
import io
import zipfile


def list_zip_contents(bucket_name, zip_key):
    s3 = boto3.client("s3")

    # Download ZIP file as a stream
    response = s3.get_object(Bucket=bucket_name, Key=zip_key)
    zip_stream = io.BytesIO(response["Body"].read())

    # Open ZIP file from memory
    with zipfile.ZipFile(zip_stream, "r") as zip_ref:
        file_list = zip_ref.namelist()
        print("Files in ZIP:", file_list)
        return file_list


def extract_file_from_zip(bucket_name, zip_key, file_name):
    s3 = boto3.client("s3")

    # Download ZIP file as a stream
    response = s3.get_object(Bucket=bucket_name, Key=zip_key)
    zip_stream = io.BytesIO(response["Body"].read())

    # Open ZIP file from memory
    with zipfile.ZipFile(zip_stream, "r") as zip_ref:
        if file_name in zip_ref.namelist():
            file_data = zip_ref.read(file_name)
            print(f"Extracted {file_name}: {len(file_data)} bytes")
            return file_data
        else:
            print(f"{file_name} not found in ZIP.")
            return None


if __name__ == "__main__":
    bucket_name = "digitalcorpora"
    prefix = "corpora/files/CC-MAIN-2021-31-PDF-UNTRUNCATED/"

    list_s3_objects(bucket_name, prefix)

    bucket = "digitalcorpora"
    zip_path = (
        "corpora/files/CC-MAIN-2021-31-PDF-UNTRUNCATED/zipfiles/0000-0999/0000.zip"
    )

    # List files in the ZIP
    files = list_zip_contents(bucket, zip_path)

    # Example: Extract a specific file (replace 'yourfile.pdf' with an actual filename)
    if files:
        file_to_extract = files[0]  # Extract the first file as an example
        extracted_data = extract_file_from_zip(bucket, zip_path, file_to_extract)

        # Optional: Save to local file (Uncomment to enable)
        # with open(file_to_extract, "wb") as f:
        #     f.write(extracted_data)
