-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseSyncList.sh
executable file
·212 lines (163 loc) · 6.66 KB
/
parseSyncList.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
# This script parses a WhatsApp group export txt file and syncs spotify tracks to a playlist
parseCodeFromRedirectURL(){
local URL=$1
local CODE=''
# Extract the code from the URL
CODE=$(echo "$URL" | grep -o 'code=[^&]*' | cut -d'=' -f2)
echo "$CODE"
}
ACCESS_TOKEN='';
authorize(){
echo "Authorizing Spotify..."
local ACCESS_TOKEN_RESPONSE='';
local ACCESS_ERROR='';
local CODE='';
# Open the Spotify authorization URL
open "https://accounts.spotify.com/authorize?client_id=$SPOTIFY_CLIENT_ID&response_type=code&redirect_uri=$SPOTIFY_REDIRECT_URI&scope=playlist-modify-private%20playlist-modify-public"
# Get the code from the redirect URL
echo "Enter the redirect URL:"
read -r CODE_INPUT
CODE=$(parseCodeFromRedirectURL "$CODE_INPUT")
ACCESS_TOKEN_RESPONSE=$(curl -s -X POST "https://accounts.spotify.com/api/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: Basic $(echo -n "$SPOTIFY_CLIENT_ID:$SPOTIFY_CLIENT_SECRET" | base64)" \
-d "grant_type=authorization_code&code=$CODE&redirect_uri=$SPOTIFY_REDIRECT_URI")
# Get the access token
ACCESS_TOKEN=$(echo $ACCESS_TOKEN_RESPONSE | jq -r '.access_token')
ACCESS_ERROR=$(echo $ACCESS_TOKEN_RESPONSE | jq -r '.error')
if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_ERROR" != "null" ]; then
echo "Access error: $ACCESS_ERROR"
echo "🚫 Failed to get access token 😢"
exit 1
else
echo "✅ Successfully authorized 🔐"
fi
}
removeTracksAlreadyInPlaylist(){
local TRACK_LIST=$1
local PATTERN=''
local FILTERED_TRACK_LIST=''
# Convert existing track IDs into a grep-friendly pattern
PATTERN=$(echo "$EXISTING_TRACKS" | sed 's/ /\\|/g')
if [ -z "$PATTERN" ]; then
echo "$TRACK_LIST"
return
fi
# Filter TRACK_LIST by removing entries matching the pattern
FILTERED_TRACK_LIST=$(echo "$TRACK_LIST" | grep -v "$PATTERN")
echo "$FILTERED_TRACK_LIST"
}
TRACK_LIST=''
loadList(){
local TRACK_DUPLICATES=''
# Parse the WhatsApp group export txt file and extract the spotify track links
TRACK_LIST=$(grep -o 'https://open.spotify.com/track/[a-zA-Z0-9]*' < _chat.txt)
TRACK_DUPLICATES=$(echo "$TRACK_LIST" | sort | uniq -d)
# Print duplicates
if [ -n "$TRACK_DUPLICATES" ]; then
# Print count of how many duplicates found
echo "Found $(echo "$TRACK_DUPLICATES" | wc -l | tr -d '[:space:]') duplicate tracks"
fi
# Remove duplicates while maintaining order
TRACK_LIST=$(echo "$TRACK_LIST" | awk '!seen[$0]++')
# Print number of tracks found
echo "Found $(echo "$TRACK_LIST" | wc -l | tr -d '[:space:]') tracks"
# Remove tracks already in the playlist
TRACK_LIST_COUNT_BEFORE=$(echo "$TRACK_LIST" | wc -l | tr -d '[:space:]');
TRACK_LIST=$(removeTracksAlreadyInPlaylist "$TRACK_LIST")
if [ -z "$TRACK_LIST" ]; then
echo "✅ All $TRACK_LIST_COUNT_BEFORE already added. No new tracks to add"
exit 0
fi
TRACK_LIST_COUNT_AFTER=$(echo "$TRACK_LIST" | wc -l | tr -d '[:space:]');
echo "✨ Found $TRACK_LIST_COUNT_AFTER new tracks to add 🎵"
echo "$TRACK_LIST"
}
fetchTracks(){
local PLAYLIST_TRACKS_RESPONSE=''
PLAYLIST_TRACKS_RESPONSE=$(curl -s -X GET "$1" \
-H "Authorization: Bearer $ACCESS_TOKEN")
echo "$PLAYLIST_TRACKS_RESPONSE"
}
fetchExistingTracks(){
echo "Fetching existing tracks from the playlist..."
local FETCH_RESPONSE=''
local NEXT_URL=''
local EXISTING_TRACKS_COUNT=''
FETCH_RESPONSE=$(fetchTracks "https://api.spotify.com/v1/playlists/$SPOTIFY_PLAYLIST_ID/tracks?limit=100&offset=0")
FETCH_ERROR=$(echo "$FETCH_RESPONSE" | jq -r '.error')
if [ "$FETCH_ERROR" != "null" ]; then
echo "Fetch error: $FETCH_ERROR"
echo "Response: $FETCH_RESPONSE"
echo "🚫 Failed to fetch existing tracks 😢"
exit 1
fi
# Total number of tracks in the playlist
EXISTING_TRACKS_COUNT=$(echo "$FETCH_RESPONSE" | jq -r '.total')
if [ "$EXISTING_TRACKS_COUNT" -eq 0 ]; then
echo "✅ Playlist is empty 🎵"
return 0
fi
EXISTING_TRACKS=$(echo "$FETCH_RESPONSE" | jq -r '.items[].track.id');
NEXT_URL=$(echo "$FETCH_RESPONSE" | jq -r '.next')
while [ "$NEXT_URL" != "null" ]; do
echo "Fetching next page of tracks: $NEXT_URL"
FETCH_RESPONSE=$(fetchTracks "$NEXT_URL")
FETCH_ERROR=$(echo "$FETCH_RESPONSE" | jq -r '.error')
if [ "$FETCH_ERROR" != "null" ]; then
echo "Fetch error: $FETCH_ERROR"
echo "Response: $FETCH_RESPONSE"
echo "🚫 Failed to fetch existing tracks 😢"
exit 1
fi
EXISTING_TRACKS+=$'\n'
EXISTING_TRACKS+="$(echo "$FETCH_RESPONSE" | jq -r '.items[].track.id')"
NEXT_URL=$(echo "$FETCH_RESPONSE" | jq -r '.next')
done
EXISTING_TRACKS_COUNT=$(echo "$EXISTING_TRACKS" | wc -l | tr -d '[:space:]')
echo "✅ Successfully fetched $EXISTING_TRACKS_COUNT existing tracks 🎵"
}
convertToSpotifyURIs(){
echo $(echo "$TRACK_LIST" | sed 's|https://open.spotify.com/track/|spotify:track:|g')
}
convertSpotifyURIsToArrayString(){
echo "[\""$(echo "$1" | sed 's| |", "|g')"\"]"
}
createTrackListGroups(){
# Spotify has a limit of 100 tracks per request so we need to split the list into groups of 100
while IFS= read -r line; do
TRACK_LIST_GROUPS+=("$line")
done < <(echo "$TRACK_LIST" | xargs -n 100)
echo "Created $(echo "${#TRACK_LIST_GROUPS[@]}") groups of tracks"
}
updatePlaylist(){
echo "Updating playlist..."
for group_list_string in "${TRACK_LIST_GROUPS[@]}"; do
#split the string by space and count the number of items
group_list_length=$(echo "$group_list_string" | wc -w)
echo "Adding: $group_list_length tracks to playlist..."
array_string=$(convertSpotifyURIsToArrayString "$group_list_string")
response=$(curl -s -X POST "https://api.spotify.com/v1/playlists/$SPOTIFY_PLAYLIST_ID/tracks" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"uris\": $array_string}")
response_error=$(echo "$response" | jq -r '.error')
if [ "$response_error" != "null" ]; then
echo "Response error: $response_error"
echo "Response: $response"
echo "🚫 Failed to update playlist 😢"
exit 1
fi
done
echo "✅ Successfully updated playlist 🎵"
}
authorize
EXISTING_TRACKS=''
fetchExistingTracks
loadList
TRACK_LIST=$(convertToSpotifyURIs)
TRACK_LIST_GROUPS=()
createTrackListGroups
updatePlaylist
exit 0