-
Notifications
You must be signed in to change notification settings - Fork 0
AP-687 Only download files if local file is older #13
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44738a0
AP-687
jason-raitz 4ce7ca2
Update tind_client/client.py
jason-raitz 5d5f9a9
Apply suggestions from code review
jason-raitz 651654e
line too long
jason-raitz 823111e
changes from code reviews
jason-raitz 3ec2ff6
fixed url filename parsing
jason-raitz 7e18eb9
version bump
jason-raitz 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 |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ | |
| """ | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import re | ||
| from datetime import datetime, timezone | ||
| from io import StringIO | ||
| from pathlib import Path | ||
| from typing import Any, Iterator | ||
|
|
@@ -16,6 +18,7 @@ | |
| from .api import tind_get, tind_download | ||
| from .errors import RecordNotFoundError, TINDError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| NS = "http://www.loc.gov/MARC21/slim" | ||
| E.register_namespace("", NS) | ||
|
|
@@ -69,12 +72,17 @@ def fetch_metadata(self, record: str) -> Record: | |
|
|
||
| return records[0] | ||
|
|
||
| def fetch_file(self, file_url: str, output_dir: str = "") -> str: | ||
| def fetch_file(self, file_url: str, output_dir: str = "", modified: str = "") -> str: | ||
|
Member
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. following @awilfox's suggestion, i think |
||
| """Download a file from TIND and save it locally. | ||
|
|
||
| If the file already exists in the output directory and was modified at or after a supplied | ||
| ``modified`` timestamp, the file will not be re-downloaded. | ||
|
|
||
| :param str file_url: The TIND file download URL. | ||
| :param str output_dir: Directory in which to save the file. | ||
| Falls back to ``default_storage_dir`` when empty. | ||
| :param str modified: Optional modified timestamp from the file metadata returned by TIND. | ||
| If not specified, the file will always be downloaded. | ||
| :raises AuthorizationError: When the TIND API key is invalid or the file is restricted. | ||
| :raises ValueError: When ``file_url`` is not a valid TIND file download URL. | ||
| :raises RecordNotFoundError: When the file is invalid or not found. | ||
|
|
@@ -84,9 +92,20 @@ def fetch_file(self, file_url: str, output_dir: str = "") -> str: | |
| raise ValueError("URL is not a valid TIND file download URL.") | ||
|
|
||
| output_target = output_dir or self.default_storage_dir | ||
|
|
||
| expected_filename = file_url.rstrip("/").split("/")[-2] | ||
| expected_path = Path(output_target) / expected_filename | ||
|
|
||
| if modified and expected_path.exists(): | ||
| meta_mtime = datetime.fromisoformat(modified).replace(tzinfo=timezone.utc) | ||
| local_mtime = datetime.fromtimestamp(expected_path.stat().st_mtime, tz=timezone.utc) | ||
| if local_mtime >= meta_mtime: | ||
| logger.debug("Cached file at (%s) is newer; skipping download.", expected_path) | ||
| return str(expected_path) | ||
|
|
||
| (status, saved_to) = tind_download(file_url, output_dir=output_target, api_key=self.api_key) | ||
|
|
||
| if status != 200: | ||
| if status != 200 or not saved_to: | ||
| raise RecordNotFoundError("Referenced file could not be downloaded.") | ||
|
|
||
| return saved_to | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.