Last active
September 8, 2024 15:58
-
-
Save akamhy/23a4e1407e4a8a2b3ad82321061d1849 to your computer and use it in GitHub Desktop.
Bash Script to get YouTube Playlist Length / Duration right in Your terminal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have started a new service hosted on Digital Ocean. See https://youtube-playlist-length-analyzer.akamhy.me.