Skip to content

Instantly share code, notes, and snippets.

@akamhy
Last active March 5, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akamhy/23a4e1407e4a8a2b3ad82321061d1849 to your computer and use it in GitHub Desktop.
Save akamhy/23a4e1407e4a8a2b3ad82321061d1849 to your computer and use it in GitHub Desktop.
Bash Script to get YouTube Playlist Length / Duration right in Your terminal.
#!/bin/bash
# Check if curl is installed
if ! [ -x "$(command -v curl)" ]; then
echo "Error: curl is not installed. Please install curl and try again." >&2
exit 1
fi
# Check if jq is installed
if ! [ -x "$(command -v jq)" ]; then
echo "Error: jq is not installed. Please install jq and try again." >&2
exit 1
fi
# Check if a playlist ID is passed as an argument
if [ $# -eq 0 ]; then
echo "Error: No playlist ID provided. Please provide a playlist ID as an argument and try again." >&2
exit 1
fi
# Store the playlist ID passed as an argument
playlist_id="$1"
# Store the output of the curl command in a variable
output=$(curl --silent -X 'GET' \
"https://youtube-playlist-length-analyzer.akamhy.me/api?playlist_id=$playlist_id" \
-H 'accept: application/json')
# Check the status of the API response
status=$(echo "$output" | jq '.status')
# If the status is not "success", display the error message and exit
if [ "$status" != '"success"' ]; then
error_message=$(echo "$output" | jq '.message')
echo "Error: $error_message" >&2
exit 1
fi
# Extract the values of the desired keys and store them in variables
playlist_title=$(echo "$output" | jq '.playlist_title')
playlist_video_count=$(echo "$output" | jq '.playlist_video_count')
playlist_human_readable_duration=$(echo "$output" | jq '.playlist_human_readable_duration')
playlist_human_readable_average_video_duration=$(echo "$output" | jq '.playlist_human_readable_average_video_duration')
# Print the values of the desired keys
echo "Playlist Title: $playlist_title"
echo "Playlist Video Count: $playlist_video_count"
echo "Playlist Duration: $playlist_human_readable_duration"
echo "Playlist Average Video Duration: $playlist_human_readable_average_video_duration"
@akamhy
Copy link
Author

akamhy commented Feb 10, 2023

I have started a new service hosted on Digital Ocean. See https://youtube-playlist-length-analyzer.akamhy.me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment