diff --git a/.env.EXAMPLE b/.env.EXAMPLE index d9d5761..3c8bec1 100644 --- a/.env.EXAMPLE +++ b/.env.EXAMPLE @@ -1,6 +1,7 @@ HASHTAG_FILTER = ichlausche,music,musik,nowplaying,tunetuesday,nowlistening URL_FILTER = song.link,album.link,spotify.com,music.apple.com,bandcamp.com YOUTUBE_API_KEY = CHANGE_ME +YOUTUBE_DISABLE = false MASTODON_INSTANCE = 'metalhead.club' BASE_URL = 'https://moshingmammut.phlaym.net' VERBOSE = false diff --git a/README.md b/README.md index 74328ed..e6b97eb 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,9 @@ and set your `User`, `Group`, `ExecStart` and `WorkingDirectory` accordingly. Copy `.env.EXAMPLE` to `.env` and add your `YOUTUBE_API_KEY`. To obtain one follow [YouTube's guide](https://developers.google.com/youtube/registering_an_application) to create an -_API key_. As soon as #13 is implemented, this will be optional! +_API key_. +If `YOUTUBE_API_KEY` is unset, all YouTube videos will be assumed to contain music links. +If this is unwanted, set `YOUTUBE_DISABLE` to `true`). Run `npm run build` and copy the output folder, usually `build` to `$APP_DIR` on your server. diff --git a/src/lib/server/db.ts b/src/lib/server/db.ts index c2655ab..b6271c3 100644 --- a/src/lib/server/db.ts +++ b/src/lib/server/db.ts @@ -1,11 +1,12 @@ import { env } from '$env/dynamic/private'; import type { Account, Post, Tag } from '$lib/mastodon/response'; +import { isTruthy } from '$lib/truthyString'; import sqlite3 from 'sqlite3'; const { DEV } = import.meta.env; const db: sqlite3.Database = new sqlite3.Database('moshingmammut.db'); -if (DEV && env.VERBOSE === 'true') { +if (DEV && isTruthy(env.VERBOSE)) { sqlite3.verbose(); db.on('change', (t, d, table, rowid) => { console.debug('DB change event', t, d, table, rowid); diff --git a/src/lib/server/timeline.ts b/src/lib/server/timeline.ts index 7806a92..59bd80d 100644 --- a/src/lib/server/timeline.ts +++ b/src/lib/server/timeline.ts @@ -2,11 +2,13 @@ import { HASHTAG_FILTER, MASTODON_INSTANCE, URL_FILTER, - YOUTUBE_API_KEY + YOUTUBE_API_KEY, + YOUTUBE_DISABLE } from '$env/static/private'; import type { Post, Tag, TimelineEvent } from '$lib/mastodon/response'; import { getPosts, savePost } from '$lib/server/db'; import { createFeed, saveAtomFeed } from '$lib/server/rss'; +import { isTruthy } from '$lib/truthyString'; import { WebSocket } from 'ws'; const YOUTUBE_REGEX = new RegExp( @@ -17,6 +19,11 @@ export class TimelineReader { private static _instance: TimelineReader; private static async isMusicVideo(videoId: string) { + if (YOUTUBE_API_KEY === undefined) { + // Assume that it *is* a music link when no YT API key is provided + // If it should assumed to not be YOUTUBE_DISABLE needs to be set to something truthy + return true; + } const searchParams = new URLSearchParams([ ['part', 'snippet'], ['id', videoId], @@ -50,6 +57,9 @@ export class TimelineReader { } private static async checkYoutubeMatches(postContent: string): Promise { + if (isTruthy(YOUTUBE_DISABLE)) { + return false; + } const matches = postContent.matchAll(YOUTUBE_REGEX); for (const match of matches) { if (match === undefined || match.groups === undefined) { diff --git a/src/lib/truthyString.ts b/src/lib/truthyString.ts new file mode 100644 index 0000000..55e4aab --- /dev/null +++ b/src/lib/truthyString.ts @@ -0,0 +1,7 @@ +export function isTruthy(value: string | number | boolean | null | undefined): boolean { + if (typeof value === 'string') { + return value.toLowerCase() === 'true' || !!+value; // here we parse to number first + } + + return !!value; +}