Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: delete downloaded video

Christian Feibc8a4b50f866
diff --git a/client/components/video-element.js b/client/components/video-element.js
index 66263a3..d5ab6c5 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -67,7 +67,7 @@ class VideoElement extends HTMLElement {
<h4 class="title">${this.video.title}</h4>
<div class="actions flex">
${this.video.downloaded
- ? ''
+ ? /*html*/`<span tabindex="0" class="action delete" data-video-id="${this.video.id}">🗑️ Delete</span>`
: /*html*/`<span tabindex="0" class="action download" data-video-id="${this.video.id}">⬇️ Download</span>`}
${!this.video.summary
? /*html*/`<span tabindex="0" class="action summarize" data-video-id="${this.video.id}">📖 Summarize</span>`
@@ -79,6 +79,7 @@ class VideoElement extends HTMLElement {
}
registerEvents () {
handleClickAddListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))
+ handleClickAddListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))
handleClickAddListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))
handleClickAddListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))
handleClickAddListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))
@@ -87,6 +88,7 @@ class VideoElement extends HTMLElement {
}
unregisterEvents () {
handleClickRemoveListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))
+ handleClickRemoveListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))
handleClickRemoveListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))
handleClickRemoveListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))
handleClickRemoveListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))
@@ -124,6 +126,21 @@ class VideoElement extends HTMLElement {
.then(() => console.log('Download started'))
.catch((error) => console.error('Error starting download:', error))
}
+ deleteVideoHandler (event) {
+ event.preventDefault()
+ if (!confirm('About to delete video files, are you sure?')) return
+ fetch('/api/delete-video', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ id: this.video.id }),
+ })
+ .then(() => {
+ console.log('Video deleted')
+ this.video.downloaded = false
+ this.render()
+ })
+ .catch((error) => console.error('Error deleting video:', error))
+ }
summarizeVideoHandler (event) {
event.preventDefault()
const summaryStartedText = '⚡️ summary started'
diff --git a/lib/repository.js b/lib/repository.js
index 831f736..b69695b 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -53,6 +53,19 @@ export default class Repository {
})
this.saveVideos()
}
+ deleteVideo(id) {
+ const files = fs.readdirSync('./data/videos')
+ files.forEach(file => {
+ if (file.startsWith(id)) {
+ fs.unlinkSync(`./data/videos/${file}`)
+ }
+ })
+ this.videos = this.videos.map(v => {
+ if (v.id === id) return Object.assign(v, {downloaded: false})
+ return v
+ })
+ this.saveVideos()
+ }
saveVideos() {
fs.writeFileSync(this.paths.videos, JSON.stringify(this.videos, null, 2))
}
diff --git a/lib/server.js b/lib/server.js
index 7759191..c9fbd98 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -44,6 +44,8 @@ export function createServer ({repo, connections = []}) {
return summarizeVideoHandler(req, res, repo, connections, llmSettings)
if (url.pathname === '/api/ignore-video' && req.method === 'POST')
return ignoreVideoHandler(req, res, repo, connections)
+ if (url.pathname === '/api/delete-video' && req.method === 'POST')
+ return deleteVideoHandler(req, res, repo, connections)
if (url.pathname === '/api/videos' && req.method === 'GET')
return allVideosHandler(req, res, repo, connections)
if (url.pathname.match(/\/videos\/.*/) && req.method === 'GET')
@@ -247,4 +249,17 @@ export function createServer ({repo, connections = []}) {
res.writeHead(200, { 'Content-Type': 'application/json' })
return res.end(JSON.stringify(repo.toggleIgnoreVideo(id)))
}
+
+ async function deleteVideoHandler(req, res, repo, connections = []) {
+ const body = await getBody(req)
+ let { id } = JSON.parse(body)
+ try {
+ repo.deleteVideo(id)
+ res.writeHead(200)
+ res.end()
+ } catch (err) {
+ res.writeHead(500)
+ res.end()
+ }
+ }
}