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

7.3 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 refactor the process-stage-4 script below to be in accordance with the above.

This script will not need to check for the existence of Stage 4, or worry about creating it. This task is handled elsewhere now. Remove any function that checks for Stage 4, creates it or recreates it.

Please create the process-stage-4 in the same style as the process-stage-2 script below:

#!/usr/bin/env python # Script Name: process-stage-2

import os import subprocess from pyfiglet import Figlet from halo import Halo

Define the paths based on the project details

PROJECT_ROOT = "/home/ld/mgk-scrapes" BIN_STAGE_2_DIR = os.path.join(PROJECT_ROOT, 'bin', 'stage-2') BIN_DIR = os.path.join(PROJECT_ROOT, 'bin')

def run_scripts(): """Run the specified scripts in order.""" scripts = [ os.path.join(BIN_STAGE_2_DIR, "prepare-stage-2.py"), os.path.join(BIN_STAGE_2_DIR, "remove-utm.py"), os.path.join(BIN_STAGE_2_DIR, "sanitize-review-count.py"), os.path.join(BIN_STAGE_2_DIR, "sanitize-phone-data.py"), os.path.join(BIN_STAGE_2_DIR, "sanitize-gbp-business-website.py"), os.path.join(BIN_DIR, "remove-empty-columns.py") ]

# Run scripts that do not require arguments
for script in scripts[:-1]:
    subprocess.run(["python", script], check=True)

# Run script that requires the 'stage-2' argument
subprocess.run(["python", scripts[-1], "stage-2"], check=True)

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

print("Running scripts for Stage 2...\n")
run_scripts()

final_spinner = Halo(spinner='dots', color='green')
final_spinner.start()
final_spinner.succeed("All scripts completed.")

if name == 'main': main() </process-stage-2 script>

Here is the current version of the process-stage-4 script which needs refactoring:

#!/usr/bin/env python

import os import shutil import subprocess from tqdm import tqdm from pyfiglet import Figlet

Get the project root directory

project_root = os.path.abspath(os.path.join(os.path.dirname(file), '..', 'Concrete Sealing Company')) data_dir = os.path.join(project_root, '.data') stage2_dir = os.path.join(data_dir, 'stage-2') stage3_dir = os.path.join(data_dir, 'stage-3')

def check_and_prepare_stage3(): # Check if the stage-3 directory exists if os.path.exists(stage3_dir): # Remove the existing stage-3 directory shutil.rmtree(stage3_dir)

# Copy stage-2 directory to stage-3
shutil.copytree(stage2_dir, stage3_dir)

def run_scripts(): scripts = [ "column-search_review-count.py", "column-search_review-rating.py", "column-search_gbp-location.py", "column-search_yib.py", "column-search_gbp-business-category.py", "column-search_gbp-matching-services.py", "column-search_business-name.py", ("remove-empty-columns.py", "stage-3"), "column-search_unknowns.py" ]

for script in scripts:
    if isinstance(script, tuple):
        script_name, arg = script
        subprocess.run(["python", script_name, arg], check=True)
    else:
        subprocess.run(["python", script], check=True)

# Call report-unknowns.py with the "stage-3" option
subprocess.run(["python", "report-unknowns.py", "stage-3"], check=True)

if name == 'main': check_and_prepare_stage3() run_scripts() </process-stage-4 script>

Item 1: The process-stage-4 script will need to call a script named "prepare-stage-4" script as the first script it calls. This script will need to be ran first by process-stage-4, and will be located in [Stage 4 Binaries]

Item 2: The following scripts which process-stage-4 calls are located in [Stage 4 Binaries]:

<stage 4 scripts> reshift-terms.py strip-neg-terms-from-unknown-cols.py strip-neg-patterns-from-unknown-cols.py delete-sponsored-listings.py </stage 4 scripts>

The following script is located in [Binaries].

remove-empty-columns.py report-unknowns.py

The scripts need to be ran in the following order:

reshift-terms.py strip-neg-terms-from-unknown-cols.py strip-neg-patterns-from-unknown-cols.py delete-sponsored-listings.py remove-empty-columns.py report-unknowns.py