53 lines
1.5 KiB
Bash
53 lines
1.5 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
|
||
|
|
# Setup logging
|
||
|
|
function setup_logging() {
|
||
|
|
LOG_DIR="$ROOT_DIR/logs"
|
||
|
|
mkdir -p "$LOG_DIR"
|
||
|
|
SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}" .sh)
|
||
|
|
LOG_FILE="$LOG_DIR/$(date +%Y%m%d).log"
|
||
|
|
}
|
||
|
|
|
||
|
|
function timestamp() {
|
||
|
|
date "+%Y-%m-%d %H:%M:%S"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Function to handle cleanup and exit
|
||
|
|
function cleanup() {
|
||
|
|
# Kill all child processes of this script
|
||
|
|
pkill -P $$
|
||
|
|
exit $1
|
||
|
|
}
|
||
|
|
|
||
|
|
# Function to handle requests and responses
|
||
|
|
function handle_vk_request() {
|
||
|
|
local request_name=$1
|
||
|
|
local request_url=$2
|
||
|
|
local path=$3
|
||
|
|
local fileName=$4
|
||
|
|
local additional_params=$5
|
||
|
|
local debug_response=${6:-true}
|
||
|
|
|
||
|
|
echo "$(timestamp) [REQUEST] Getting $request_name" >> "$LOG_FILE"
|
||
|
|
response=$(curl -s -w "%{http_code}" -H "Authorization: Bearer $VK_API_USER_TOKEN" \
|
||
|
|
-X GET \
|
||
|
|
"$request_url$additional_params")
|
||
|
|
http_code=${response: -3}
|
||
|
|
response_body=${response:0:-3}
|
||
|
|
if [ "$http_code" = "200" ]; then
|
||
|
|
if ! echo "$response_body" | jq -e 'has("error")' > /dev/null; then
|
||
|
|
touch "$path/$fileName"
|
||
|
|
echo "$response_body" > "$path/$fileName"
|
||
|
|
else
|
||
|
|
error_msg=$(echo "$response_body" | jq -r '.error.error_msg')
|
||
|
|
echo "$(timestamp) [ERROR] $error_msg" >> "$LOG_FILE"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ "$debug_response" = true ]; then
|
||
|
|
echo "$(timestamp) [RESPONSE] code=$http_code body=$response_body" >> "$LOG_FILE"
|
||
|
|
else
|
||
|
|
echo "$(timestamp) [RESPONSE] code=$http_code" >> "$LOG_FILE"
|
||
|
|
fi
|
||
|
|
}
|