Skip to content

Commit 119dafe

Browse files
committed
Add database artifact logic to dev action and main workflow
1 parent ac2a5cb commit 119dafe

2 files changed

Lines changed: 69 additions & 4 deletions

File tree

.github/workflows/dev.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
- '!master'
88
workflow_dispatch:
99

10+
permissions:
11+
actions: read
12+
1013
jobs:
1114
run-cute-pets:
1215
runs-on: ubuntu-latest
@@ -21,6 +24,27 @@ jobs:
2124
- name: Install dependencies
2225
run: pip install --break-system-packages -r requirements.txt
2326

27+
- name: Get Previous Run ID
28+
id: get_id
29+
env:
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
run: |
32+
# Fetches the ID of the last completed run for the current workflow
33+
PREVIOUS_RUN_ID=$(gh run list --workflow "${{ github.workflow }}" \
34+
--status success \
35+
--limit 1 \
36+
--json databaseId \
37+
--jq '.[0].databaseId')
38+
echo "previous_run_id=$PREVIOUS_RUN_ID" >> "$GITHUB_OUTPUT"
39+
40+
- name: Download previous database artifact
41+
uses: actions/download-artifact@v8
42+
with:
43+
name: database.json
44+
github-token: ${{ secrets.GITHUB_TOKEN }}
45+
run-id: ${{ steps.get_id.outputs.previous_run_id }}
46+
continue-on-error: true
47+
2448
- name: Call RescueGroups API
2549
env:
2650
CUTEPETSBOSTON_RESCUEGROUPS_API_KEY: ${{ secrets.CUTEPETSBOSTON_RESCUEGROUPS_API_KEY }}
@@ -31,3 +55,10 @@ jobs:
3155
run: |
3256
#In order to create posts on the test accounts remove the --debugposters debug flag
3357
python ./main.py --debugsources --debugposters
58+
59+
- name: Upload database artifact
60+
uses: actions/upload-artifact@v7
61+
with:
62+
path: database.json
63+
retention-days: 14
64+
archive: false

main.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import random
22
import argparse
3+
import json
4+
import sys
5+
import traceback
6+
from pathlib import Path
7+
from datetime import datetime, timezone, timedelta
38

49
def main():
510
parser = argparse.ArgumentParser()
@@ -74,10 +79,39 @@ def run(sources, posters):
7479

7580

7681
def pick_pet(pets):
77-
eligible = [pet for pet in pets if pet.image_url and pet.adoption_url]
78-
if not eligible:
79-
return None
80-
return random.choice(eligible)
82+
Path("database.json").touch(exist_ok=True)
83+
# Open file
84+
with open("database.json", "r+") as f:
85+
# Load json
86+
try:
87+
data = json.load(f)
88+
except (json.JSONDecodeError, ValueError) as e:
89+
print(f"{type(e).__name__}:{e}", file=sys.stderr)
90+
traceback.print_exc()
91+
data = {}
92+
93+
if "posted_pets" in data:
94+
posted_pet_ids = {pet.pet_id for pet in data["posted_pets"]}
95+
else:
96+
pet_ids = {}
97+
data["posted_pets"] = []
98+
# Check pet has an image, adoption url, and has not been posted
99+
eligible = [pet for pet in pets if pet.image_url and pet.adoption_url and pet.pet_id not in posted_pet_ids]
100+
if not eligible:
101+
return None
102+
103+
selected_pet = random.choice(eligible)
104+
# Add pet ID to list of posted pets
105+
data["posted_pets"].append({"name": selected_pet.name, "pet_id": selected_pet.pet_id, "time": datetime.now(timezone.utc).isoformat()})
106+
# Remove old pets
107+
cutoff = datetime.now() - timedelta(weeks=12)
108+
new_pets = [item for item in data["posted_pets"] if datetime.fromisoformat(item['time']) > cutoff]
109+
data["posted_pets"] = new_pets
110+
# Export json
111+
f.seek(0)
112+
json.dump(data, f)
113+
f.truncate()
114+
return selected_pet
81115

82116

83117
if __name__ == "__main__":

0 commit comments

Comments
 (0)