-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathspotify_delete_duplicate_tracks_in_playlists.sh
executable file
·92 lines (69 loc) · 2.53 KB
/
spotify_delete_duplicate_tracks_in_playlists.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# args: test
#
# Author: Hari Sekhon
# Date: 2020-07-24 19:05:25 +0100 (Fri, 24 Jul 2020)
#
# https://github.com/HariSekhon/Spotify-Playlists
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/spotify.sh"
# shellcheck disable=SC2034,SC2154
usage_description="
Deletes duplicate Spotify tracks in a given playlist (by Artist - Track name match, may be from different albums / singles and not 100% identical performances)
Playlist must be specified as the first argument and can be either a Spotify playlist ID or a full playlist name (see spotify_playlists.sh)
$usage_playlist_help
$usage_auth_help
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="<playlist_name_or_id>"
help_usage "$@"
min_args 1 "$@"
playlist="$1"
shift || :
# requires authorized token
export SPOTIFY_PRIVATE=1
spotify_token
# this script returns the ID if it's already in the correct format, otherwise queries and returns the playlist ID for the playlist
playlist_id="$(SPOTIFY_PLAYLIST_EXACT_MATCH=1 "$srcdir/spotify_playlist_name_to_id.sh" "$playlist")"
export SPOTIFY_DUPLICATE_TRACK_POSITIONS=1
timestamp "Finding and deleting duplicate tracks in playlist \"$playlist\" by exact \"Artist - Track\" name match:"
duplicates="$("$srcdir/spotify_duplicate_tracks_in_playlist.sh" "$playlist_id")"
if [ -z "$duplicates" ]; then
timestamp "No duplicate track names found"
exit 0
fi
count="$(wc -l <<< "$duplicates" | sed 's/[[:space:]]*//g')"
if [ -z "${SPOTIFY_NO_CONFIRM:-}" ]; then
if is_interactive; then
timestamp "Duplicates to remove:"
echo >&2
while read -r position uri; do
printf '%s\t' "$position"
"$srcdir/spotify_uri_to_name.sh" <<< "$uri"
done <<< "$duplicates"
echo
read -r -p "Are you sure you want to delete these $count tracks from playlist \"$playlist\"? (y/N) " answer
echo >&2
shopt -s nocasematch
if ! [[ "$answer" =~ y|yes ]]; then
timestamp "Aborting..."
exit 1
fi
fi
fi
timestamp "Deleting $count duplicate tracks"
echo >&2
"$srcdir/spotify_delete_from_playlist.sh" "$playlist_id" <<< "$duplicates"