poke/backend-services/scripts/inv-restart-docker.sh

136 lines
4.7 KiB
Bash
Raw Normal View History

#!/bin/bash
generate_random_chrome_version() {
major=$((RANDOM % 100 + 1)) # Major version 1-99
minor=$((RANDOM % 100)) # Minor version 0-99
build=$((RANDOM % 10000)) # Build version 0-9999
patch=$((RANDOM % 100)) # Patch version 0-99
echo "$major.$minor.$build.$patch"
}
restart_services() {
echo "Restarting services..."
# Navigate to the script directory
scriptDir=$(dirname "$(readlink -f "$0")")
cd "$scriptDir/../services/invidious" || { echo "Error: Failed to navigate to $scriptDir/../services/invidious"; exit 1; }
docker compose down
echo "Services stopped. Restarting..."
echo "Services restarted successfully."
/home/qt/globe/scripts/inv-update-token.sh
}
fetch_playlist() {
local playlist_id="$1"
response=$(curl -s -w "%{http_code}" -o /tmp/playlist_data.json "https://invid-api.poketube.fun/api/v1/playlists/${playlist_id}")
if [ "$response" -eq 502 ] || [ "$response" -eq 500 ]; then
echo "Error: Failed to fetch playlist data. HTTP Status: $response"
restart_services
return 1
elif [ "$response" -ne 200 ]; then
echo "Error: Failed to fetch playlist data. HTTP Status: $response"
return 1
fi
}
extract_video_ids() {
local json_data="$1"
video_ids=$(jq -r '.videos[].videoId' "$json_data")
if [ -z "$video_ids" ]; then
echo "Error: Failed to extract video IDs from the playlist data."
return 1
fi
echo "$video_ids"
}
playlist_ids=("PLMws9SCqJ1JCeVMVPsdamuUM0HK0MbA6g")
# Base URL for the API
base_url="http://localhost:54301/latest_version?id="
random_playlist_id=("PLMC9KNkIncKvYin_USF1qoJQnIyMAfRxl")
echo "Randomly selected playlist: $random_playlist_id"
fetch_playlist "$random_playlist_id"
if [ $? -ne 0 ]; then
echo "Error: Playlist fetch failed. Restarting services..."
restart_services # Restart services before exiting
exit 1
fi
video_ids=($(extract_video_ids "/tmp/playlist_data.json"))
if [ $? -ne 0 ]; then
echo "Error: Failed to extract video IDs. Exiting..."
exit 1
fi
shuffled_video_ids=($(shuf -e "${video_ids[@]}" | head -n 4))
error_count=0
all_errors=(500 502)
# Loop through the selected random videos and check for errors
for video_id in "${shuffled_video_ids[@]}"; do
# Add a cache buster query (unique random number)
unique_param=$RANDOM
url="${base_url}${video_id}&itag=18&local=true&_=${unique_param}"
chrome_version=$(generate_random_chrome_version)
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$chrome_version Safari/537.36"
response_headers=$(curl -s -D - -H "Cache-Control: no-cache, no-store, must-revalidate" \
-H "Pragma: no-cache" -H "Expires: 0" -A "$user_agent" "$url" -o /dev/null)
# Extract ETag and last modified info (if available)
etag=$(echo "$response_headers" | grep -i ETag | awk '{print $2}' | tr -d '"')
last_modified=$(echo "$response_headers" | grep -i Last-Modified | cut -d' ' -f2-)
# Use conditional request if ETag is present
if [ -n "$etag" ]; then
status_code=$(curl -s -o /dev/null -w "%{http_code}" -H "If-None-Match: $etag" \
-H "Cache-Control: no-cache, no-store, must-revalidate" -A "$user_agent" "$url")
else
status_code=$(curl -s -o /dev/null -w "%{http_code}" -A "$user_agent" "$url")
fi
echo "Checking URL: $url"
echo "User Agent: $user_agent"
echo "HTTP Status Code for ID $video_id: $status_code"
if [[ " ${all_errors[@]} " =~ " ${status_code} " ]]; then
echo "Error: Received $status_code for ID $video_id."
error_count=$((error_count + 1))
echo "Running inv-update-token.sh for ID $video_id..."
/home/qt/globe/scripts/inv-update-token.sh
echo "inv-update-token.sh script executed successfully."
status_code=$(curl -s -o /dev/null -w "%{http_code}" -A "$user_agent" "$url")
echo "Post-token-refresh Status Code for ID $video_id: $status_code"
if [[ " ${all_errors[@]} " =~ " ${status_code} " ]]; then
echo "Error: Received $status_code for ID $video_id after token refresh."
else
echo "Token refresh succeeded for ID $video_id."
fi
elif [ "$status_code" -eq 304 ]; then
echo "Content is still fresh for ID $video_id. No action required."
else
echo "we are so barack (Status code for ID $video_id is neither 502 nor 500.)"
fi
echo "----------------------------------------" # Separator for readability
done
if [ "$error_count" -eq "${#shuffled_video_ids[@]}" ]; then
echo "All videos failed to load after running inv-update-token.sh. Restarting services..."
restart_services
fi