File size: 8,814 Bytes
3636d4e
 
 
 
 
 
8c3cf63
3636d4e
 
 
 
4ac214c
 
3636d4e
 
 
8ca9883
584c24c
 
8ca9883
3636d4e
 
 
cce61b2
3636d4e
584c24c
ae1a503
 
 
d41d499
bb4cf64
 
 
 
ae1a503
 
 
3636d4e
 
 
 
 
 
 
 
 
 
 
b571955
8d57283
b571955
 
 
 
 
 
 
 
2f4074b
 
 
d44f01b
3636d4e
4ac214c
3636d4e
 
 
 
 
 
 
 
 
 
 
4ac214c
 
 
584c24c
 
3636d4e
2f4074b
 
 
 
 
 
 
 
 
 
 
4ac214c
 
 
 
 
3636d4e
4ac214c
 
8ca9883
 
 
 
 
 
3636d4e
8ca9883
 
 
 
 
 
 
 
 
3636d4e
584c24c
 
 
 
 
 
 
8ca9883
 
 
 
 
 
 
 
 
 
 
584c24c
8ca9883
 
4ac214c
8ca9883
3636d4e
2f4074b
4ac214c
2f4074b
4c64cef
2f4074b
 
 
 
 
584c24c
4ac214c
2f4074b
 
 
 
 
 
 
 
 
 
4c64cef
 
 
 
 
2f4074b
 
 
 
 
 
 
 
 
 
4ac214c
 
3636d4e
 
bb4cf64
4ac214c
8ca9883
4ac214c
 
 
 
 
 
 
 
 
8ca9883
4ac214c
 
584c24c
 
8ca9883
4ac214c
 
 
bb4cf64
8ca9883
 
 
 
 
584c24c
 
 
 
 
 
 
 
 
8ca9883
 
 
4c64cef
 
 
 
 
 
 
 
 
 
bb4cf64
4c64cef
 
 
 
3636d4e
 
 
 
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ToKnow.ai - Shazam Playlist to Youtube Playlist</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://www.youtube.com/iframe_api"></script>
    <style>
        .youtube-player,
        iframe {
            width: 100%;
            height: 100%;
        }

        .playlist tbody tr {
            cursor: pointer;
        }
    </style>
</head>

<body class="bg-light">
    <div class="container my-4">
        <h1 class="text-center">Convert Your Shazam Playlist to YouTube Playlist</h1>
        <p class="text-center">
            <em>
                <i>
                    Read details or <b>Comment</b> at
                    <a target="_blank"
                        href="https://toknow.ai/posts/shazam-playlist-to-youtube-playlist/"><b>ToKnow.ai</b> blog
                        post.
                    </a>
                </i>
            </em>
        </p>
        <p class="text-center">
            Download the CSV of your playlist from
            <a href="https://www.shazam.com/myshazam" target="_blank">https://www.shazam.com/myshazam</a>.
        </p>
        <p class="text-center">Upload your Shazam Playlist CSV file.</p>
        <div class="row mx-2 justify-content-center">
            <div class="col-md-6">
                <input type="file" class="form-control upload-form col-md-6" accept=".csv">
            </div>
        </div>

        <div class="row mt-2 justify-content-center">
            <div class="col-md-6 d-flex justify-content-around flex-wrap">
                <a id="load-test-playlist" class="btn btn-warning btn-sm fst-italic">
                    click here to load test playlist
                </a>

                <img class="rounded"
                    src="https://api.visitorbadge.io/api/visitors?path=https://toknow.ai/posts/shazam-playlist-to-youtube-playlist" />
            </div>
            
        </div>

        <div class="row mt-3 justify-content-center">
            <div class="col-md-8">
                <div class="object-fit-contain border rounded ratio ratio-16x9">
                    <div class="youtube-player"></div>
                </div>
            </div>
        </div>

        <div class="row mt-5">
            <div class="col-md-12 playlist table-responsive">
            </div>
        </div>
    </div>

    <script>
        const playlistTable = document.querySelector('.playlist');
        const uploaForm = document.querySelector('.upload-form');
        let songsPlaylist = []
        let videoIndex = -1;
        let youtubePlayer;

        document.querySelector('#load-test-playlist').addEventListener('click', async e => {
            try {
                e.preventDefault();

                const playlist = await(await fetch('/parse_csv_test')).json();
                generateTable(playlist)
            } catch (error) {
                playlistTable.innerHTML = error;
            }
        });

        uploaForm.addEventListener('input', e => {
            e.preventDefault();
            if (e.target.files.length == 0) {
                return;
            }

            parseCsv(e.target.files[0], playlistTable);
        });
        playlistTable.addEventListener('click', e => {
            e.preventDefault();
            const row = event.target.closest('tr');
            if (row) {
                const index = row.dataset.index ? Number(row.dataset.index) : undefined;
                onContinue(undefined, index);
            }
        });
        function resetCurrentPlayingBackground() {
            playlistTable.querySelectorAll('tbody tr').forEach(row => {
                if (Number(row.dataset.index) == videoIndex) {
                    row.classList.add('bg-warning');
                } else {
                    row.classList.remove('bg-warning');
                }
            })
        }
        function addErrorToCurrentIndex() {
            playlistTable.querySelectorAll('tbody tr').forEach(row => {
                if (Number(row.dataset.index) == videoIndex) {
                    row.classList.add('bg-danger');
                }
            })
        }
        async function getVideoId(song) {
            const response = await fetch(
                '/video_id',
                {
                    headers: { 'Content-Type': 'application/json' },
                    method: 'POST',
                    body: JSON.stringify({ title: song.Title, artist: song.Artist })
                });
            return await response.text()
        }
        async function nextVideo(callback, newIndex = undefined) {
            newIndex = newIndex >= 0 ? newIndex : (videoIndex + 1)
            videoIndex = newIndex < songsPlaylist.length ? newIndex : 0;
            let video_id = await getVideoId(songsPlaylist[videoIndex]);
            callback(video_id);
            resetCurrentPlayingBackground();
        }
        function generateTable(playlist) {
            try {
                songsPlaylist = playlist
                const tableBody = songsPlaylist.map((i, index) => `
                            <tr data-index="${index}">
                                <th>${index + 1}</th>
                                <th>${i.Title}</th>
                                <th>${i.Artist}</th>
                            </tr>`
                ).join('')
                playlistTable.innerHTML = `
                <table class="table table-striped table-hover table-bordered rounded">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Title</th>
                            <th>Artist</th>
                        </tr>
                    </thead>
                    <tbody>${tableBody}</tbody>
                </table>`

                tryToPlay(() => {
                    videoIndex = -1;
                    onContinue();
                }, 100)
            } catch (error) {
                playlistTable.innerHTML = error;
            }
        }
        async function parseCsv(file, playlistTable) {
            try {
                const formData = new FormData();
                formData.append('file', file);
                const playlist = await (await fetch('/parse_csv', { method: 'POST', body: formData })).json();
                generateTable(playlist)
            } catch (error) {
                playlistTable.innerHTML = error;
            }
        }
        function initiatePlayer() {
            const youtubePlayerElement = document.querySelector('.youtube-player');
            youtubePlayer = window.youtubePlayer = new YT.Player(youtubePlayerElement, {
                height: '100%',
                width: '100%',
                playerVars: { autoplay: 1 },
                events: {
                    'onReady': function (event) {
                        event.target.playVideo()
                    },
                    'onStateChange': function (event) {
                        if (event.data === YT.PlayerState.ENDED) {
                            onContinue(event?.target);
                        }
                    },
                    'onError': function (event) {
                        addErrorToCurrentIndex();
                        onContinue(event?.target);
                    }
                }
            });
        }
        function onContinue(player = undefined, newIndex = undefined) {
            if (songsPlaylist.length > 0) {
                nextVideo((value) => {
                    player = player || youtubePlayer
                    player.loadVideoById(value);
                    setTimeout(() => {
                        player.playVideo();

                        setTimeout(() => {
                            if (player.getPlayerState() != YT.PlayerState.PLAYING) {
                                player.playVideo();
                            }
                        }, 10);
                    }, 10);
                }, newIndex);
            }
        }

        function tryToPlay(playCallback, timeout) {
            setTimeout(() => {
                if (YT.Player) {
                    if (!youtubePlayer) {
                        initiatePlayer();
                        tryToPlay?.();
                    } else if (songsPlaylist.length > 0) {
                        playCallback();
                    }
                }
            }, timeout);
        }

        tryToPlay(null, 300);
    </script>
</body>

</html>