Fix #13, make youtube API optional
This commit is contained in:
@ -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);
|
||||
|
@ -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
7
src/lib/truthyString.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user