From a6776a963e4ce340e07fe910a80313019abdb814 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Fri, 24 Apr 2020 15:24:51 +0100 Subject: [PATCH] add argsparse, logging and coord's. --- app/coordinators/art_coordinator.py | 45 +++++++++++++++++++++++++++++ app/main.py | 39 ++++--------------------- app/services/logging_services.py | 6 ++++ app/services/parser_services.py | 21 ++++++++++++++ 4 files changed, 78 insertions(+), 33 deletions(-) create mode 100644 app/coordinators/art_coordinator.py create mode 100644 app/services/logging_services.py create mode 100644 app/services/parser_services.py diff --git a/app/coordinators/art_coordinator.py b/app/coordinators/art_coordinator.py new file mode 100644 index 0000000..8abb9b7 --- /dev/null +++ b/app/coordinators/art_coordinator.py @@ -0,0 +1,45 @@ +import requests +from services import art_services, data_services, logging_services + +def update_art_data(arguments): + directory = arguments.target + v_setting = arguments.verbose + v_out = logging_services.log # Partial function -- for brevity. + save = data_services.store_json # (P.F.) For easier reading. + + v_out(v_setting, "Beginning to update Art data...") + + try: + raw_art_data = data_services.get_json( + "https://api.craigoates.net/api/1.0/Artwork") + v_out(v_setting, "Data from API retrived.") + + save(art_services.get_creation_date_totals(raw_art_data), + f"{directory}/art_creation_dates.json") + v_out(v_setting, "Art creation dates processed.") + + save(art_services.get_db_column_totals(raw_art_data, "category"), + f"{directory}/art_category_totals.json") + v_out(v_setting, "Art categories processed.") + + save(art_services.get_db_column_totals(raw_art_data, "medium"), + f"{directory}/art_medium_total.json") + v_out(v_setting, "Art medium(s) totals processed.") + + save(art_services.get_dimension_totals(raw_art_data, "dimensions", "width"), + f"{directory}/art_width_totals.json") + v_out(v_setting, "Art width totals processed.") + + save(art_services.get_dimension_totals(raw_art_data, "dimensions", "height"), + f"{directory}/art_height_totals.json") + v_out(v_setting, "Art height totals processed.") + + save(art_services.get_dimension_totals(raw_art_data, "dimensions", "depth"), + f"{directory}/art_depth_totals.json") + v_out(v_setting, "Art depth totals processed.") + + v_out(v_setting, "Completed updating Art data.") + + except Exception: + print("ERROR: Unable to update Art data. Exiting program.") + return 0 diff --git a/app/main.py b/app/main.py index dd0be54..df68439 100644 --- a/app/main.py +++ b/app/main.py @@ -1,40 +1,13 @@ #!/usr/bin/python3 -import requests -from datetime import datetime -from services import art_services, data_services -from pathlib import Path +from services import parser_services +from coordinators import art_coordinator def main(): - # The intention is for this to be accessible from other projects. - directory = str(f"{Path.home()}/coblob-data") - - raw_art_data = data_services.get_json( - "https://api.craigoates.net/api/1.0/Artwork") - - save = data_services.store_json # For easier reading. - - save(art_services.get_creation_date_totals(raw_art_data), - f"{directory}/art_creation_dates.json") - - save(art_services.get_db_column_totals(raw_art_data, "category"), - f"{directory}/art_category_totals.json") - - save(art_services.get_db_column_totals(raw_art_data, "medium"), - f"{directory}/art_medium_total.json") - - save(art_services.get_dimension_totals(raw_art_data, "dimensions", "width"), - f"{directory}/art_width_totals.json") - - save(art_services.get_dimension_totals(raw_art_data, "dimensions", "height"), - f"{directory}/art_height_totals.json") - - save(art_services.get_dimension_totals(raw_art_data, "dimensions", "depth"), - f"{directory}/art_depth_totals.json") - - # Use for console priting for the moment. - date = datetime.now().strftime('%d/%m/%Y') - time = datetime.now().strftime("%H:%M") + args = parser_services.create_args() + art_coordinator.update_art_data(args) + # update_software_data(args) # Future update. + # update_article_data(args) # Future update. if __name__ == "__main__": main() diff --git a/app/services/logging_services.py b/app/services/logging_services.py new file mode 100644 index 0000000..faca2b0 --- /dev/null +++ b/app/services/logging_services.py @@ -0,0 +1,6 @@ +# This is for outputting the program's status when the verbose switch +# is used. + +def log(log_output, message): + if log_output is True: + print(message) diff --git a/app/services/parser_services.py b/app/services/parser_services.py new file mode 100644 index 0000000..2fa9c2d --- /dev/null +++ b/app/services/parser_services.py @@ -0,0 +1,21 @@ +import argparse +import os + +def dir_path(string): + if os.path.isdir(string): + return string + else: + raise NotADirectoryError(string) + +def create_args(): + parser = argparse.ArgumentParser( + "Parses the coblob database and transforms it. " + + "This is mostly for the the benefit of the co-data project. " + + "It, also, requires access to the co-api project, via the internet.") + parser.add_argument("-t", "--target", type = dir_path, required = True, + help = "the location you would like the data to be stored at.") + parser.add_argument("-v", "--verbose", action = "store_true", + help = "provides detailed output when program is running.") + + args = parser.parse_args() + return args