-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.sh
executable file
·145 lines (120 loc) · 3.74 KB
/
transactions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
function join {
local IFS="$1"
shift
echo "$*"
}
function printgold()
{
local copper=$(($1 % 100))
local silver=$(($(($(($1 - copper)) / 100)) % 100))
local gold=$(($(($(($(($1 - copper)) / 100)) - silver)) / 100))
printf "\e[33m"
[[ $gold -ne 0 ]] && printf "%dg " "$gold"
printf "\e[37m"
[[ $1 -ge 100 ]] && ([[ $1 -ge 10000 ]] && printf "%02ds " "$silver" || printf "%ds " "$silver")
printf "\e[31m"
[[ $1 -ge 100 ]] && printf "%02dc" "$copper" || printf "%dc" "$copper"
printf "\e[0m"
}
function getSaleBuyList()
{
l="$(printf "=%.0s" {1..80})"
printf "%s\n%s\n%s\n" "$l" "$1" "$l"
apikey=$(cat gw2apikey)
buys=$(curl "https://api.guildwars2.com/v2/commerce/transactions/${1}?access_token=${apikey}" 2>/dev/null)
prices=($(echo "${buys}" | jq '.[].price'))
amount=($(echo "${buys}" | jq '.[].quantity'))
itemid=($(echo "${buys}" | jq '.[].item_id'))
total=0
search=$(join , "${itemid[@]}" | uniq)
items=$(curl "https://api.guildwars2.com/v2/items?ids=$search" 2>/dev/null)
pricelist=$(curl "https://api.guildwars2.com/v2/commerce/prices?ids=$search" 2>/dev/null)
IFS=$'\n'
for (( i = 0; i < "${#itemid[@]}"; i++ )); do
curr=${itemid[$i]}
name=$(echo "$items" | jq -r ".[] | select(.id == $curr)| .name")
count=${amount[$i]}
price=${prices[$i]}
instantprice=$(echo "$pricelist" | \
jq -r ".[] | select(.id == $curr)| .buys.unit_price")
slowprice=$(echo "$pricelist" | \
jq -r ".[] | select(.id == $curr)| .sells.unit_price")
thisprice=$((count * price))
printf "%-4s %-40s %32s each:%32s buyorder: %32s sell: %32s\n" \
"${amount[$i]}x" \
"$name" \
"$(printgold "$thisprice")" \
"$(printgold "$price")" \
"$(printgold "$instantprice")" \
"$(printgold "$slowprice")"
total=$((total + thisprice))
done
echo "total $(printgold $total)"
}
function getDelivery()
{
apikey=$(cat gw2apikey)
delivery=$(curl "https://api.guildwars2.com/v2/commerce/delivery?access_token=${apikey}" 2>/dev/null)
coins=$(echo "${delivery}" | jq '.coins')
amount=($(echo "${delivery}" | jq '.items[].count'))
itemid=($(echo "${delivery}" | jq '.items[].id'))
if [[ $coins -ne 0 || ${#itemid[@]} -ne 0 ]]; then
l="$(printf "=%.0s" {1..80})"
printf "%s\n%s\n%s\n" "$l" "waiting for pickup" "$l"
fi
search=$(join , "${itemid[@]}" | uniq)
items=$(curl "https://api.guildwars2.com/v2/items?ids=$search" 2>/dev/null)
IFS=$'\n'
for (( i = 0; i < "${#itemid[@]}"; i++ )); do
curr=${itemid[$i]}
name=$(echo "$items" | jq -r ".[] | select(.id == $curr)| .name")
count=${amount[$i]}
printf "%-4s %-40s\n" \
"${amount[$i]}x" \
"$name"
done
if [[ $coins -ne 0 ]]; then
echo "$(printgold $coins)"
fi
}
function printhelp
{
echo "valid options"
echo "-c = current instead of history"
echo "-s = show sales/for sale"
echo "-b = show bought/buy orders"
}
[[ $# -eq 0 ]] && printhelp && exit 0
scope="history"
while getopts "hcsb" c;do
case "$c" in
h)
printhelp
exit 0
;;
c)
scope="current"
;;
s)
showsales=1
;;
b)
showbuys=1
;;
\?)
echo "invalid option $c"
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [[ $showsales -eq 1 ]]; then
getSaleBuyList "${scope}/sells"
fi
if [[ $showbuys -eq 1 ]]; then
getSaleBuyList "${scope}/buys"
fi
getDelivery