-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathdownload_md.sh
More file actions
executable file
·49 lines (44 loc) · 1.64 KB
/
download_md.sh
File metadata and controls
executable file
·49 lines (44 loc) · 1.64 KB
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
figi_list=figi.txt
token=inset_token_here
minimum_year=2017
current_year=$(date +%Y)
url=https://invest-public-api.tinkoff.ru/history-data
function download {
local figi=$1
local year=$2
# выкачиваем все архивы с текущего до 2017 года
if [ "$year" -lt "$minimum_year" ]; then
return 0
fi
local file_name=${figi}_${year}.zip
echo "downloading $figi for year $year"
local response_code=$(curl -s --location "${url}?figi=${figi}&year=${year}" \
-H "Authorization: Bearer ${token}" -o "${file_name}" -w '%{http_code}\n')
# Если превышен лимит запросов в минуту (30) - повторяем запрос.
if [ "$response_code" = "429" ]; then
echo "rate limit exceed. sleep 5"
sleep 5
download "$figi" "$year";
return 0
fi
# Если невалидный токен - выходим.
if [ "$response_code" = "401" ] || [ "$response_code" = "500" ]; then
echo 'invalid token'
exit 1
fi
# Если данные по инструменту за указанный год не найдены.
if [ "$response_code" = "404" ]; then
echo "data not found for figi=${figi}, year=${year}, removing empty file"
# Удаляем пустой архив.
rm -rf $file_name
elif [ "$response_code" != "200" ]; then
# В случае другой ошибки - просто напишем ее в консоль и выйдем.
echo "unspecified error with code: ${response_code}"
exit 1
fi
((year--))
download "$figi" "$year";
}
while read -r figi; do
download "$figi" "$current_year"
done < ${figi_list}