generated from Nich87/Svelte-Flowbite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+page.svelte
249 lines (227 loc) · 7 KB
/
+page.svelte
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
240
241
242
243
244
245
246
247
248
249
<script lang="ts">
import Icon from '@iconify/svelte';
import {
toggleLoadingState,
togglepopupUrlErrorModal,
togglepopupFetchModal,
togglepopupRegionErrorModal
} from '$lib/store';
import { parseVideoUrl } from '$lib/parseURL';
import type { VideoInfo, PlaylistInfo, SearchInfo } from '$lib/types/index';
import Header from 'components/Header.svelte';
import Footer from 'components/Footer.svelte';
import RegionError from 'components/Modals/RegionError.svelte';
import URLErrorModal from 'components/Modals/URLError.svelte';
import FetchErrorModal from 'components/Modals/FetchError.svelte';
import MainTabs from 'components/Tabs/index.svelte';
import Spinner from 'components/Spinner.svelte';
let url: string;
let videoInfo: VideoInfo;
let playlistInfo: PlaylistInfo;
let searchInfo: SearchInfo;
async function searchVideoInfo() {
const videoUrl = parseVideoUrl(url);
if (!videoUrl) return togglepopupUrlErrorModal();
const response = await fetch(`/api/ytdl/info?id=${videoUrl}`, {
method: 'GET',
headers: { 'content-type': 'application/json' }
});
toggleLoadingState();
if (response.status !== 200) {
toggleLoadingState();
togglepopupFetchModal();
return console.error(response.status, response);
}
videoInfo = await response.json();
toggleLoadingState();
}
async function searchPlaylistInfo() {
const playlistUrl = url;
if (!playlistUrl) return togglepopupUrlErrorModal();
const response = await fetch(`/api/ytdl/playlist?url=${playlistUrl}`, {
method: 'GET',
headers: { 'content-type': 'application/json' }
});
toggleLoadingState();
if (response.status !== 200) {
toggleLoadingState();
togglepopupFetchModal();
return console.error(response.status, response);
}
playlistInfo = await response.json();
toggleLoadingState();
}
async function searchQueryInfo() {
const query = url;
if (!query) return togglepopupUrlErrorModal();
const response = await fetch(`/api/ytdl/search?q=${query}`, {
method: 'GET',
headers: { 'content-type': 'application/json' }
});
toggleLoadingState();
if (response.status !== 200) {
toggleLoadingState();
togglepopupFetchModal();
return console.error(response.status, response);
}
searchInfo = await response.json();
toggleLoadingState();
}
async function downloadVideo(videoId: string, downloadType: string) {
const response = await fetch(`/api/ytdl/download?v=${videoId}&type=${downloadType}`, {
method: 'GET',
headers: {
'content-type': downloadType === 'video' ? 'video/mp4' : 'audio/mpeg'
}
});
toggleLoadingState();
if (response.status !== 200) {
let json = await response.json();
json.errorobj.info.error_type === 'UNPLAYABLE'
? togglepopupRegionErrorModal()
: togglepopupFetchModal();
toggleLoadingState();
return console.error(response.status, response);
}
const blob = await response.blob();
toggleLoadingState();
const _url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = _url;
a.download = `${videoId}.${downloadType === 'video' ? 'mp4' : 'mp3'}`;
document.body.appendChild(a);
a.click();
a.remove();
}
</script>
<Header />
<div class="mx-auto max-w-4xl p-4">
<MainTabs
on:Video={(e) => {
url = e.detail;
searchVideoInfo();
}}
on:Playlist={(e) => {
url = e.detail;
searchPlaylistInfo();
}}
on:Query={(e) => {
url = e.detail;
searchQueryInfo();
}}
/>
</div>
<Spinner />
{#if videoInfo}
<div class="mx-auto mt-8 max-w-3xl">
<div class="flex justify-center space-x-4">
<button class="btn btn-primary" on:click={() => downloadVideo(videoInfo.videoId, 'video')}>
<Icon icon="mdi:video" class="m-1" /> Download Video
</button>
<button class="btn btn-secondary" on:click={() => downloadVideo(videoInfo.videoId, 'audio')}>
<Icon icon="mdi:file-music" class="m-1" /> Download Audio
</button>
</div>
<hr class="my-4" />
<iframe
title={videoInfo.title}
src={videoInfo.iframe.iframeUrl}
width="100%"
height={videoInfo.iframe.iframeHeight}
/>
<p class="text-2xl font-semibold">{videoInfo.title}</p>
<p class="text-xl font-semibold">Category: {videoInfo.category}</p>
<p class="text-xl font-semibold">👀 {videoInfo.counts.viewCount}</p>
<p class="text-xl font-semibold">👍 {videoInfo.counts.likeCount}</p>
<hr class="my-4" />
<blockquote class="overflow-auto border-l-4 border-gray-500 bg-gray-100 p-4">
<pre class="whitespace-pre-wrap">{videoInfo.description}</pre>
</blockquote>
<hr class="my-4" />
<div class="flex flex-wrap items-center">
{#each videoInfo.keywords as keyword}
<a
href="https://www.youtube.com/results?search_query={keyword}"
target="_blank"
class="badge badge-error mb-2 mr-2"
>
<Icon icon="mdi:magnify" class="mr-1.5 h-2.5 w-2.5" />
{keyword}
</a>
{/each}
</div>
</div>
{/if}
{#if playlistInfo}
<div class="mx-auto mt-8 max-w-3xl">
<div class="flex flex-col items-center">
<img src={playlistInfo.author.url} alt="CoverImage" class="h-32 w-32 object-cover" />
<div class="mt-4 text-center">
<p class="text-lg">Playlist: {playlistInfo.title}</p>
<p class="text-sm">
Author: {playlistInfo.author.name} | itemCount: {playlistInfo.itemCount}
</p>
</div>
</div>
<div class="mt-4 border border-red-500 p-2">
<ul class="list-none">
{#each playlistInfo.videos as video}
<li class="flex justify-between border-b p-2">
<a href={video.url} class="flex-grow">{video.title}</a>
<div class="flex">
<Icon
icon="mdi:video"
class="m-1"
on:click={() => downloadVideo(video.videoId, 'video')}
/>
<Icon
icon="mdi:file-music"
class="m-1"
on:click={() => downloadVideo(video.videoId, 'audio')}
/>
</div>
</li>
{/each}
</ul>
</div>
</div>
{/if}
{#if searchInfo}
<div class="grid grid-cols-1 gap-4 py-8 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
{#each searchInfo as entry}
{#if entry?.videoId}
<div class="flex h-full flex-col items-center">
<div class="card flex w-full flex-col bg-base-100 shadow-xl">
<figure>
<img src={entry.thumbnail.url} alt="thumbnail" class="object-cover" />
</figure>
<div class="card-body flex-1">
<h2 class="card-title truncate">{entry.title}</h2>
<p>Published at {entry.published ? entry.published : 'unknown'}</p>
<p>Views: {entry.viewCount}</p>
<p>Duration: {entry.duration}</p>
</div>
</div>
<div class="mt-2 flex space-x-2">
<button
class="btn btn-primary px-2 py-1 text-sm"
on:click={() => downloadVideo(entry.videoId, 'video')}
>
<Icon icon="mdi:video" class="mr-1" /> Download Video
</button>
<button
class="btn btn-secondary px-2 py-1 text-sm"
on:click={() => downloadVideo(entry.videoId, 'audio')}
>
<Icon icon="mdi:file-music" class="mr-1" /> Download Audio
</button>
</div>
</div>
{/if}
{/each}
</div>
{/if}
<RegionError />
<FetchErrorModal />
<URLErrorModal />
<Footer />