-
-
Notifications
You must be signed in to change notification settings - Fork 71
Add Smithsonian fetch #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimidRobot
wants to merge
3
commits into
main
Choose a base branch
from
smithsonian
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| #!/usr/bin/env python | ||
| """ | ||
| Fetch metrics usage from Smithsonian Institution Open Access API. | ||
| """ | ||
|
|
||
| # Standard library | ||
| import argparse | ||
| import csv | ||
| import os | ||
| import sys | ||
| import textwrap | ||
| import traceback | ||
| from operator import itemgetter | ||
|
|
||
| # Third-party | ||
| import requests | ||
| from pygments import highlight | ||
| from pygments.formatters import TerminalFormatter | ||
| from pygments.lexers import PythonTracebackLexer | ||
|
|
||
| # Add parent directory so shared can be imported | ||
| sys.path.append(os.path.join(os.path.dirname(__file__), "..")) | ||
|
|
||
| # First-party/Local | ||
| import shared # noqa: E402 | ||
|
|
||
| # Setup | ||
| LOGGER, PATHS = shared.setup(__file__) | ||
|
|
||
| # Constants | ||
| DATA_GOV_API_KEY = os.getenv("DATA_GOV_API_KEY") | ||
| FILE_1_METRICS = os.path.join(PATHS["data_phase"], "smithsonian_1_metrics.csv") | ||
| FILE_2_UNITS = os.path.join(PATHS["data_phase"], "smithsonian_2_units.csv") | ||
| HEADER_1_METRICS = [ | ||
| "CC0_RECORDS", | ||
| "CC0_RECORDS_WITH_CC0_MEDIA", | ||
| "CC0_MEDIA", | ||
| "CC0_MEDIA_PERCENTAGE", | ||
| "TOTAL_OBJECTS", | ||
| ] | ||
| HEADER_2_UNITS = [ | ||
| "UNIT", | ||
| "CC0_RECORDS", | ||
| "CC0_RECORDS_WITH_CC0_MEDIA", | ||
| "TOTAL_OBJECTS", | ||
| ] | ||
| QUARTER = os.path.basename(PATHS["data_quarter"]) | ||
|
|
||
|
|
||
| def parse_arguments(): | ||
| """ | ||
| Parse command-line options, returns parsed argument namespace. | ||
| """ | ||
| LOGGER.info("Parsing command-line options") | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--enable-save", | ||
| action="store_true", | ||
| help="Enable saving results", | ||
| ) | ||
| parser.add_argument( | ||
| "--enable-git", | ||
| action="store_true", | ||
| help="Enable git actions (fetch, merge, add, commit, and push)", | ||
| ) | ||
| args = parser.parse_args() | ||
| if not args.enable_save and args.enable_git: | ||
| parser.error("--enable-git requires --enable-save") | ||
| return args | ||
|
|
||
|
|
||
| def check_for_completion(): | ||
| completed_metrics = False | ||
| completed_units = False | ||
|
|
||
| try: | ||
| with open(FILE_1_METRICS, "r", newline="") as file_obj: | ||
| reader = csv.DictReader(file_obj, dialect="unix") | ||
| if len(list(reader)) > 0: | ||
| completed_metrics = True | ||
| except FileNotFoundError: | ||
| pass # File may not be found without --enable-save, etc. | ||
|
|
||
| try: | ||
| with open(FILE_2_UNITS, "r", newline="") as file_obj: | ||
| reader = csv.DictReader(file_obj, dialect="unix") | ||
| if len(list(reader)) > 30: | ||
| completed_units = True | ||
| except FileNotFoundError: | ||
| pass # File may not be found without --enable-save, etc. | ||
|
|
||
| if completed_metrics and completed_units: | ||
| raise shared.QuantifyingException( | ||
| f"Data fetch completed for {QUARTER}", 0 | ||
| ) | ||
|
|
||
|
|
||
| def write_data(args, data_metrics, data_units): | ||
| if not args.enable_save: | ||
| return args | ||
|
|
||
| # Create data directory for this phase | ||
| os.makedirs(PATHS["data_phase"], exist_ok=True) | ||
|
|
||
| with open(FILE_1_METRICS, "w", encoding="utf-8", newline="\n") as file_obj: | ||
| writer = csv.DictWriter( | ||
| file_obj, fieldnames=HEADER_1_METRICS, dialect="unix" | ||
| ) | ||
| writer.writeheader() | ||
| for row in data_metrics: | ||
| writer.writerow(row) | ||
|
|
||
| with open(FILE_2_UNITS, "w", encoding="utf-8", newline="\n") as file_obj: | ||
| writer = csv.DictWriter( | ||
| file_obj, fieldnames=HEADER_2_UNITS, dialect="unix" | ||
| ) | ||
| writer.writeheader() | ||
| for row in data_units: | ||
| writer.writerow(row) | ||
|
|
||
| return args | ||
|
|
||
|
|
||
| def query_smithsonian(args, session): | ||
| if not DATA_GOV_API_KEY: | ||
| raise shared.QuantifyingException( | ||
| "Authentication (DATA_GOV_API_KEY) required. Please ensure your" | ||
| " API key is set in .env", | ||
| 1, | ||
| ) | ||
| LOGGER.info("Fetch data from API") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be LOGGER.info("Fetching media and record counts from internet archive") |
||
| url = "https://api.si.edu/openaccess/api/v1.0/stats" | ||
| params = {"api_key": DATA_GOV_API_KEY} | ||
| try: | ||
| with session.get(url, params=params) as response: | ||
| response.raise_for_status() | ||
| data = response.json()["response"] | ||
| except requests.HTTPError as e: | ||
| raise shared.QuantifyingException(f"HTTP Error: {e}", 1) | ||
| except requests.RequestException as e: | ||
| raise shared.QuantifyingException(f"Request Exception: {e}", 1) | ||
| except KeyError as e: | ||
| raise shared.QuantifyingException(f"KeyError: {e}", 1) | ||
| data_metrics = [ | ||
| { | ||
| "CC0_MEDIA": data["metrics"]["CC0_media"], | ||
| "CC0_MEDIA_PERCENTAGE": data["metrics"]["CC0_media_percentage"], | ||
| "CC0_RECORDS": data["metrics"]["CC0_records"], | ||
| "CC0_RECORDS_WITH_CC0_MEDIA": data["metrics"][ | ||
| "CC0_records_with_CC0_media" | ||
| ], | ||
| "TOTAL_OBJECTS": data["total_objects"], | ||
| } | ||
| ] | ||
| data_units = [] | ||
| for unit in data["units"]: | ||
| if unit["total_objects"] == 0: | ||
| continue | ||
| data_units.append( | ||
| { | ||
| "UNIT": unit["unit"], | ||
| "CC0_RECORDS": unit["metrics"]["CC0_records"], | ||
| "CC0_RECORDS_WITH_CC0_MEDIA": unit["metrics"][ | ||
| "CC0_records_with_CC0_media" | ||
| ], | ||
| "TOTAL_OBJECTS": unit["total_objects"], | ||
| } | ||
| ) | ||
| data_units = sorted(data_units, key=itemgetter("UNIT")) | ||
| LOGGER.info(f"Fetched stats for {len(data_units)} units") | ||
| return data_metrics, data_units | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_arguments() | ||
| shared.paths_log(LOGGER, PATHS) | ||
| check_for_completion() | ||
| session = shared.get_session() | ||
| data_metrics, data_units = query_smithsonian(args, session) | ||
| args = write_data(args, data_metrics, data_units) | ||
| args = shared.git_add_and_commit( | ||
| args, | ||
| PATHS["repo"], | ||
| PATHS["data_quarter"], | ||
| f"Add and commit new Smithsonian data for {QUARTER}", | ||
| ) | ||
| shared.git_push_changes(args, PATHS["repo"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| main() | ||
| except shared.QuantifyingException as e: | ||
| if e.exit_code == 0: | ||
| LOGGER.info(e.message) | ||
| else: | ||
| LOGGER.error(e.message) | ||
| sys.exit(e.exit_code) | ||
| except SystemExit as e: | ||
| if e.code != 0: | ||
| LOGGER.error(f"System exit with code: {e.code}") | ||
| sys.exit(e.code) | ||
| except KeyboardInterrupt: | ||
| LOGGER.info("(130) Halted via KeyboardInterrupt.") | ||
| sys.exit(130) | ||
| except Exception: | ||
| traceback_formatted = textwrap.indent( | ||
| highlight( | ||
| traceback.format_exc(), | ||
| PythonTracebackLexer(), | ||
| TerminalFormatter(), | ||
| ), | ||
| " ", | ||
| ) | ||
| LOGGER.critical(f"(1) Unhandled exception:\n{traceback_formatted}") | ||
| sys.exit(1) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why is the required line 0 here? and the one for units is 30?