Fix #13, make youtube API optional

This commit is contained in:
Max Nuding 2023-04-11 18:39:02 +02:00
parent 4fbd9a260f
commit d716b3882b
Signed by: phlaym
GPG Key ID: A06651BAB6777237
5 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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.

View File

@ -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);

View File

@ -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<boolean> {
if (isTruthy(YOUTUBE_DISABLE)) {
return false;
}
const matches = postContent.matchAll(YOUTUBE_REGEX);
for (const match of matches) {
if (match === undefined || match.groups === undefined) {

7
src/lib/truthyString.ts Normal file
View File

@ -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;
}