-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #370 from COS301-SE-2024/dev/feat/update_birds
✨ Added automations needed to import birds from SABAB
- Loading branch information
Showing
8 changed files
with
167 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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,45 @@ | ||
name: 🦜 Update Bird data | ||
|
||
on: | ||
schedule: | ||
- cron: "0 8 * * *" | ||
workflow_dispatch: | ||
workflow_call: | ||
|
||
jobs: | ||
update_data: | ||
name: 🦜 Get bird data | ||
outputs: | ||
artifact_url: ${{ steps.artifact-upload-step.outputs.artifact-url}} | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
- uses: actions/checkout@v4 | ||
|
||
- name: 🪽 Run auto import script | ||
if: ${{ !env.ACT }} | ||
run: ./scripts/bash/auto_import.sh ./scripts/bash/provinces.txt ${{ secrets.DATA_URL }} | ||
|
||
- name: 🧪 Test auto update script | ||
if: ${{ env.ACT }} | ||
run: ./scripts/bash/auto_import.sh ./scripts/bash/provinces.txt ${{ secrets.DATA_URL }} ./res/species_list/south_africa.csv | ||
|
||
- uses: actions/upload-artifact@v4 | ||
id: artifact-upload-step | ||
with: | ||
name: bird-data | ||
path: | | ||
*.csv | ||
notify_discord: | ||
name: 🔔 Send Discord notification about deployment | ||
needs: [update_data] | ||
if: ${{ !cancelled() && (success() || failure()) }} | ||
uses: ./.github/workflows/discord.yml | ||
with: | ||
content: "${{ contains(needs.update_data.result, 'success') && 'Successfully updated bird data' || 'Error during update of' }} ${{ github.ref_name }} for bird data" | ||
title: "${{ contains(needs.update_data.result, 'success') && 'Successfully updated bird data' || 'Error during update of' }} ${{ github.ref_name }} for bird data" | ||
url: ${{ needs.update_data.outputs.artifact_url }} | ||
description: "${{ contains(needs.update_data.result, 'success') && 'Updated:' || 'Update failed:' }} ${{ github.event.head_commit.message }}" | ||
color: ${{ contains(needs.update_data.result, 'success') && 65280 || 16711680 }} | ||
secrets: inherit |
This file contains 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 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 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 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 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,58 @@ | ||
#!/bin/bash | ||
|
||
if [[ -z "$1" ]]; then | ||
echo "No proince list" | ||
exit 1 | ||
else | ||
PROVINCE_LIST="$1" | ||
echo "Given province list" | ||
fi | ||
|
||
if [[ -z "$2" ]]; then | ||
URL="http://localhost:5050" | ||
echo "Using default Url" | ||
else | ||
URL="$2" | ||
echo "Using given URL" | ||
fi | ||
|
||
BIRDS=birds.csv | ||
if [[ -z "$3" ]]; then | ||
echo "Getting birds" | ||
curl -L -o birds.csv "https://api.birdmap.africa/sabap2/v2/coverage/country/southafrica/species?format=csv" | ||
else | ||
IS_TEST=true | ||
BIRDS=$3 | ||
echo "Test mode" | ||
fi | ||
|
||
GOOD_BIRDS=$(awk -F, 'BEGIN {FS=","} {if ($6 > 0.01 ) print $0}' $BIRDS) | ||
echo "$GOOD_BIRDS" > good_birds.csv | ||
|
||
curl -X 'POST' \ | ||
"$URL/api/Import/importBirds" \ | ||
-H 'accept: */*' \ | ||
-H 'Content-Type: multipart/form-data' \ | ||
-F 'file=@good_birds.csv;type=text/csv' | ||
|
||
base_url="https://api.birdmap.africa/sabap2/v2/monthly/speciesbypentad/province/" | ||
|
||
set -o pipefail | ||
while IFS= read -r line | ||
do | ||
FULL_URL="${base_url}${line}?period=&dates=&format=csv" | ||
|
||
echo "Downloading ${line} CSV" | ||
|
||
if curl -L "$FULL_URL" -o ${line}.csv ; then | ||
if [[ IS_TEST ]]; then | ||
awk '{FS=","} {print } NR==10{exit}' ${line}.csv > ${line}_short.csv | ||
cat ${line}_short.csv | ||
fi | ||
curl -X 'POST' \ | ||
"$URL/api/Import/import?province=${line}" \ | ||
-H 'accept: */*' \ | ||
-H 'Content-Type: multipart/form-data' \ | ||
-F "file=@${line}_short.csv;type=text/csv" | ||
fi | ||
done < "$PROVINCE_LIST" |
This file contains 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