instant-data-scraper-utils/bin/stage-2/prepare-stage-2.md
2024-07-16 18:08:36 -04:00

7.1 KiB

Prompt 1

- [Description]: This project revolves around creating a set of python scripts which we will use to parse CSV data. The CSV data represents Google Business Profile data. It was scraped from Google Maps. Our purpose is to clean this data up and prepare it for use with a toolkit of custom made python scripts. - [Project Root]: "/home/ld/mgk-scrapes" - [Current Dataset]: "[Project Root]/current-data/" - [Data Directory]: "[Current Dataset]/.data/" - [Parent Data]: "[Current Dataset]/"(All directories that do not begin with a period ".") - [Stage Directories]: "[Data Directory]/stage-#" (Where # is a number.) - [State Directories]: The state directories are the directories directly under one of the [Stage Directories]. These directories store data associated with a specific state. - [County Directories]: The coiunty directories are the directories directly under one of the [State Directories]. These directories store data associated with a specific county inside of the state the county directory is under. - [Stage 1]: "[Data Directory]/stage-1/" - [Stage 2]: "[Data Directory]/stage-2/" - [Stage 3]: "[Data Directory]/stage-3/" - [Stage 4]: "[Data Directory]/stage-4/" - [Stage 5]: "[Data Directory]/stage-5/" - [Stage 6]: "[Data Directory]/stage-6/" - [Binaries]: "[Project Root]/bin/" - [Stage 1 Binaries]: "[Binaries]/stage-1/" - [Stage 2 Binaries]: "[Binaries]/stage-2/" - [Stage 3 Binaries]: "[Binaries]/stage-3/" - [Stage 4 Binaries]: "[Binaries]/stage-4/" - [Stage 5 Binaries]: "[Binaries]/stage-5/" - [Stage 6 Binaries]: "[Binaries]/stage-6/" - [Bad Matching Services]: "[Data Directory]/bad-matching-services.txt" - [GBP Business Categories]: "[Data Directory]/gbp-business-categories.txt" - [GBP Matching Services]: "[Data Directory]/gbp-matching-services.txt" - [Unknown Blacklist]: "[Data Directory]/unknown-blacklist.txt" - [Rule 1]: All scripts need to be able to be ran from any directory. - [Rule 2]: All scripts need output what they are doing, as they are doing it. - [Rule 3]: All tasks should use halo to report successes and failures. - [Rule 4]: The python module "tqdm" can be used to report progress when appropriate. - [Rule 5]: Scripts must be heavily commented, describing the purpose of the script and what each code block inside the code is for. - [Rule 6]: Every script should begin with a hashbang "#!/usr/bin/env python". - [Rule 7]: Every script should have a name, and the name of the script should be immediately beneath the hashbang in a comment field prefixed by a string that reads "Script Name: ". - [Rule 8]: Scripts should generally have robust error checking. - [Rule 9]: The first output from any script should be pyfiglet outputting the name of the script. The name that pyfiglet outputs though should be a modified version of the script name. The pyfiglet script name needs to replace the hyphens with spaces in the script name, and capitalize the words in the script name after the hyphens have been replaced.

Please be sure to reference project details above when factoring code, or answering questions.

Now please create the prepare-stage-2 script.

Base the 'prepare-stage-2' script on the 'prepare-stage-1' script below:

#!/usr/bin/env python # Script Name: prepare-stage-1

import os import shutil import halo from tqdm import tqdm from pyfiglet import Figlet

Define the paths based on the project details

PROJECT_ROOT = "/home/ld/mgk-scrapes" CURRENT_DATASET = os.path.join(PROJECT_ROOT, "current-data") DATA_DIRECTORY = os.path.join(CURRENT_DATASET, ".data") STAGE_1_DIRECTORY = os.path.join(DATA_DIRECTORY, "stage-1") PARENT_DATA_DIRECTORY = CURRENT_DATASET

def create_stage_1_directory(): """Create the stage-1 directory, replacing it if it already exists.""" if os.path.exists(STAGE_1_DIRECTORY): print("Existing Stage 1 found, deleting and replacing...") shutil.rmtree(STAGE_1_DIRECTORY) os.makedirs(STAGE_1_DIRECTORY) print("Stage 1 directory created.")

def find_parent_data(): """Find all parent data directories.""" parent_data_dirs = [ d for d in os.listdir(PARENT_DATA_DIRECTORY) if os.path.isdir(os.path.join(PARENT_DATA_DIRECTORY, d)) and not d.startswith('.') ] return parent_data_dirs

def copy_data_to_stage_1(parent_data_dirs): """Copy data from parent data directories to stage-1.""" for directory in parent_data_dirs: src_dir = os.path.join(PARENT_DATA_DIRECTORY, directory) dest_dir = os.path.join(STAGE_1_DIRECTORY, directory) shutil.copytree(src_dir, dest_dir)

def verify_stage_1_data(parent_data_dirs): """Verify that the data in stage-1 matches the parent data.""" for directory in parent_data_dirs: src_dir = os.path.join(PARENT_DATA_DIRECTORY, directory) dest_dir = os.path.join(STAGE_1_DIRECTORY, directory)

    for root, dirs, files in os.walk(src_dir):
        for file in files:
            src_file = os.path.join(root, file)
            dest_file = src_file.replace(PARENT_DATA_DIRECTORY, STAGE_1_DIRECTORY)
            if not os.path.exists(dest_file):
                return False
return True

def main(): figlet = Figlet(font='slant') script_name = "prepare-stage-1".replace("-", " ").title() print(figlet.renderText(script_name))

print("Preparing Stage 1...")

create_stage_1_directory()

parent_data_dirs = find_parent_data()

if parent_data_dirs:
    print("Found data for the following states:")
    print(", ".join(parent_data_dirs))
    
    print("Copying data to Stage 1...")
    spinner = halo.Halo(text='Copying data', spinner='dots')
    spinner.start()
    copy_data_to_stage_1(parent_data_dirs)
    spinner.succeed("Data copied.")
    
    print("Verifying Stage 1 data...")
    is_valid = verify_stage_1_data(parent_data_dirs)
    
    if is_valid:
        print("Data verification successful. Stage 1 data is valid.")
    else:
        print("Data verification failed. Stage 1 data is not valid.")
else:
    print("No parent data found.")

if name == "main": main()

Prompt 2

The script errored out.

Here is the output:

Preparing Stage 2... Stage 2 directory created. Copying data to Stage 2... ⠙ Copying dataTraceback (most recent call last): File "/home/ld/mgk-scrapes/bin/stage-2/./prepare-stage-2.py", line 63, in main() File "/home/ld/mgk-scrapes/bin/stage-2/./prepare-stage-2.py", line 51, in main copy_data_to_stage_2() File "/home/ld/mgk-scrapes/bin/stage-2/./prepare-stage-2.py", line 27, in copy_data_to_stage_2 shutil.copytree(STAGE_1_DIRECTORY, STAGE_2_DIRECTORY) File "/usr/lib64/python3.12/shutil.py", line 600, in copytree return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/shutil.py", line 498, in _copytree os.makedirs(dst, exist_ok=dirs_exist_ok) File "", line 225, in makedirs FileExistsError: [Errno 17] File exists: '/home/ld/mgk-scrapes/current-data/.data/stage-2'