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

165 lines
6 KiB
Bash
Raw Normal View History

#!/bin/bash
2024-10-20 11:15:16 +02:00
#
# Copyright (C) 2024-20xx Poke! (https://codeberg.org/ashley/poke)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2024-10-22 15:54:47 +02:00
# Function to generate a random Chrome version
generate_random_chrome_version() {
major=$((RANDOM % 100 + 1)) # Major version 1-99
2024-10-22 15:54:47 +02:00
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")")
2024-10-22 15:54:47 +02:00
cd "$scriptDir/../services/invidious" || { echo "Error: Failed to navigate to $scriptDir/../services/invidious"; exit 1; }
docker compose down
echo "Services stopped. Restarting..."
2024-10-22 15:54:47 +02:00
docker compose up -d
echo "Services restarted successfully."
2024-10-22 15:54:47 +02:00
/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}")
2024-10-22 15:54:47 +02:00
if [ "$response" -eq 502 ] || [ "$response" -eq 500 ] || [ "$response" -eq 403 ]; 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"
}
2024-10-22 15:54:47 +02:00
# Playlist IDs to fetch
playlist_ids=("PLMws9SCqJ1JCeVMVPsdamuUM0HK0MbA6g")
# Base URL for the API
base_url="http://localhost:54301/latest_version?id="
2024-10-22 15:54:47 +02:00
# Pick a random playlist (without using invalid options in shuf)
random_playlist_id="PLMC9KNkIncKvYin_USF1qoJQnIyMAfRxl"
echo "Randomly selected playlist: $random_playlist_id"
2024-10-22 15:54:47 +02:00
# Fetch playlist JSON data
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
2024-10-22 15:54:47 +02:00
# Extract video IDs from the playlist
video_ids=($(extract_video_ids "/tmp/playlist_data.json"))
if [ $? -ne 0 ]; then
echo "Error: Failed to extract video IDs. Exiting..."
exit 1
fi
2024-10-22 15:54:47 +02:00
# Shuffle video IDs and pick 4 random videos
shuffled_video_ids=($(shuf -e "${video_ids[@]}" | head -n 4))
error_count=0
2024-10-22 15:54:47 +02:00
all_errors=(500 502 403)
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}"
2024-10-22 15:54:47 +02:00
# Generate a random Chrome version
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
2024-10-22 15:54:47 +02:00
# Echo the status code for visibility
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))
2024-10-22 15:54:47 +02:00
# Run the token refresh script
echo "Running inv-update-token.sh for ID $video_id..."
/home/qt/globe/scripts/inv-update-token.sh
2024-10-22 15:54:47 +02:00
/home/qt/globe/scripts/inv-update-token.sh
echo "inv-update-token.sh script executed successfully."
2024-10-22 15:54:47 +02:00
# Fetch the video again after token refresh
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"
2024-10-22 15:54:47 +02:00
# Check if it still results in 500/502/403 after refresh
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
2024-10-22 15:54:47 +02:00
echo "we are so barack (Status code for ID $video_id is neither 502, 500, nor 403.)"
fi
2024-10-22 15:54:47 +02:00
echo "----------------****************----------------" # Separator for readability
done
2024-10-22 15:54:47 +02:00
# If all videos still resulted in 500/502/403 errors even after running inv-update-token.sh, try restaring
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