diff --git a/aim/digifeeds/functions.py b/aim/digifeeds/functions.py index 97613a4..a336c3f 100644 --- a/aim/digifeeds/functions.py +++ b/aim/digifeeds/functions.py @@ -1,6 +1,8 @@ from aim.services import S import boto3 from pathlib import Path +from rclone_python import rclone +from datetime import datetime def list_barcodes_in_input_bucket(): @@ -13,3 +15,17 @@ def list_barcodes_in_input_bucket(): response = s3.list_objects_v2(Bucket=S.digifeeds_s3_bucket, Prefix=prefix) barcodes = [Path(object["Key"]).stem for object in response["Contents"]] return barcodes + + +def barcodes_added_in_last_two_weeks(): + files = rclone.ls( + path=f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_input_path}", + args=["--max-age 14d"], + ) + output = [] + for file in files: + barcode = file["Name"].split(".")[0] + date = datetime.fromisoformat(file["ModTime"]).strftime("%Y-%m-%d") + output.append([date, barcode]) + + return output diff --git a/tests/digifeeds/test_functions.py b/tests/digifeeds/test_functions.py new file mode 100644 index 0000000..4376c98 --- /dev/null +++ b/tests/digifeeds/test_functions.py @@ -0,0 +1,33 @@ +import json +from aim.digifeeds.functions import rclone, barcodes_added_in_last_two_weeks + + +def test_barcodes_added_in_last_two_weeks(mocker): + ls_data_raw = """ + [ + { + "Path": "35112203951670.zip", + "Name": "35112203951670.zip", + "Size": 554562627, + "MimeType": "application/zip", + "ModTime": "2024-12-14T02:01:05.093051502-05:00", + "IsDir": false, + "Tier": "STANDARD" + }, + { + "Path": "39015004707009.zip", + "Name": "39015004707009.zip", + "Size": 232895588, + "MimeType": "application/zip", + "ModTime": "2024-12-14T02:02:29.111076546-05:00", + "IsDir": false, + "Tier": "STANDARD" + } + ] +""" + mocker.patch.object(rclone, "ls", return_value=json.loads(ls_data_raw)) + output = barcodes_added_in_last_two_weeks() + assert output == [ + ["2024-12-14", "35112203951670"], + ["2024-12-14", "39015004707009"], + ]