|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +This file is dedicated to visualizing and analyzing the data collected |
| 4 | +from Deviantart. |
| 5 | +""" |
| 6 | +# Standard library |
| 7 | +import argparse |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import traceback |
| 11 | +from datetime import datetime, timezone |
| 12 | + |
| 13 | +# Third-party |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import pandas as pd |
| 16 | +import seaborn as sns |
| 17 | +from pandas import PeriodIndex |
| 18 | + |
| 19 | +# Add parent directory so shared can be imported |
| 20 | +sys.path.append(os.path.join(os.path.dirname(__file__), "..")) |
| 21 | + |
| 22 | +# First-party/Local |
| 23 | +import shared # noqa: E402 |
| 24 | + |
| 25 | +# Setup |
| 26 | +LOGGER, PATHS = shared.setup(__file__) |
| 27 | + |
| 28 | + |
| 29 | +def parse_arguments(): |
| 30 | + """ |
| 31 | + Parses command-line arguments, returns parsed arguments. |
| 32 | + """ |
| 33 | + LOGGER.info("Parsing command-line arguments") |
| 34 | + |
| 35 | + # Taken from shared module, fix later |
| 36 | + datetime_today = datetime.now(timezone.utc) |
| 37 | + quarter = PeriodIndex([datetime_today.date()], freq="Q")[0] |
| 38 | + |
| 39 | + parser = argparse.ArgumentParser(description="Deviantart Reports") |
| 40 | + parser.add_argument( |
| 41 | + "--quarter", |
| 42 | + "-q", |
| 43 | + type=str, |
| 44 | + required=False, |
| 45 | + default=f"{quarter}", |
| 46 | + help="Data quarter in format YYYYQx, e.g., 2024Q2", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--skip-commit", |
| 50 | + action="store_true", |
| 51 | + help="Don't git commit changes (also skips git push changes)", |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "--skip-push", |
| 55 | + action="store_true", |
| 56 | + help="Don't git push changes", |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + "--show-plots", |
| 60 | + action="store_true", |
| 61 | + help="Show generated plots (in addition to saving them)", |
| 62 | + ) |
| 63 | + args = parser.parse_args() |
| 64 | + if args.skip_commit: |
| 65 | + args.skip_push = True |
| 66 | + return args |
| 67 | + |
| 68 | + |
| 69 | +def load_data(args): |
| 70 | + """ |
| 71 | + Load the collected data from the CSV file. |
| 72 | + """ |
| 73 | + selected_quarter = args.quarter |
| 74 | + |
| 75 | + file_path = os.path.join( |
| 76 | + PATHS["data"], |
| 77 | + f"{selected_quarter}", |
| 78 | + "1-fetch", |
| 79 | + "deviantart_fetched.csv", |
| 80 | + ) |
| 81 | + |
| 82 | + if not os.path.exists(file_path): |
| 83 | + LOGGER.error(f"Data file not found: {file_path}") |
| 84 | + return pd.DataFrame() |
| 85 | + |
| 86 | + data = pd.read_csv(file_path) |
| 87 | + LOGGER.info(f"Data loaded from {file_path}") |
| 88 | + return data |
| 89 | + |
| 90 | + |
| 91 | +def visualize_by_license_type(data, args): |
| 92 | + """ |
| 93 | + Create a bar chart for the number of repositories licensed by license type. |
| 94 | + """ |
| 95 | + LOGGER.info( |
| 96 | + "Creating a bar chart for the number of documents by license type." |
| 97 | + ) |
| 98 | + |
| 99 | + selected_quarter = args.quarter |
| 100 | + |
| 101 | + # Strip any leading/trailing spaces from the columns |
| 102 | + data.columns = data.columns.str.strip() |
| 103 | + |
| 104 | + plt.figure(figsize=(12, 8)) |
| 105 | + ax = sns.barplot(x=data["LICENSE TYPE"], y=data["Document Count"]) |
| 106 | + plt.title("Number of DeviantArt Documents by License Type") |
| 107 | + plt.xlabel("License Type") |
| 108 | + plt.ylabel("Document Count") |
| 109 | + plt.xticks(rotation=45, ha="right") |
| 110 | + |
| 111 | + # Add value numbers to the top of each bar |
| 112 | + for p in ax.patches: |
| 113 | + ax.annotate( |
| 114 | + format(p.get_height(), ",.0f"), |
| 115 | + (p.get_x() + p.get_width() / 2.0, p.get_height()), |
| 116 | + ha="center", |
| 117 | + va="center", |
| 118 | + xytext=(0, 9), |
| 119 | + textcoords="offset points", |
| 120 | + ) |
| 121 | + |
| 122 | + output_directory = os.path.join( |
| 123 | + PATHS["data"], f"{selected_quarter}", "3-report" |
| 124 | + ) |
| 125 | + |
| 126 | + LOGGER.info(f"Output directory: {output_directory}") |
| 127 | + |
| 128 | + os.makedirs(output_directory, exist_ok=True) |
| 129 | + image_path = os.path.join( |
| 130 | + output_directory, "deviantart_license_report.png" |
| 131 | + ) |
| 132 | + plt.savefig(image_path) |
| 133 | + |
| 134 | + if args.show_plots: |
| 135 | + plt.show() |
| 136 | + |
| 137 | + shared.update_readme( |
| 138 | + PATHS, |
| 139 | + image_path, |
| 140 | + "DeviantArt", |
| 141 | + "Number of DeviantArt Documents by License Type", |
| 142 | + "License Type Report", |
| 143 | + args, |
| 144 | + ) |
| 145 | + |
| 146 | + LOGGER.info("Visualization by license type created.") |
| 147 | + |
| 148 | + |
| 149 | +def main(): |
| 150 | + |
| 151 | + # Fetch and merge changes |
| 152 | + shared.fetch_and_merge(PATHS["repo"]) |
| 153 | + |
| 154 | + args = parse_arguments() |
| 155 | + |
| 156 | + data = load_data(args) |
| 157 | + if data.empty: |
| 158 | + return |
| 159 | + |
| 160 | + current_directory = os.getcwd() |
| 161 | + LOGGER.info(f"Current working directory: {current_directory}") |
| 162 | + |
| 163 | + visualize_by_license_type(data, args) |
| 164 | + |
| 165 | + # Add and commit changes |
| 166 | + if not args.skip_commit: |
| 167 | + shared.add_and_commit( |
| 168 | + PATHS["repo"], "Added and committed new Deviantart reports" |
| 169 | + ) |
| 170 | + |
| 171 | + # Push changes |
| 172 | + if not args.skip_push: |
| 173 | + shared.push_changes(PATHS["repo"]) |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + try: |
| 178 | + main() |
| 179 | + except shared.QuantifyingException as e: |
| 180 | + if e.exit_code == 0: |
| 181 | + LOGGER.info(e.message) |
| 182 | + else: |
| 183 | + LOGGER.error(e.message) |
| 184 | + sys.exit(e.exit_code) |
| 185 | + except SystemExit as e: |
| 186 | + LOGGER.error(f"System exit with code: {e.code}") |
| 187 | + sys.exit(e.code) |
| 188 | + except KeyboardInterrupt: |
| 189 | + LOGGER.info("(130) Halted via KeyboardInterrupt.") |
| 190 | + sys.exit(130) |
| 191 | + except Exception: |
| 192 | + LOGGER.exception(f"(1) Unhandled exception: {traceback.format_exc()}") |
| 193 | + sys.exit(1) |
0 commit comments