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: implement filter in repo + server

Christian Fei1654d877503a
diff --git a/lib/repository.js b/lib/repository.js
index 2b78c1a..18d60dd 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -69,12 +69,20 @@ export default class Repository {
fs.writeFileSync(this.paths.settings, JSON.stringify(this.settings, null, 2))
console.log('saving settings', this.settings)
}
- getVideos () {
- const videos = this.videos
+ getVideos (query = {}) {
+ const isFiltering = Object.keys(query).length > 0 && query.filter
+ let videos = this.videos
+ .filter(v => isFiltering ? true : !v.ignored)
.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
- .sort((a, b) => (b.ignored ? -1 : 0) + (a.ignored ? 1 : 0))
- // .filter((_, i) => i < 500)
- // console.log('get videos', videos.length)
+
+ if (isFiltering) {
+ const searchTerm = query.filter.toLowerCase()
+ videos = videos.filter(v => {
+ return v.channelName.toLowerCase().includes(searchTerm)
+ || v.title.toLowerCase().includes(searchTerm)
+ || v.description.toLowerCase().includes(searchTerm)
+ })
+ }
return videos
}
diff --git a/lib/server.js b/lib/server.js
index 5ec52fe..9032379 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -55,7 +55,7 @@ export function createServer ({repo, connections = [], updateVideos = true} = {}
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)
+ return searchVideosHandler(req, res, repo, connections)
if (url.pathname === '/api/video-quality' && req.method === 'GET')
return getVideoQualityHandler(req, res, repo, connections)
if (url.pathname === '/api/video-quality' && req.method === 'POST')
@@ -120,7 +120,7 @@ async function getBody (req) {
}
function getQuery (req) {
- return req.url.indexOf('?') >= 0
+ return (req.url && req.url.indexOf('?') >= 0)
? querystring.parse(req.url.substring(req.url.indexOf('?') + 1))
: {}
}
@@ -313,8 +313,10 @@ function watchVideoHandler(req, res, repo) {
}
}
-function allVideosHandler(req, res, repo) {
- const videos = repo.getVideos()
+export function searchVideosHandler(req, res, repo) {
+ const query = getQuery(req)
+ console.log(query)
+ const videos = repo.getVideos(query)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(videos))
}
diff --git a/test/repository.test.js b/test/repository.test.js
index 9100232..900df43 100644
--- a/test/repository.test.js
+++ b/test/repository.test.js
@@ -1,4 +1,4 @@
-import { test } from 'node:test'
+import { test, describe } from 'node:test'
import fs from 'fs'
import assert from 'assert';
import Repository from '../lib/repository.js'
@@ -10,106 +10,128 @@ test.beforeEach(() => {
}
repo = new Repository('./test/data')
})
-const video1 = {id: '12345', channelName: 'tester', title: 'The Code'}
-const video2 = {id: '67890', channelName: 'programmer', title: 'The Error'}
-
-test('empty repo provisions data folder', () => {
- assert.ok(repo)
- assert.equal(repo.paths.videos, './test/data/videos.json')
- assert.equal(repo.paths.channels, './test/data/channels.json')
- assert.equal(repo.getVideos().length, 0)
- assert.ok(fs.existsSync(repo.paths.videos))
- assert.ok(fs.existsSync(repo.paths.channels))
-})
-
-test('adds channel', () => {
- repo.addChannel('veritasium')
- assert.equal(repo.getChannels().length, 1)
- assert.equal(repo.channelExists('veritasium'), true)
-})
-test('delete channel', () => {
- repo.addChannel('veritasium')
- repo.deleteChannel('veritasium')
- assert.equal(repo.getChannels().length, 0)
- assert.equal(repo.channelExists('veritasium'), false)
-})
-
-test('gets channels', () => {
- repo.addChannel('veritasium')
- const channels = repo.getChannels()
- assert.equal(channels.length, 1)
- assert.equal(channels[0].name, 'veritasium')
-})
-
-test('upserts videos', () => {
- repo.upsertVideos([video1, video2])
- assert.equal(repo.getVideos().length, 2)
-})
-
-test('upserts single video', () => {
- repo.upsertVideos(video1)
- assert.equal(repo.getVideos().length, 1)
-})
-
-test('gets channel videos', () => {
- repo.upsertVideos([video1, video2])
- const videos = repo.getChannelVideos('tester')
- assert.equal(videos.length, 1)
- assert.equal(videos[0].title, 'The Code')
-})
-
-test('gets single video by id', () => {
- repo.upsertVideos([video1, video2])
- const video = repo.getVideo('12345')
- assert.equal(video.title, 'The Code')
-})
-
-test('deletes a video by id and sets downloaded true', () => {
- repo.upsertVideos([video1, video2])
- repo.deleteVideo('12345')
- assert.equal(repo.getVideos().length, 2)
- assert.deepEqual(repo.getVideo('12345'), {id: '12345', channelName: 'tester', title: 'The Code', downloaded: false})
-})
-
-test('toggles ignore video', () => {
- repo.upsertVideos([video1, video2])
- repo.toggleIgnoreVideo('12345')
- assert.equal(repo.getVideo('12345').ignored, true)
-})
-
-test('marks video as downloaded', () => {
- repo.upsertVideos([video1, video2])
- repo.setVideoDownloaded('12345')
- assert.equal(repo.getVideo('12345').downloaded, true)
-})
-
-test('sets video transcript', () => {
- repo.upsertVideos([video1, video2])
- repo.setVideoTranscript('12345', 'This is the transcript for The Code')
- assert.equal(repo.getVideo('12345').transcript, 'This is the transcript for The Code')
-})
-
-test('sets video summary', () => {
- repo.upsertVideos([video1, video2])
- repo.setVideoSummary('12345', 'This is the summary for The Code')
- assert.equal(repo.getVideo('12345').summary, 'This is the summary for The Code')
-})
-
-test('patches new video with addedAt', () => {
- const patchedVideo = repo.patchVideo(video1)
- assert.ok(patchedVideo.addedAt)
-})
-
-test('updates single video by id', () => {
- repo.upsertVideos(video1)
- const updatedVideo = repo.updateVideo(video1.id, { title: 'Updated Title' })
- assert.equal(updatedVideo.title, 'Updated Title')
-})
-
-test('gets video quality', () => {
- assert.equal(repo.getVideoQuality(), 720)
-})
-test('sets video quality', () => {
- assert.ok(repo.setVideoQuality(1080))
- assert.equal(repo.getVideoQuality(), 1080)
+const video1 = {id: '12345', channelName: 'tester', title: 'The Code', description: 'some description in common'}
+const video2 = {id: '67890', channelName: 'programmer', title: 'The Error', description: 'some description'}
+
+describe('repository', () => {
+ test('empty repo provisions data folder', () => {
+ assert.ok(repo)
+ assert.equal(repo.paths.videos, './test/data/videos.json')
+ assert.equal(repo.paths.channels, './test/data/channels.json')
+ assert.equal(repo.getVideos().length, 0)
+ assert.ok(fs.existsSync(repo.paths.videos))
+ assert.ok(fs.existsSync(repo.paths.channels))
+ })
+
+ test('adds channel', () => {
+ repo.addChannel('veritasium')
+ assert.equal(repo.getChannels().length, 1)
+ assert.equal(repo.channelExists('veritasium'), true)
+ })
+ test('delete channel', () => {
+ repo.addChannel('veritasium')
+ repo.deleteChannel('veritasium')
+ assert.equal(repo.getChannels().length, 0)
+ assert.equal(repo.channelExists('veritasium'), false)
+ })
+
+ test('gets channels', () => {
+ repo.addChannel('veritasium')
+ const channels = repo.getChannels()
+ assert.equal(channels.length, 1)
+ assert.equal(channels[0].name, 'veritasium')
+ })
+
+ test('upserts videos', () => {
+ repo.upsertVideos([video1, video2])
+ assert.equal(repo.getVideos().length, 2)
+ })
+
+ test('upserts single video', () => {
+ repo.upsertVideos(video1)
+ assert.equal(repo.getVideos().length, 1)
+ })
+
+ test('gets channel videos', () => {
+ repo.upsertVideos([video1, video2])
+ const videos = repo.getChannelVideos('tester')
+ assert.equal(videos.length, 1)
+ assert.equal(videos[0].title, 'The Code')
+ })
+
+ test('gets single video by id', () => {
+ repo.upsertVideos([video1, video2])
+ const video = repo.getVideo('12345')
+ assert.equal(video.title, 'The Code')
+ })
+
+ test('deletes a video by id and sets downloaded true', () => {
+ repo.upsertVideos([video1, video2])
+ repo.deleteVideo('12345')
+ assert.equal(repo.getVideos().length, 2)
+ assert.deepEqual(repo.getVideo('12345'), {id: '12345', channelName: 'tester', title: 'The Code', description: 'some description in common', downloaded: false})
+ })
+
+ test('toggles ignore video', () => {
+ repo.upsertVideos([video1, video2])
+ repo.toggleIgnoreVideo('12345')
+ assert.equal(repo.getVideo('12345').ignored, true)
+ })
+
+ test('marks video as downloaded', () => {
+ repo.upsertVideos([video1, video2])
+ repo.setVideoDownloaded('12345')
+ assert.equal(repo.getVideo('12345').downloaded, true)
+ })
+
+ test('sets video transcript', () => {
+ repo.upsertVideos([video1, video2])
+ repo.setVideoTranscript('12345', 'This is the transcript for The Code')
+ assert.equal(repo.getVideo('12345').transcript, 'This is the transcript for The Code')
+ })
+
+ test('sets video summary', () => {
+ repo.upsertVideos([video1, video2])
+ repo.setVideoSummary('12345', 'This is the summary for The Code')
+ assert.equal(repo.getVideo('12345').summary, 'This is the summary for The Code')
+ })
+
+ test('patches new video with addedAt', () => {
+ const patchedVideo = repo.patchVideo(video1)
+ assert.ok(patchedVideo.addedAt)
+ })
+
+ test('updates single video by id', () => {
+ repo.upsertVideos(video1)
+ const updatedVideo = repo.updateVideo(video1.id, { title: 'Updated Title' })
+ assert.equal(updatedVideo.title, 'Updated Title')
+ })
+
+ test('gets video quality', () => {
+ assert.equal(repo.getVideoQuality(), 720)
+ })
+ test('sets video quality', () => {
+ assert.ok(repo.setVideoQuality(1080))
+ assert.equal(repo.getVideoQuality(), 1080)
+ })
+})
+
+describe('search', () => {
+ test('searches videos by title', async () => {
+ repo.upsertVideos([video1, video2])
+ const results = await repo.getVideos({filter: 'The Error'})
+ assert.equal(results.length, 1)
+ assert.deepEqual(results, [video2])
+ })
+ test('searches videos by channel name', async () => {
+ repo.upsertVideos([video1, video2])
+ const results = await repo.getVideos({filter: 'tester'})
+ assert.equal(results.length, 1)
+ assert.deepEqual(results, [video1])
+ })
+ test('searches videos by description', async () => {
+ repo.upsertVideos([video1, video2])
+ const results = await repo.getVideos({filter: 'some description'})
+ assert.equal(results.length, 2)
+ })
})
\ No newline at end of file
diff --git a/test/search.test.js b/test/search.test.js
new file mode 100644
index 0000000..cb0623a
--- /dev/null
+++ b/test/search.test.js
@@ -0,0 +1,63 @@
+import {test, describe} from 'node:test'
+import fs from 'fs'
+import assert from 'assert'
+import { searchVideosHandler } from '../lib/server.js'
+import Repository from '../lib/repository.js'
+
+const req = {}
+const assertRes = (assertionCb) => ({
+ writeHead: () => {},
+ end: assertionCb
+})
+
+let repo
+test.beforeEach(() => {
+ if (fs.existsSync('./test/data')) {
+ fs.rmSync('./test/data', { recursive: true })
+ }
+ repo = new Repository('./test/data')
+ repo.upsertVideos([
+ {
+ id: "1",
+ channelName: "SomeChannel",
+ title: "A video title",
+ description: "Wow nice description",
+ downloaded: true,
+ ignored: false,
+ summary: "some summary"
+ },
+ {
+ id: "2",
+ channelName: "AnotherChannel",
+ title: "Another video title",
+ description: "What, another nice description",
+ downloaded: false,
+ ignored: true,
+ summary: "some summary"
+ },
+ {
+ id: "3",
+ channelName: "AnotherChannel",
+ title: "WOW Another video title",
+ description: "What, another nice description, that's cool",
+ downloaded: true,
+ ignored: true,
+ summary: "some summary"
+ },
+ ])
+})
+test('gets all videos without filters, show only not ignored videos', () => {
+ searchVideosHandler(req, assertRes(data => {
+ assert.deepEqual(JSON.parse(data).length, 1)
+ }), repo)
+})
+test('filters videos by channel name', () => {
+ searchVideosHandler({url: '?filter=SomeChannel'}, assertRes(data => {
+ assert.deepEqual(JSON.parse(data).length, 1)
+ }), repo)
+})
+test('filters videos by title', () => {
+ searchVideosHandler({url: '?filter=A+video+title'}, assertRes(data => {
+ assert.deepEqual(JSON.parse(data).length, 1)
+ }), repo)
+})
\ No newline at end of file
diff --git a/test/server.test.js b/test/server.test.js
index e318649..d10e286 100644
--- a/test/server.test.js
+++ b/test/server.test.js
@@ -4,7 +4,6 @@ import assert from 'assert'
import { createServer } from '../lib/server.js'
import Repository from '../lib/repository.js'
-
let repo
test.beforeEach(() => {
if (fs.existsSync('./test/data')) {