Skip to content

Commit da092b7

Browse files
committed
feat: improve bash scripts
1 parent df8559f commit da092b7

File tree

4 files changed

+163
-67
lines changed

4 files changed

+163
-67
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ Options:
4646
Print help
4747
```
4848

49-
3. You can also use the small bash script `pull_all.sh` that uses the `vegapull` CLI to download data for all existing packs:
49+
3. You can also use the example bash scripts that directly use the `vegapull` CLI to download data for all existing packs:
5050
```console
51-
coko7@example:~$ bash pull_all.sh
51+
coko7@example:~$ bash scripts/pull-all.sh
52+
# the `gum` one is more complete but requires some additional tooling to install in your shell:
53+
coko7@example:~$ bash scripts/pull-all-gum.sh
5254
```
5355

5456
## 🃏 Supported card fields

pull_all.sh

Lines changed: 0 additions & 65 deletions
This file was deleted.

scripts/pull-all-gum.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env bash
2+
3+
LANGUAGES="🇬🇧 english\n🇯🇵 japanese"
4+
language=$(echo -e "$LANGUAGES" | gum choose --header="Language:" | cut -d' ' -f2)
5+
if [ -z "$language" ]; then
6+
exit 1
7+
fi
8+
9+
default_data_dir="data/$language"
10+
data_dir=$(gum input \
11+
--value="$default_data_dir" \
12+
--placeholder="Enter a path..." \
13+
--prompt="📦 TCG Download location> ")
14+
if [ -z "$data_dir" ]; then
15+
exit 1
16+
fi
17+
18+
echo "📦 TCG Download location: $data_dir"
19+
20+
if [ -d "$data_dir" ]; then
21+
if ! gum confirm "📁 Directory '$data_dir' is about to be wiped to hold new data, do you want to proceed?"; then
22+
exit 1
23+
fi
24+
25+
rm -rf "$data_dir"
26+
fi
27+
28+
mkdir "$data_dir"
29+
echo -e "📁 Created directory: $data_dir\n"
30+
31+
if ! gum spin --title="VegaPull packs..." -- vegapull --language "$language" packs > "$data_dir/packs.json"; then
32+
echo "Failed to pull list of packs using vegapull. Aborted" >&2
33+
exit 1
34+
fi
35+
36+
pack_count=$(jq length "$data_dir"/packs.json)
37+
38+
echo -e "✅ Successfully found $pack_count packs!\n"
39+
40+
function pull_cards() {
41+
local index=1
42+
local packs
43+
packs=$(cat "$data_dir/packs.json")
44+
45+
while read -r pack_id; do
46+
pack_title=$(echo "$packs" | jq --arg pack_id "$pack_id" -r '.[] | select(.id == $pack_id) | .title_parts.title')
47+
message="[$index/$pack_count] VagaPulling cards for: $pack_title ($pack_id)..."
48+
49+
if ! gum spin --title="$message" -- \
50+
vegapull --language "$language" cards "$pack_id" > "$data_dir/cards_$pack_id.json"; then
51+
echo "Failed to pull cards using vegapull. Aborted" >&2
52+
return 1
53+
else
54+
echo "[$index/$pack_count] Succesfully VegaPulled cards for: $pack_title ($pack_id) ✅"
55+
fi
56+
57+
((index++))
58+
done < <( echo "$packs" | jq -r '.[].id')
59+
60+
echo "✅ Successfully downloaded data for $index packs!"
61+
}
62+
63+
if ! pull_cards; then
64+
exit 1
65+
fi
66+
67+
function download_images() {
68+
local index=1
69+
local packs
70+
packs=$(cat "$data_dir/packs.json")
71+
72+
while read -r pack_id; do
73+
local output_dir="$data_dir/images/$pack_id"
74+
pack_title=$(echo "$packs" | jq --arg pack_id "$pack_id" -r '.[] | select(.id == $pack_id) | .title_parts.title')
75+
echo "[$index/$pack_count] VagaPulling images for: $pack_title ($pack_id)..."
76+
77+
if ! vegapull --language "$language" images --output-dir="$output_dir" "$pack_id" -vv; then
78+
echo "Failed to pull images using vegapull. Aborted" >&2
79+
return 1
80+
else
81+
echo "[$index/$pack_count] Succesfully VegaPulled images for: $pack_title ($pack_id) ✅"
82+
fi
83+
84+
((index++))
85+
done < <( echo "$packs" | jq -r '.[].id')
86+
87+
echo "✅ Successfully downloaded data for $index packs!"
88+
89+
}
90+
91+
if gum confirm "🖼️ Download card images as well?"; then
92+
download_images
93+
fi
94+
95+
echo "✅ Successfully filled the punk records with latest data"

scripts/pull-all.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
LANGUAGE="japanese"
4+
VEGA_DATA=data/$LANGUAGE
5+
6+
if [ -d "$VEGA_DATA" ]; then
7+
read -rp "The $VEGA_DATA is about to be wiped to hold the new data, do you want to proceed? (y/N) " confirm
8+
case $confirm in
9+
[Yy]* ) ;;
10+
* ) echo "Aborted" >&2; exit 1 ;;
11+
esac
12+
13+
rm -rf $VEGA_DATA
14+
fi
15+
16+
mkdir $VEGA_DATA
17+
echo -e "Created dir: $VEGA_DATA\n"
18+
19+
echo "VegaPulling the list of packs ($LANGUAGE)..."
20+
21+
if ! ./target/release/vegapull --language $LANGUAGE packs > $VEGA_DATA/packs.json; then
22+
echo "Failed to pull list of packs using vegapull. Aborted" >&2
23+
exit 1
24+
fi
25+
26+
count=$(jq length $VEGA_DATA/packs.json)
27+
28+
echo -e "Successfully pulled $count packs!\n"
29+
30+
function pull_cards() {
31+
local index=1
32+
local packs
33+
packs=$(cat $VEGA_DATA/packs.json)
34+
35+
while read -r id; do
36+
echo -n "[$index/$count] VagaPulling cards for pack '$id'..."
37+
if ! ./target/release/vegapull --language $LANGUAGE cards "$id" > "$VEGA_DATA/cards_$id.json"; then
38+
echo "Failure"
39+
echo "Failed to pull cards using vegapull. Aborted" >&2
40+
return 1
41+
fi
42+
43+
echo " OK"
44+
((index++))
45+
done < <( echo "$packs" | jq -r '.[].id')
46+
47+
echo "Successfully download data for $index packs!"
48+
}
49+
50+
if ! pull_cards; then
51+
exit 1
52+
fi
53+
54+
function download_images() {
55+
echo "NOT IMPLEMENTED YET"
56+
}
57+
58+
read -rp "Download card images as well? (y/N) " confirm
59+
case $confirm in
60+
[Yy]* ) download_images ;;
61+
* ) ;;
62+
esac
63+
64+
echo "Successfully filled the punk records with latest data"

0 commit comments

Comments
 (0)