-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger_get-indian-stock-prices_bse.sh
45 lines (40 loc) · 1.12 KB
/
ledger_get-indian-stock-prices_bse.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
#!/usr/bin/env bash
ledger_dir=~/accounts
rates_file=$ledger_dir/rates_stocks.journal
url="https://www.bseindia.com/download/BhavCopy/Equity/"
date=$(date "+%d%m%y")
n=0
print_rates() {
gunzip </tmp/EQ_ISINCODE_${date}.zip \
| grep -f /tmp/hledger-stock-commodities \
| sort \
| tr -d '\r' \
| awk -F"," '{printf("\"%s\";%s;%s;",$15,"₹"$8,$2);system("date -d "$16" -I");}' \
| awk -F";" '{print "P",$4,$1,$2 "\t"" ; "$3}' \
| sponge -a $rates_file
}
hledger commodities | grep -E '^IN.[0-9]+' > /tmp/hledger-stock-commodities
if [ -s /tmp/EQ_ISINCODE_${date}.zip ]; then
tail -n 10 $rates_file
exit
else
while [ $n -gt -1 ]; do
date=$(date -d "- $n days" "+%d%m%y")
full_url="$url/EQ_ISINCODE_${date}.zip"
curl_opts=( --silent --location --output-dir "/tmp" --fail --remote-name)
code=$(curl -w "%{http_code}" "${curl_opts[@]}" "$full_url")
if [[ $code = 200 ]]; then
n=-1
echo $full_url
echo $code
else
((n=n+1))
echo $full_url
echo $code
fi
done
fi
print_rates
awk ' !x[$0]++' $rates_file \
| sponge $rates_file
tail -n 10 $rates_file