Compare commits
6 Commits
1cd9d83910
...
v1.3.1
Author | SHA1 | Date | |
---|---|---|---|
e3cf6fb5f2
|
|||
bca4382988
|
|||
68aade4f1f
|
|||
9bbcc843c2
|
|||
42d91a097f
|
|||
971c846dd1
|
@ -1,7 +1,5 @@
|
|||||||
HASHTAG_FILTER = ichlausche,music,musik,nowplaying,tunetuesday,nowlistening
|
HASHTAG_FILTER = ichlausche,music,musik,nowplaying,tunetuesday,nowlistening
|
||||||
URL_FILTER = song.link,album.link,spotify.com,music.apple.com,bandcamp.com,songwhip.com
|
|
||||||
YOUTUBE_API_KEY = CHANGE_ME
|
YOUTUBE_API_KEY = CHANGE_ME
|
||||||
YOUTUBE_DISABLE = false
|
|
||||||
ODESLI_API_KEY = CHANGE_ME
|
ODESLI_API_KEY = CHANGE_ME
|
||||||
MASTODON_INSTANCE = 'metalhead.club'
|
MASTODON_INSTANCE = 'metalhead.club'
|
||||||
BASE_URL = 'https://moshingmammut.phlaym.net'
|
BASE_URL = 'https://moshingmammut.phlaym.net'
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
{
|
{
|
||||||
|
"apexskier.eslint.config.eslintConfigPath" : ".eslintrc.cjs",
|
||||||
"apexskier.eslint.config.eslintPath" : "node_modules\/@eslint\/eslintrc\/dist\/eslintrc.cjs",
|
"apexskier.eslint.config.eslintPath" : "node_modules\/@eslint\/eslintrc\/dist\/eslintrc.cjs",
|
||||||
"apexskier.typescript.config.formatDocumentOnSave" : "true",
|
"apexskier.eslint.config.fixOnSave" : "Enable",
|
||||||
|
"apexskier.typescript.config.formatDocumentOnSave" : "false",
|
||||||
"apexskier.typescript.config.isEnabledForJavascript" : "Enable",
|
"apexskier.typescript.config.isEnabledForJavascript" : "Enable",
|
||||||
"apexskier.typescript.config.organizeImportsOnSave" : "true",
|
"apexskier.typescript.config.organizeImportsOnSave" : "true",
|
||||||
"apexskier.typescript.config.userPreferences.quotePreference" : "single",
|
"apexskier.typescript.config.userPreferences.quotePreference" : "single",
|
||||||
"apexskier.typescript.config.userPreferences.useLabelDetailsInCompletionEntries" : true
|
"apexskier.typescript.config.userPreferences.useLabelDetailsInCompletionEntries" : true,
|
||||||
|
"prettier.format-on-save" : "Global Default"
|
||||||
}
|
}
|
||||||
|
11
README.md
11
README.md
@ -11,8 +11,8 @@ Having a quick overview over what is being posted can be a great way to discover
|
|||||||
|
|
||||||
This is fairly simple from a technical point of view! metalhead.club's local timeline is being watched using the
|
This is fairly simple from a technical point of view! metalhead.club's local timeline is being watched using the
|
||||||
Mastodon Streaming API over a Websocket. Every time a new post arrives, it is checked if it contains any music by
|
Mastodon Streaming API over a Websocket. Every time a new post arrives, it is checked if it contains any music by
|
||||||
checking included hashtags and URLs. A list of tags and URLs can be found in [the configuration](.env.EXAMPLE).
|
checking included hashtags and URLs. A list of tags can be found in [the configuration](.env.EXAMPLE).
|
||||||
Additionally, lins to YouTube are queried, if they are music or other videos using the YouTube API.
|
Additionally, links are vetted if they are music by checking if https://song.link finds info on them.
|
||||||
|
|
||||||
If a post passes this check it is saved to a SQLite database.
|
If a post passes this check it is saved to a SQLite database.
|
||||||
|
|
||||||
@ -93,11 +93,12 @@ and set your `User`, `Group`, `ExecStart` and `WorkingDirectory` accordingly.
|
|||||||
|
|
||||||
#### On your development machine
|
#### On your development machine
|
||||||
|
|
||||||
Copy `.env.EXAMPLE` to `.env` and add your `YOUTUBE_API_KEY`.
|
Copy `.env.EXAMPLE` to `.env` and add your `YOUTUBE_API_KEY` and `ODESLI_API_KEY`.
|
||||||
To obtain one follow [YouTube's guide](https://developers.google.com/youtube/registering_an_application) to create an
|
To obtain one follow [YouTube's guide](https://developers.google.com/youtube/registering_an_application) to create an
|
||||||
_API key_.
|
_API key_.
|
||||||
If `YOUTUBE_API_KEY` is unset, all YouTube videos will be assumed to contain music links.
|
If `YOUTUBE_API_KEY` is unset, no playlist will be updated.
|
||||||
If this is unwanted, set `YOUTUBE_DISABLE` to `true`).
|
|
||||||
|
If `ODESLI_API_KEY` is unset, your rate limit to the song.link API will be lower.
|
||||||
|
|
||||||
Run `npm run build` and copy the output folder, usually `build` to `$APP_DIR` on your server.
|
Run `npm run build` and copy the output folder, usually `build` to `$APP_DIR` on your server.
|
||||||
|
|
||||||
|
903
package-lock.json
generated
903
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "moshing-mammut",
|
"name": "moshing-mammut",
|
||||||
"version": "1.1.0",
|
"version": "1.3.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"license": "LGPL-3.0-or-later",
|
"license": "LGPL-3.0-or-later",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { log } from '$lib/log';
|
||||||
import { TimelineReader } from '$lib/server/timeline';
|
import { TimelineReader } from '$lib/server/timeline';
|
||||||
import type { HandleServerError } from '@sveltejs/kit';
|
import type { HandleServerError } from '@sveltejs/kit';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
@ -6,7 +7,7 @@ TimelineReader.init();
|
|||||||
|
|
||||||
export const handleError = (({ error }) => {
|
export const handleError = (({ error }) => {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
console.error('Something went wrong: ', error.name, error.message);
|
log.error('Something went wrong: ', error.name, error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
|
|
||||||
<style>
|
<style>
|
||||||
img {
|
img {
|
||||||
max-width: 50px;
|
max-width: 100%;
|
||||||
max-height: 50px;
|
max-height: 100%;
|
||||||
width: auto;
|
width: auto;
|
||||||
height: auto;
|
height: auto;
|
||||||
object-fit: contain;
|
object-fit: contain;
|
||||||
|
@ -24,34 +24,139 @@
|
|||||||
|
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<div class="avatar"><AvatarComponent account={post.account} /></div>
|
<div class="avatar"><AvatarComponent account={post.account} /></div>
|
||||||
<div class="post">
|
<div class="account"><AccountComponent account={post.account} /></div>
|
||||||
<div class="meta">
|
<div class="meta">
|
||||||
<AccountComponent account={post.account} />
|
<small><a href={post.url} target="_blank" title={absoluteDate}>{dateCreated}</a></small>
|
||||||
<small><a href={post.url} target="_blank" title={absoluteDate}>{dateCreated}</a></small>
|
</div>
|
||||||
</div>
|
<div class="content">{@html post.content}</div>
|
||||||
<div class="content">{@html post.content}</div>
|
<div class="song">
|
||||||
|
{#if post.songs}
|
||||||
|
{#each post.songs as song (song.pageUrl)}
|
||||||
|
<div class="info-wrapper">
|
||||||
|
<div class="bgimage" style="background-image: url({song.thumbnailUrl});" />
|
||||||
|
<a href={song.pageUrl ?? song.postedUrl} target="_blank">
|
||||||
|
<div class="info">
|
||||||
|
<img
|
||||||
|
src={song.thumbnailUrl}
|
||||||
|
class="cover"
|
||||||
|
alt="Cover for {song.artistName} - {song.title}"
|
||||||
|
/>
|
||||||
|
<span class="text">{song.artistName} - {song.title}</span>
|
||||||
|
</div>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.wrapper {
|
.wrapper {
|
||||||
display: flex;
|
display: grid;
|
||||||
}
|
grid-template-columns: 50px 1fr auto auto;
|
||||||
.post {
|
grid-template-rows: auto 1fr auto;
|
||||||
display: flex;
|
grid-template-areas:
|
||||||
flex-direction: column;
|
'avatar account account meta'
|
||||||
flex-grow: 2;
|
'avatar content content song'
|
||||||
word-break: break-word;
|
'. content content song';
|
||||||
}
|
grid-column-gap: 6px;
|
||||||
.meta {
|
column-gap: 6px;
|
||||||
display: flex;
|
grid-row-gap: 6px;
|
||||||
justify-content: space-between;
|
row-gap: 6px;
|
||||||
}
|
}
|
||||||
.avatar {
|
.avatar {
|
||||||
margin-right: 1em;
|
grid-area: avatar;
|
||||||
|
max-width: 50px;
|
||||||
|
max-height: 50px;
|
||||||
|
}
|
||||||
|
.account {
|
||||||
|
grid-area: account;
|
||||||
|
}
|
||||||
|
.meta {
|
||||||
|
grid-area: meta;
|
||||||
|
justify-self: end;
|
||||||
}
|
}
|
||||||
.content {
|
.content {
|
||||||
max-width: calc(600px - 1em - 50px);
|
grid-area: content;
|
||||||
overflow-x: auto;
|
word-break: break-word;
|
||||||
|
translate: 0 -0.5em;
|
||||||
|
}
|
||||||
|
.song {
|
||||||
|
grid-area: song;
|
||||||
|
align-self: center;
|
||||||
|
justify-self: center;
|
||||||
|
max-width: 200px;
|
||||||
|
}
|
||||||
|
.cover {
|
||||||
|
max-width: 200px;
|
||||||
|
display: block;
|
||||||
|
border-radius: 3px;
|
||||||
|
margin-bottom: 3px;
|
||||||
|
}
|
||||||
|
.bgimage {
|
||||||
|
display: none;
|
||||||
|
background-color: var(--color-bg);
|
||||||
|
}
|
||||||
|
.info {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5em;
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
.info * {
|
||||||
|
z-index: inherit;
|
||||||
|
}
|
||||||
|
@media only screen and (max-width: 650px) {
|
||||||
|
.wrapper {
|
||||||
|
grid-template-areas:
|
||||||
|
'avatar account account meta'
|
||||||
|
'content content content content'
|
||||||
|
'song song song song';
|
||||||
|
grid-row-gap: 3px;
|
||||||
|
row-gap: 3px;
|
||||||
|
}
|
||||||
|
.song {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.song,
|
||||||
|
.cover {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
.cover {
|
||||||
|
height: 60px;
|
||||||
|
}
|
||||||
|
.cover:not(.background) {
|
||||||
|
z-index: 1;
|
||||||
|
width: 60px;
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
-webkit-backdrop-filter: blur(10px);
|
||||||
|
}
|
||||||
|
.bgimage {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
height: 60px;
|
||||||
|
z-index: 0;
|
||||||
|
filter: blur(10px);
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-size: cover;
|
||||||
|
background-position: center;
|
||||||
|
}
|
||||||
|
.info {
|
||||||
|
position: relative;
|
||||||
|
top: -60px;
|
||||||
|
flex-direction: row;
|
||||||
|
}
|
||||||
|
.info-wrapper {
|
||||||
|
margin-bottom: -50px;
|
||||||
|
}
|
||||||
|
.text {
|
||||||
|
padding: 3px;
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
-webkit-backdrop-filter: blur(10px);
|
||||||
|
border-radius: 3px;
|
||||||
|
background-color: var(--color-bg-translucent);
|
||||||
|
color: var(--color-text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
32
src/lib/log.ts
Normal file
32
src/lib/log.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { env } from '$env/dynamic/private';
|
||||||
|
import { isTruthy } from '$lib/truthyString';
|
||||||
|
const { DEV } = import.meta.env;
|
||||||
|
|
||||||
|
export const enableVerboseLog = isTruthy(env.VERBOSE);
|
||||||
|
|
||||||
|
export const log = {
|
||||||
|
verbose: (...params: any[]) => {
|
||||||
|
if (!enableVerboseLog) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.debug(new Date().toISOString(), ...params);
|
||||||
|
},
|
||||||
|
debug: (...params: any[]) => {
|
||||||
|
if (!DEV) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.debug(new Date().toISOString(), ...params);
|
||||||
|
},
|
||||||
|
log: (...params: any[]) => {
|
||||||
|
console.log(new Date().toISOString(), ...params);
|
||||||
|
},
|
||||||
|
info: (...params: any[]) => {
|
||||||
|
console.info(new Date().toISOString(), ...params);
|
||||||
|
},
|
||||||
|
warn: (...params: any[]) => {
|
||||||
|
console.warn(new Date().toISOString(), ...params);
|
||||||
|
},
|
||||||
|
error: (...params: any[]) => {
|
||||||
|
console.error(new Date().toISOString(), ...params);
|
||||||
|
}
|
||||||
|
};
|
@ -1,3 +1,5 @@
|
|||||||
|
import type { SongInfo } from '$lib/odesliResponse';
|
||||||
|
|
||||||
export interface TimelineEvent {
|
export interface TimelineEvent {
|
||||||
event: string;
|
event: string;
|
||||||
payload: string;
|
payload: string;
|
||||||
@ -11,6 +13,7 @@ export interface Post {
|
|||||||
content: string;
|
content: string;
|
||||||
account: Account;
|
account: Account;
|
||||||
card?: PreviewCard;
|
card?: PreviewCard;
|
||||||
|
songs?: SongInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PreviewCard {
|
export interface PreviewCard {
|
||||||
|
@ -5,6 +5,7 @@ export type SongInfo = {
|
|||||||
title?: string;
|
title?: string;
|
||||||
artistName?: string;
|
artistName?: string;
|
||||||
thumbnailUrl?: string;
|
thumbnailUrl?: string;
|
||||||
|
postedUrl: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SongwhipReponse = {
|
export type SongwhipReponse = {
|
||||||
|
@ -1,10 +1,55 @@
|
|||||||
import { env } from '$env/dynamic/private';
|
|
||||||
import { IGNORE_USERS, MASTODON_INSTANCE } from '$env/static/private';
|
import { IGNORE_USERS, MASTODON_INSTANCE } from '$env/static/private';
|
||||||
|
import { enableVerboseLog, log } from '$lib/log';
|
||||||
import type { Account, Post, Tag } from '$lib/mastodon/response';
|
import type { Account, Post, Tag } from '$lib/mastodon/response';
|
||||||
import { isTruthy } from '$lib/truthyString';
|
import type { SongInfo } from '$lib/odesliResponse';
|
||||||
|
import { TimelineReader } from '$lib/server/timeline';
|
||||||
import sqlite3 from 'sqlite3';
|
import sqlite3 from 'sqlite3';
|
||||||
|
|
||||||
const { DEV } = import.meta.env;
|
const { DEV } = import.meta.env;
|
||||||
|
|
||||||
|
type FilterParameter = {
|
||||||
|
$limit: number | undefined | null;
|
||||||
|
$since?: string | undefined | null;
|
||||||
|
$before?: string | undefined | null;
|
||||||
|
[x: string]: string | number | undefined | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PostRow = {
|
||||||
|
id: string;
|
||||||
|
content: string;
|
||||||
|
created_at: string;
|
||||||
|
url: string;
|
||||||
|
account_id: string;
|
||||||
|
acct: string;
|
||||||
|
username: string;
|
||||||
|
display_name: string;
|
||||||
|
account_url: string;
|
||||||
|
avatar: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PostTagRow = {
|
||||||
|
post_id: string;
|
||||||
|
tag: string;
|
||||||
|
url: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type SongRow = {
|
||||||
|
post_url: string;
|
||||||
|
postedUrl: string;
|
||||||
|
overviewUrl?: string;
|
||||||
|
type: 'album' | 'song';
|
||||||
|
youtubeUrl?: string;
|
||||||
|
title?: string;
|
||||||
|
artistName?: string;
|
||||||
|
thumbnailUrl?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Migration = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
statement: string;
|
||||||
|
};
|
||||||
|
|
||||||
const db: sqlite3.Database = new sqlite3.Database('moshingmammut.db');
|
const db: sqlite3.Database = new sqlite3.Database('moshingmammut.db');
|
||||||
// for the local masto instance, the instance name is *not* saved
|
// for the local masto instance, the instance name is *not* saved
|
||||||
// as part of the username or acct, so it needs to be stripped
|
// as part of the username or acct, so it needs to be stripped
|
||||||
@ -20,38 +65,71 @@ const ignoredUsers: string[] =
|
|||||||
);
|
);
|
||||||
let databaseReady = false;
|
let databaseReady = false;
|
||||||
|
|
||||||
if (DEV && isTruthy(env.VERBOSE)) {
|
if (enableVerboseLog) {
|
||||||
sqlite3.verbose();
|
sqlite3.verbose();
|
||||||
db.on('change', (t, d, table, rowid) => {
|
db.on('change', (t, d, table, rowid) => {
|
||||||
console.debug('DB change event', t, d, table, rowid);
|
log.verbose('DB change event', t, d, table, rowid);
|
||||||
});
|
});
|
||||||
|
|
||||||
db.on('trace', (sql) => {
|
db.on('trace', (sql) => {
|
||||||
console.debug('Running', sql);
|
log.verbose('Running', sql);
|
||||||
});
|
});
|
||||||
|
|
||||||
db.on('profile', (sql) => {
|
db.on('profile', (sql) => {
|
||||||
console.debug('Finished', sql);
|
log.verbose('Finished', sql);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Migration {
|
async function applyDbMigration(migration: Migration): Promise<void> {
|
||||||
id: number;
|
return new Promise(async (resolve, reject) => {
|
||||||
name: string;
|
db.exec(migration.statement, (err) => {
|
||||||
statement: string;
|
if (err !== null) {
|
||||||
|
log.error(`Failed to apply migration ${migration.name}`, err);
|
||||||
|
reject(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function applyMigration(migration: Migration) {
|
||||||
|
if (migration.id === 4) {
|
||||||
|
// When this is run, no posts will have added song data,
|
||||||
|
// so filtering won't help
|
||||||
|
const posts = await getPostsInternal(null, null, 10000);
|
||||||
|
let current = 0;
|
||||||
|
let total = posts.length.toString().padStart(4, '0');
|
||||||
|
for (const post of posts) {
|
||||||
|
current++;
|
||||||
|
if (post.songs && post.songs.length) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
log.info(
|
||||||
|
`Fetching songs for existing post ${current.toString().padStart(4, '0')} of ${total}`,
|
||||||
|
post.url
|
||||||
|
);
|
||||||
|
const songs = await TimelineReader.getSongInfoInPost(post);
|
||||||
|
await saveSongInfoData(post.url, songs);
|
||||||
|
log.debug(`Fetched ${songs.length} songs for existing post`, post.url);
|
||||||
|
}
|
||||||
|
log.debug(`Finished fetching songs`);
|
||||||
|
} else {
|
||||||
|
await applyDbMigration(migration);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db.on('open', () => {
|
db.on('open', () => {
|
||||||
console.log('Opened database');
|
log.info('Opened database');
|
||||||
db.serialize();
|
db.serialize();
|
||||||
db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))');
|
db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))');
|
||||||
db.all('SELECT id FROM migrations', (err, rows: Migration[]) => {
|
db.all('SELECT id FROM migrations', (err, rows: Migration[]) => {
|
||||||
if (err !== null) {
|
if (err !== null) {
|
||||||
console.error('Could not fetch existing migrations', err);
|
log.error('Could not fetch existing migrations', err);
|
||||||
databaseReady = true;
|
databaseReady = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.debug('Already applied migrations', rows);
|
log.debug('Already applied migrations', rows);
|
||||||
const appliedMigrations: Set<number> = new Set(rows.map((row) => row['id']));
|
const appliedMigrations: Set<number> = new Set(rows.map((row) => row['id']));
|
||||||
const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id));
|
const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id));
|
||||||
let remaining = toApply.length;
|
let remaining = toApply.length;
|
||||||
@ -60,7 +138,7 @@ db.on('open', () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (const migration of toApply) {
|
for (const migration of toApply) {
|
||||||
db.exec(migration.statement, (err) => {
|
applyMigration(migration).then(() => {
|
||||||
remaining--;
|
remaining--;
|
||||||
// This will set databaseReady to true before the migration has been inserted as applies,
|
// This will set databaseReady to true before the migration has been inserted as applies,
|
||||||
// but that doesn't matter. It's only important that is has been applied
|
// but that doesn't matter. It's only important that is has been applied
|
||||||
@ -68,7 +146,7 @@ db.on('open', () => {
|
|||||||
databaseReady = true;
|
databaseReady = true;
|
||||||
}
|
}
|
||||||
if (err !== null) {
|
if (err !== null) {
|
||||||
console.error(`Failed to apply migration ${migration.name}`, err);
|
log.error(`Failed to apply migration ${migration.name}`, err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
db.run(
|
db.run(
|
||||||
@ -76,10 +154,10 @@ db.on('open', () => {
|
|||||||
[migration.id, migration.name],
|
[migration.id, migration.name],
|
||||||
(e: Error) => {
|
(e: Error) => {
|
||||||
if (e !== null) {
|
if (e !== null) {
|
||||||
console.error(`Failed to mark migration ${migration.name} as applied`, e);
|
log.error(`Failed to mark migration ${migration.name} as applied`, e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.info(`Applied migration ${migration.name}`);
|
log.info(`Applied migration ${migration.name}`);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -87,7 +165,7 @@ db.on('open', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
db.on('error', (err) => {
|
db.on('error', (err) => {
|
||||||
console.error('Error opening database', err);
|
log.error('Error opening database', err);
|
||||||
});
|
});
|
||||||
|
|
||||||
function getMigrations(): Migration[] {
|
function getMigrations(): Migration[] {
|
||||||
@ -170,35 +248,52 @@ function getMigrations(): Migration[] {
|
|||||||
DROP TABLE poststags;
|
DROP TABLE poststags;
|
||||||
ALTER TABLE poststags_new RENAME TO poststags;
|
ALTER TABLE poststags_new RENAME TO poststags;
|
||||||
`
|
`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
name: 'song info for posts',
|
||||||
|
statement: `
|
||||||
|
CREATE TABLE songs (
|
||||||
|
id integer PRIMARY KEY,
|
||||||
|
postedUrl TEXT NOT NULL,
|
||||||
|
overviewUrl TEXT,
|
||||||
|
type TEXT CHECK ( type in ('album', 'song') ),
|
||||||
|
youtubeUrl TEXT,
|
||||||
|
title TEXT,
|
||||||
|
artistName TEXT,
|
||||||
|
thumbnailUrl TEXT,
|
||||||
|
post_url TEXT,
|
||||||
|
FOREIGN KEY (post_url) REFERENCES posts(url)
|
||||||
|
);`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
name: 'song info for existing posts',
|
||||||
|
statement: ``
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function waitReady(): Promise<undefined> {
|
async function waitReady(): Promise<void> {
|
||||||
// Simpler than a semaphore and is really only needed on startup
|
// Simpler than a semaphore and is really only needed on startup
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
if (DEV) {
|
if (DEV) {
|
||||||
console.debug('Waiting for database to be ready');
|
log.debug('Waiting for database to be ready');
|
||||||
}
|
}
|
||||||
if (databaseReady) {
|
if (databaseReady) {
|
||||||
if (DEV) {
|
if (DEV) {
|
||||||
console.debug('DB is ready');
|
log.debug('DB is ready');
|
||||||
}
|
}
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
resolve(undefined);
|
resolve();
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function savePost(post: Post): Promise<undefined> {
|
function saveAccountData(account: Account): Promise<void> {
|
||||||
if (!databaseReady) {
|
return new Promise<void>((resolve, reject) => {
|
||||||
await waitReady();
|
|
||||||
}
|
|
||||||
return await new Promise<undefined>((resolve, reject) => {
|
|
||||||
console.debug(`Saving post ${post.url}`);
|
|
||||||
const account = post.account;
|
|
||||||
db.run(
|
db.run(
|
||||||
`
|
`
|
||||||
INSERT INTO accounts (id, acct, username, display_name, url, avatar)
|
INSERT INTO accounts (id, acct, username, display_name, url, avatar)
|
||||||
@ -220,192 +315,297 @@ export async function savePost(post: Post): Promise<undefined> {
|
|||||||
],
|
],
|
||||||
(err) => {
|
(err) => {
|
||||||
if (err !== null) {
|
if (err !== null) {
|
||||||
console.error(`Could not insert/update account ${account.id}`, err);
|
log.error(`Could not insert/update account ${account.id}`, err);
|
||||||
reject(err);
|
reject(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
db.run(
|
resolve();
|
||||||
`
|
|
||||||
INSERT INTO posts (id, content, created_at, url, account_id)
|
|
||||||
VALUES (?, ?, ?, ?, ?) ON CONFLICT(url) DO UPDATE SET
|
|
||||||
content=excluded.content,
|
|
||||||
created_at=excluded.created_at,
|
|
||||||
id=excluded.id,
|
|
||||||
account_id=excluded.account_id;`,
|
|
||||||
[post.id, post.content, post.created_at, post.url, post.account.url],
|
|
||||||
(postErr) => {
|
|
||||||
if (postErr !== null) {
|
|
||||||
console.error(`Could not insert post ${post.url}`, postErr);
|
|
||||||
reject(postErr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!post.tags.length) {
|
|
||||||
resolve(undefined);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.parallelize(() => {
|
|
||||||
let remaining = post.tags.length;
|
|
||||||
for (const tag of post.tags) {
|
|
||||||
db.run(
|
|
||||||
`
|
|
||||||
INSERT INTO tags (url, tag) VALUES (?, ?)
|
|
||||||
ON CONFLICT(url) DO UPDATE SET
|
|
||||||
tag=excluded.tag;`,
|
|
||||||
[tag.url, tag.name],
|
|
||||||
(tagErr) => {
|
|
||||||
if (tagErr !== null) {
|
|
||||||
console.error(`Could not insert/update tag ${tag.url}`, tagErr);
|
|
||||||
reject(tagErr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
db.run(
|
|
||||||
'INSERT INTO poststags (post_id, tag_url) VALUES (?, ?)',
|
|
||||||
[post.url, tag.url],
|
|
||||||
(posttagserr) => {
|
|
||||||
if (posttagserr !== null) {
|
|
||||||
console.error(
|
|
||||||
`Could not insert poststags ${tag.url}, ${post.url}`,
|
|
||||||
posttagserr
|
|
||||||
);
|
|
||||||
reject(posttagserr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Don't decrease on fail
|
|
||||||
remaining--;
|
|
||||||
// Only resolve after all have been inserted
|
|
||||||
if (remaining === 0) {
|
|
||||||
resolve(undefined);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
type FilterParameter = {
|
function savePostData(post: Post): Promise<void> {
|
||||||
$limit: number | undefined | null;
|
return new Promise<void>((resolve, reject) => {
|
||||||
$since?: string | undefined | null;
|
db.run(
|
||||||
$before?: string | undefined | null;
|
`
|
||||||
[x: string]: string | number | undefined | null;
|
INSERT INTO posts (id, content, created_at, url, account_id)
|
||||||
};
|
VALUES (?, ?, ?, ?, ?) ON CONFLICT(url) DO UPDATE SET
|
||||||
|
content=excluded.content,
|
||||||
|
created_at=excluded.created_at,
|
||||||
|
id=excluded.id,
|
||||||
|
account_id=excluded.account_id;`,
|
||||||
|
[post.id, post.content, post.created_at, post.url, post.account.url],
|
||||||
|
(postErr) => {
|
||||||
|
if (postErr !== null) {
|
||||||
|
log.error(`Could not insert post ${post.url}`, postErr);
|
||||||
|
reject(postErr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export async function getPosts(since: string | null, before: string | null, limit: number) {
|
function savePostTagData(post: Post): Promise<void> {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
if (!post.tags.length) {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
db.parallelize(() => {
|
||||||
|
let remaining = post.tags.length;
|
||||||
|
for (const tag of post.tags) {
|
||||||
|
db.run(
|
||||||
|
`
|
||||||
|
INSERT INTO tags (url, tag) VALUES (?, ?)
|
||||||
|
ON CONFLICT(url) DO UPDATE SET
|
||||||
|
tag=excluded.tag;`,
|
||||||
|
[tag.url, tag.name],
|
||||||
|
(tagErr) => {
|
||||||
|
if (tagErr !== null) {
|
||||||
|
log.error(`Could not insert/update tag ${tag.url}`, tagErr);
|
||||||
|
reject(tagErr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
db.run(
|
||||||
|
'INSERT INTO poststags (post_id, tag_url) VALUES (?, ?)',
|
||||||
|
[post.url, tag.url],
|
||||||
|
(posttagserr) => {
|
||||||
|
if (posttagserr !== null) {
|
||||||
|
log.error(`Could not insert poststags ${tag.url}, ${post.url}`, posttagserr);
|
||||||
|
reject(posttagserr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Don't decrease on fail
|
||||||
|
remaining--;
|
||||||
|
// Only resolve after all have been inserted
|
||||||
|
if (remaining === 0) {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveSongInfoData(postUrl: string, songs: SongInfo[]): Promise<void> {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
if (songs.length === 0) {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
db.parallelize(() => {
|
||||||
|
let remaining = songs.length;
|
||||||
|
for (const song of songs) {
|
||||||
|
db.run(
|
||||||
|
`
|
||||||
|
INSERT INTO songs (postedUrl, overviewUrl, type, youtubeUrl, title, artistName, thumbnailUrl, post_url)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`,
|
||||||
|
[
|
||||||
|
song.postedUrl,
|
||||||
|
song.pageUrl,
|
||||||
|
song.type,
|
||||||
|
song.youtubeUrl,
|
||||||
|
song.title,
|
||||||
|
song.artistName,
|
||||||
|
song.thumbnailUrl,
|
||||||
|
postUrl
|
||||||
|
],
|
||||||
|
(songErr) => {
|
||||||
|
if (songErr !== null) {
|
||||||
|
log.error(`Could not insert song ${song.postedUrl}`, songErr);
|
||||||
|
reject(songErr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Don't decrease on fail
|
||||||
|
remaining--;
|
||||||
|
// Only resolve after all have been inserted
|
||||||
|
if (remaining === 0) {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function savePost(post: Post, songs: SongInfo[]) {
|
||||||
if (!databaseReady) {
|
if (!databaseReady) {
|
||||||
await waitReady();
|
await waitReady();
|
||||||
}
|
}
|
||||||
const promise = await new Promise<Post[]>((resolve, reject) => {
|
|
||||||
let filter_query = '';
|
|
||||||
const params: FilterParameter = { $limit: limit };
|
|
||||||
if (since === null && before === null) {
|
|
||||||
filter_query = '';
|
|
||||||
} else if (since !== null) {
|
|
||||||
filter_query = 'WHERE posts.created_at > $since';
|
|
||||||
params.$since = since;
|
|
||||||
} else if (before !== null) {
|
|
||||||
// Setting both, before and since doesn't make sense, so this case is not explicitly handled
|
|
||||||
filter_query = 'WHERE posts.created_at < $before';
|
|
||||||
params.$before = before;
|
|
||||||
}
|
|
||||||
|
|
||||||
ignoredUsers.forEach((ignoredUser, index) => {
|
log.debug(`Saving post ${post.url}`);
|
||||||
const userParam = `$user_${index}`;
|
const account = post.account;
|
||||||
const acctParam = userParam + 'a';
|
await saveAccountData(account);
|
||||||
const usernameParam = userParam + 'u';
|
log.debug(`Saved account data ${post.url}`);
|
||||||
const prefix = filter_query === '' ? ' WHERE' : ' AND';
|
await savePostData(post);
|
||||||
filter_query += `${prefix} acct != ${acctParam} AND username != ${usernameParam} `;
|
log.debug(`Saved post data ${post.url}`);
|
||||||
params[acctParam] = ignoredUser;
|
await savePostTagData(post);
|
||||||
params[usernameParam] = ignoredUser;
|
log.debug(`Saved ${post.tags.length} tag data ${post.url}`);
|
||||||
});
|
await saveSongInfoData(post.url, songs);
|
||||||
|
log.debug(`Saved ${songs.length} song info data ${post.url}`);
|
||||||
|
}
|
||||||
|
|
||||||
type PostResult = {
|
function getPostData(filterQuery: string, params: FilterParameter): Promise<PostRow[]> {
|
||||||
id: string;
|
const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url,
|
||||||
content: string;
|
accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name,
|
||||||
created_at: string;
|
accounts.url AS account_url, accounts.avatar
|
||||||
url: string;
|
FROM posts
|
||||||
account_id: string;
|
JOIN accounts ON posts.account_id = accounts.url
|
||||||
acct: string;
|
${filterQuery}
|
||||||
username: string;
|
ORDER BY created_at DESC
|
||||||
display_name: string;
|
LIMIT $limit`;
|
||||||
account_url: string;
|
|
||||||
avatar: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type PostTagResult = {
|
return new Promise((resolve, reject) => {
|
||||||
post_id: string;
|
db.all(sql, params, (err, rows: PostRow[]) => {
|
||||||
tag: string;
|
|
||||||
url: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url,
|
|
||||||
accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name,
|
|
||||||
accounts.url AS account_url, accounts.avatar
|
|
||||||
FROM posts
|
|
||||||
JOIN accounts ON posts.account_id = accounts.url
|
|
||||||
${filter_query}
|
|
||||||
ORDER BY created_at DESC
|
|
||||||
LIMIT $limit`;
|
|
||||||
db.all(sql, params, (err, rows: PostResult[]) => {
|
|
||||||
if (err != null) {
|
if (err != null) {
|
||||||
console.error('Error loading posts', err);
|
log.error('Error loading posts', err);
|
||||||
reject(err);
|
reject(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (rows.length === 0) {
|
resolve(rows);
|
||||||
// No need to check for tags
|
|
||||||
resolve([]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const postIdsParams = rows.map(() => '?').join(', ');
|
|
||||||
db.all(
|
|
||||||
`SELECT post_id, tags.url, tags.tag
|
|
||||||
FROM poststags
|
|
||||||
JOIN tags ON poststags.tag_url = tags.url
|
|
||||||
WHERE post_id IN (${postIdsParams});`,
|
|
||||||
rows.map((r: PostResult) => r.url),
|
|
||||||
(tagErr, tagRows: PostTagResult[]) => {
|
|
||||||
if (tagErr != null) {
|
|
||||||
console.error('Error loading post tags', tagErr);
|
|
||||||
reject(tagErr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const tagMap: Map<string, Tag[]> = tagRows.reduce((result: Map<string, Tag[]>, item) => {
|
|
||||||
const tag: Tag = {
|
|
||||||
url: item.url,
|
|
||||||
name: item.tag
|
|
||||||
};
|
|
||||||
result.set(item.post_id, [...(result.get(item.post_id) || []), tag]);
|
|
||||||
return result;
|
|
||||||
}, new Map());
|
|
||||||
const posts = rows.map((row) => {
|
|
||||||
return {
|
|
||||||
id: row.id,
|
|
||||||
content: row.content,
|
|
||||||
created_at: row.created_at,
|
|
||||||
url: row.url,
|
|
||||||
tags: tagMap.get(row.id) || [],
|
|
||||||
account: {
|
|
||||||
id: row.account_id,
|
|
||||||
acct: row.acct,
|
|
||||||
username: row.username,
|
|
||||||
display_name: row.display_name,
|
|
||||||
url: row.account_url,
|
|
||||||
avatar: row.avatar
|
|
||||||
} as Account
|
|
||||||
} as Post;
|
|
||||||
});
|
|
||||||
resolve(posts);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return promise;
|
}
|
||||||
|
|
||||||
|
function getTagData(postIdsParams: String, postIds: string[]): Promise<Map<string, Tag[]>> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db.all(
|
||||||
|
`SELECT post_id, tags.url, tags.tag
|
||||||
|
FROM poststags
|
||||||
|
JOIN tags ON poststags.tag_url = tags.url
|
||||||
|
WHERE post_id IN (${postIdsParams});`,
|
||||||
|
postIds,
|
||||||
|
(tagErr, tagRows: PostTagRow[]) => {
|
||||||
|
if (tagErr != null) {
|
||||||
|
log.error('Error loading post tags', tagErr);
|
||||||
|
reject(tagErr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const tagMap: Map<string, Tag[]> = tagRows.reduce((result: Map<string, Tag[]>, item) => {
|
||||||
|
const tag: Tag = {
|
||||||
|
url: item.url,
|
||||||
|
name: item.tag
|
||||||
|
};
|
||||||
|
result.set(item.post_id, [...(result.get(item.post_id) || []), tag]);
|
||||||
|
return result;
|
||||||
|
}, new Map());
|
||||||
|
resolve(tagMap);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSongData(postIdsParams: String, postIds: string[]): Promise<Map<string, SongInfo[]>> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db.all(
|
||||||
|
`SELECT post_url, songs.postedUrl, songs.overviewUrl, songs.type, songs.youtubeUrl,
|
||||||
|
songs.title, songs.artistName, songs.thumbnailUrl, songs.post_url
|
||||||
|
FROM songs
|
||||||
|
WHERE post_url IN (${postIdsParams});`,
|
||||||
|
postIds,
|
||||||
|
(tagErr, tagRows: SongRow[]) => {
|
||||||
|
if (tagErr != null) {
|
||||||
|
log.error('Error loading post tags', tagErr);
|
||||||
|
reject(tagErr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const songMap: Map<string, SongInfo[]> = tagRows.reduce(
|
||||||
|
(result: Map<string, SongInfo[]>, item) => {
|
||||||
|
const info = {
|
||||||
|
pageUrl: item.overviewUrl,
|
||||||
|
youtubeUrl: item.youtubeUrl,
|
||||||
|
type: item.type,
|
||||||
|
title: item.title,
|
||||||
|
artistName: item.artistName,
|
||||||
|
thumbnailUrl: item.thumbnailUrl,
|
||||||
|
postedUrl: item.postedUrl
|
||||||
|
} as SongInfo;
|
||||||
|
result.set(item.post_url, [...(result.get(item.post_url) || []), info]);
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
new Map()
|
||||||
|
);
|
||||||
|
resolve(songMap);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getPosts(
|
||||||
|
since: string | null,
|
||||||
|
before: string | null,
|
||||||
|
limit: number
|
||||||
|
): Promise<Post[]> {
|
||||||
|
if (!databaseReady) {
|
||||||
|
await waitReady();
|
||||||
|
}
|
||||||
|
return await getPostsInternal(since, before, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPostsInternal(
|
||||||
|
since: string | null,
|
||||||
|
before: string | null,
|
||||||
|
limit: number
|
||||||
|
): Promise<Post[]> {
|
||||||
|
let filterQuery = '';
|
||||||
|
const params: FilterParameter = { $limit: limit };
|
||||||
|
if (since === null && before === null) {
|
||||||
|
filterQuery = '';
|
||||||
|
} else if (since !== null) {
|
||||||
|
filterQuery = 'WHERE posts.created_at > $since';
|
||||||
|
params.$since = since;
|
||||||
|
} else if (before !== null) {
|
||||||
|
// Setting both, before and since doesn't make sense, so this case is not explicitly handled
|
||||||
|
filterQuery = 'WHERE posts.created_at < $before';
|
||||||
|
params.$before = before;
|
||||||
|
}
|
||||||
|
|
||||||
|
ignoredUsers.forEach((ignoredUser, index) => {
|
||||||
|
const userParam = `$user_${index}`;
|
||||||
|
const acctParam = userParam + 'a';
|
||||||
|
const usernameParam = userParam + 'u';
|
||||||
|
const prefix = filterQuery === '' ? ' WHERE' : ' AND';
|
||||||
|
filterQuery += `${prefix} acct != ${acctParam} AND username != ${usernameParam} `;
|
||||||
|
params[acctParam] = ignoredUser;
|
||||||
|
params[usernameParam] = ignoredUser;
|
||||||
|
});
|
||||||
|
|
||||||
|
const rows = await getPostData(filterQuery, params);
|
||||||
|
if (rows.length === 0) {
|
||||||
|
// No need to check for tags and songs
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const postIdsParams = rows.map(() => '?').join(', ');
|
||||||
|
const postIds = rows.map((r: PostRow) => r.url);
|
||||||
|
const tagMap = await getTagData(postIdsParams, postIds);
|
||||||
|
const songMap = await getSongData(postIdsParams, postIds);
|
||||||
|
const posts = rows.map((row) => {
|
||||||
|
return {
|
||||||
|
id: row.id,
|
||||||
|
content: row.content,
|
||||||
|
created_at: row.created_at,
|
||||||
|
url: row.url,
|
||||||
|
tags: tagMap.get(row.url) || [],
|
||||||
|
account: {
|
||||||
|
id: row.account_id,
|
||||||
|
acct: row.acct,
|
||||||
|
username: row.username,
|
||||||
|
display_name: row.display_name,
|
||||||
|
url: row.account_url,
|
||||||
|
avatar: row.avatar
|
||||||
|
} as Account,
|
||||||
|
songs: songMap.get(row.url) || []
|
||||||
|
} as Post;
|
||||||
|
});
|
||||||
|
return posts;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { BASE_URL, WEBSUB_HUB } from '$env/static/private';
|
import { BASE_URL, WEBSUB_HUB } from '$env/static/private';
|
||||||
import { PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME } from '$env/static/public';
|
import { PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME } from '$env/static/public';
|
||||||
import type { Post } from '$lib//mastodon/response';
|
import type { Post } from '$lib//mastodon/response';
|
||||||
|
import { log } from '$lib/log';
|
||||||
import { Feed } from 'feed';
|
import { Feed } from 'feed';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
|
|
||||||
@ -59,6 +60,6 @@ export async function saveAtomFeed(feed: Feed) {
|
|||||||
body: params
|
body: params
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to update WebSub hub', e);
|
log.error('Failed to update WebSub hub', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,101 +1,51 @@
|
|||||||
import {
|
import { HASHTAG_FILTER, MASTODON_INSTANCE, ODESLI_API_KEY } from '$env/static/private';
|
||||||
HASHTAG_FILTER,
|
import { log } from '$lib/log';
|
||||||
MASTODON_INSTANCE,
|
|
||||||
ODESLI_API_KEY,
|
|
||||||
URL_FILTER,
|
|
||||||
YOUTUBE_API_KEY,
|
|
||||||
YOUTUBE_DISABLE
|
|
||||||
} from '$env/static/private';
|
|
||||||
import type { Post, Tag, TimelineEvent } from '$lib/mastodon/response';
|
import type { Post, Tag, TimelineEvent } from '$lib/mastodon/response';
|
||||||
import type { OdesliResponse, Platform, SongInfo } from '$lib/odesliResponse';
|
import type { OdesliResponse, Platform, SongInfo } from '$lib/odesliResponse';
|
||||||
import { getPosts, savePost } from '$lib/server/db';
|
import { getPosts, savePost } from '$lib/server/db';
|
||||||
import { createFeed, saveAtomFeed } from '$lib/server/rss';
|
import { createFeed, saveAtomFeed } from '$lib/server/rss';
|
||||||
import { sleep } from '$lib/sleep';
|
import { sleep } from '$lib/sleep';
|
||||||
import { isTruthy } from '$lib/truthyString';
|
|
||||||
import { WebSocket } from 'ws';
|
import { WebSocket } from 'ws';
|
||||||
|
|
||||||
const YOUTUBE_REGEX = new RegExp(
|
|
||||||
/https?:\/\/(www\.)?youtu((be.com\/.*?v=)|(\.be\/))(?<videoId>[a-zA-Z_0-9-]+)/gm
|
|
||||||
);
|
|
||||||
|
|
||||||
const URL_REGEX = new RegExp(/href="(?<postUrl>[^>]+?)" target="_blank"/gm);
|
const URL_REGEX = new RegExp(/href="(?<postUrl>[^>]+?)" target="_blank"/gm);
|
||||||
|
|
||||||
export class TimelineReader {
|
export class TimelineReader {
|
||||||
private static _instance: TimelineReader;
|
private static _instance: TimelineReader;
|
||||||
|
|
||||||
private static async isMusicVideo(videoId: string) {
|
public static async getSongInfoInPost(post: Post): Promise<SongInfo[]> {
|
||||||
if (!YOUTUBE_API_KEY || YOUTUBE_API_KEY === 'CHANGE_ME') {
|
const urlMatches = post.content.matchAll(URL_REGEX);
|
||||||
// Assume that it *is* a music link when no YT API key is provided
|
const songs: SongInfo[] = [];
|
||||||
// If it should assumed to not be YOUTUBE_DISABLE needs to be set to something truthy
|
for (const match of urlMatches) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const searchParams = new URLSearchParams([
|
|
||||||
['part', 'snippet'],
|
|
||||||
['id', videoId],
|
|
||||||
['key', YOUTUBE_API_KEY]
|
|
||||||
]);
|
|
||||||
const youtubeVideoUrl = new URL(`https://www.googleapis.com/youtube/v3/videos?${searchParams}`);
|
|
||||||
const resp = await fetch(youtubeVideoUrl);
|
|
||||||
const respObj = await resp.json();
|
|
||||||
if (!respObj.items.length) {
|
|
||||||
console.warn('Could not find video with id', videoId);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const item = respObj.items[0];
|
|
||||||
if (item.tags?.includes('music')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const categorySearchParams = new URLSearchParams([
|
|
||||||
['part', 'snippet'],
|
|
||||||
['id', item.categoryId],
|
|
||||||
['key', YOUTUBE_API_KEY]
|
|
||||||
]);
|
|
||||||
const youtubeCategoryUrl = new URL(
|
|
||||||
`https://www.googleapis.com/youtube/v3/videoCategories?${categorySearchParams}`
|
|
||||||
);
|
|
||||||
const categoryTitle: string = await fetch(youtubeCategoryUrl)
|
|
||||||
.then((r) => r.json())
|
|
||||||
.then((r) => r.items[0]?.title);
|
|
||||||
return categoryTitle === 'Music';
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async checkYoutubeMatches(postContent: string): Promise<string | null> {
|
|
||||||
if (isTruthy(YOUTUBE_DISABLE)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const matches = postContent.matchAll(YOUTUBE_REGEX);
|
|
||||||
for (const match of matches) {
|
|
||||||
if (match === undefined || match.groups === undefined) {
|
if (match === undefined || match.groups === undefined) {
|
||||||
|
log.warn('Match listed in allMatches, but either it or its groups are undefined', match);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const videoId = match.groups.videoId.toString();
|
const urlMatch = match.groups.postUrl.toString();
|
||||||
|
let url: URL;
|
||||||
try {
|
try {
|
||||||
const isMusic = await TimelineReader.isMusicVideo(videoId);
|
url = new URL(urlMatch);
|
||||||
if (isMusic) {
|
|
||||||
return match[0];
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Could not check if', videoId, 'is a music video', e);
|
log.error('URL found via Regex does not seem to be a valud url', urlMatch, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check *all* found url and let odesli determine if it is music or not
|
||||||
|
log.debug(`Checking ${url} if it contains song data`);
|
||||||
|
const info = await TimelineReader.getSongInfo(url);
|
||||||
|
log.debug(`Found song info for ${url}?`, info);
|
||||||
|
if (info) {
|
||||||
|
songs.push(info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async getSongInfo(url: string, remainingTries = 6): Promise<SongInfo | null> {
|
private static async getSongInfo(url: URL, remainingTries = 6): Promise<SongInfo | null> {
|
||||||
if (remainingTries === 0) {
|
if (remainingTries === 0) {
|
||||||
console.error('No tries remaining. Lookup failed!');
|
log.error('No tries remaining. Lookup failed!');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let hostname: string;
|
if (url.hostname === 'songwhip.com') {
|
||||||
try {
|
|
||||||
hostname = new URL(url).hostname;
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`Could not construct URL ${url}`, e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (hostname === 'songwhip.com') {
|
|
||||||
// song.link doesn't support songwhip links and songwhip themselves will provide metadata if you pass in a
|
// song.link doesn't support songwhip links and songwhip themselves will provide metadata if you pass in a
|
||||||
// Apple Music/Spotify/etc link, but won't when provided with their own link, so no way to extract song info
|
// Apple Music/Spotify/etc link, but won't when provided with their own link, so no way to extract song info
|
||||||
// except maybe scraping their HTML
|
// except maybe scraping their HTML
|
||||||
@ -103,7 +53,7 @@ export class TimelineReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const odesliParams = new URLSearchParams();
|
const odesliParams = new URLSearchParams();
|
||||||
odesliParams.append('url', url);
|
odesliParams.append('url', url.toString());
|
||||||
odesliParams.append('userCountry', 'DE');
|
odesliParams.append('userCountry', 'DE');
|
||||||
odesliParams.append('songIfSingle', 'true');
|
odesliParams.append('songIfSingle', 'true');
|
||||||
if (ODESLI_API_KEY && ODESLI_API_KEY !== 'CHANGE_ME') {
|
if (ODESLI_API_KEY && ODESLI_API_KEY !== 'CHANGE_ME') {
|
||||||
@ -115,49 +65,34 @@ export class TimelineReader {
|
|||||||
if (response.status === 429) {
|
if (response.status === 429) {
|
||||||
throw new Error('Rate limit reached', { cause: 429 });
|
throw new Error('Rate limit reached', { cause: 429 });
|
||||||
}
|
}
|
||||||
return response.json().then((odesliInfo: OdesliResponse) => {
|
const odesliInfo: OdesliResponse = await response.json();
|
||||||
const info = odesliInfo.entitiesByUniqueId[odesliInfo.entityUniqueId];
|
if (!odesliInfo || !odesliInfo.entitiesByUniqueId || !odesliInfo.entityUniqueId) {
|
||||||
const platform: Platform = 'youtube';
|
return null;
|
||||||
return {
|
}
|
||||||
...info,
|
const info = odesliInfo.entitiesByUniqueId[odesliInfo.entityUniqueId];
|
||||||
pageUrl: odesliInfo.pageUrl,
|
const platform: Platform = 'youtube';
|
||||||
youtubeUrl: odesliInfo.linksByPlatform[platform]?.url
|
return {
|
||||||
} as SongInfo;
|
...info,
|
||||||
});
|
pageUrl: odesliInfo.pageUrl,
|
||||||
|
youtubeUrl: odesliInfo.linksByPlatform[platform]?.url,
|
||||||
|
postedUrl: url.toString()
|
||||||
|
} as SongInfo;
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Error && e.cause === 429) {
|
if (e instanceof Error && e.cause === 429) {
|
||||||
console.warn('song.link rate limit reached. Trying again in 10 seconds');
|
log.warn('song.link rate limit reached. Trying again in 10 seconds');
|
||||||
await sleep(10_000);
|
await sleep(10_000);
|
||||||
return await this.getSongInfo(url, remainingTries - 1);
|
return await this.getSongInfo(url, remainingTries - 1);
|
||||||
}
|
}
|
||||||
console.error(`Failed to load ${url} info from song.link`, e);
|
log.error(`Failed to load ${url} info from song.link`, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async getUrlFromPreviewCard(post: Post): Promise<string | undefined> {
|
|
||||||
return undefined;
|
|
||||||
// Currently disabled, because it seems to always be null, even after re-fetching the post from Mastodon
|
|
||||||
/*
|
|
||||||
if (post.card) {
|
|
||||||
return post.card?.url;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const status: Post = await (
|
|
||||||
await fetch(`https://${MASTODON_INSTANCE}/api/v1/statuses/${post.id}`)
|
|
||||||
).json();
|
|
||||||
return status.card?.url;
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`Could not fetch status ${post.url}`, e);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
private startWebsocket() {
|
private startWebsocket() {
|
||||||
const socket = new WebSocket(`wss://${MASTODON_INSTANCE}/api/v1/streaming`);
|
const socket = new WebSocket(`wss://${MASTODON_INSTANCE}/api/v1/streaming`);
|
||||||
socket.onopen = () => {
|
socket.onopen = () => {
|
||||||
console.log('Connected to WS');
|
log.log('Connected to WS');
|
||||||
socket.send('{ "type": "subscribe", "stream": "public:local"}');
|
socket.send('{ "type": "subscribe", "stream": "public:local"}');
|
||||||
};
|
};
|
||||||
socket.onmessage = async (event) => {
|
socket.onmessage = async (event) => {
|
||||||
@ -170,88 +105,35 @@ export class TimelineReader {
|
|||||||
const hashttags: string[] = HASHTAG_FILTER.split(',');
|
const hashttags: string[] = HASHTAG_FILTER.split(',');
|
||||||
const found_tags: Tag[] = post.tags.filter((t: Tag) => hashttags.includes(t.name));
|
const found_tags: Tag[] = post.tags.filter((t: Tag) => hashttags.includes(t.name));
|
||||||
|
|
||||||
const urls: string[] = URL_FILTER.split(',');
|
const songs = await TimelineReader.getSongInfoInPost(post);
|
||||||
const found_urls = urls.filter((t) => post.content.includes(t));
|
|
||||||
const urlsToCheck: string[] = [];
|
|
||||||
// If we don't have any tags or non-youtube urls, check youtube
|
// If we don't have any tags or non-youtube urls, check youtube
|
||||||
// YT is handled separately, because it requires an API call and therefore is slower
|
// YT is handled separately, because it requires an API call and therefore is slower
|
||||||
if (found_urls.length === 0 && found_tags.length === 0) {
|
if (songs.length === 0 && found_tags.length === 0) {
|
||||||
const youtubeUrl = await TimelineReader.checkYoutubeMatches(post.content);
|
log.log('Ignoring post', post.url);
|
||||||
if (youtubeUrl === null) {
|
return;
|
||||||
console.log('Ignoring post', post.url);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
urlsToCheck.push(youtubeUrl);
|
|
||||||
console.log('Found YT URL', youtubeUrl, found_urls, found_urls.length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Change URL detection above to use this regex.
|
await savePost(post, songs);
|
||||||
// Looks like we're stuck with regex for now instead of using preview cards.
|
log.debug('Saved post', post.url);
|
||||||
// Might as well use it to find URLs. Could also use this for YouTube: If Odesli finds something, it's a song,
|
|
||||||
// if not, ignore it. No need to consult the YT API and give those links a special handling
|
|
||||||
const musicUrls: string[] = [];
|
|
||||||
const musicUrl = await TimelineReader.getUrlFromPreviewCard(post);
|
|
||||||
if (musicUrl) {
|
|
||||||
musicUrls.push(musicUrl);
|
|
||||||
} else {
|
|
||||||
const urlMatches = post.content.matchAll(URL_REGEX);
|
|
||||||
for (const match of urlMatches) {
|
|
||||||
if (match === undefined || match.groups === undefined) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const urlMatch = match.groups.postUrl.toString();
|
|
||||||
const musicUrl = urls.find((u) => urlMatch.includes(u));
|
|
||||||
if (musicUrl) {
|
|
||||||
musicUrls.push(urlMatch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const url of musicUrls) {
|
|
||||||
let hostname: string | null = null;
|
|
||||||
try {
|
|
||||||
hostname = new URL(url).hostname;
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`Could not check hostname for URL ${url}`, e);
|
|
||||||
}
|
|
||||||
if (hostname === 'songwhip.com') {
|
|
||||||
// TODO: Implement checking the songwhip API
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const info = await TimelineReader.getSongInfo(url);
|
|
||||||
if (info) {
|
|
||||||
console.info(
|
|
||||||
'Got song info for',
|
|
||||||
post.url,
|
|
||||||
url,
|
|
||||||
info.artistName,
|
|
||||||
info.title,
|
|
||||||
info.thumbnailUrl,
|
|
||||||
info.pageUrl,
|
|
||||||
info.youtubeUrl
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await savePost(post);
|
|
||||||
|
|
||||||
const posts = await getPosts(null, null, 100);
|
const posts = await getPosts(null, null, 100);
|
||||||
await saveAtomFeed(createFeed(posts));
|
await saveAtomFeed(createFeed(posts));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('error message', event, event.data, e);
|
log.error('error message', event, event.data, e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
socket.onclose = (event) => {
|
socket.onclose = (event) => {
|
||||||
console.warn(
|
log.warn(
|
||||||
`Websocket connection to ${MASTODON_INSTANCE} closed. Code: ${event.code}, reason: '${event.reason}'`
|
`Websocket connection to ${MASTODON_INSTANCE} closed. Code: ${event.code}, reason: '${event.reason}'`
|
||||||
);
|
);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
console.info(`Attempting to reconenct to WS`);
|
log.info(`Attempting to reconenct to WS`);
|
||||||
this.startWebsocket();
|
this.startWebsocket();
|
||||||
}, 10000);
|
}, 10000);
|
||||||
};
|
};
|
||||||
socket.onerror = (event) => {
|
socket.onerror = (event) => {
|
||||||
console.error(
|
log.error(
|
||||||
`Websocket connection to ${MASTODON_INSTANCE} failed. ${event.type}: ${event.error}, message: '${event.message}'`
|
`Websocket connection to ${MASTODON_INSTANCE} failed. ${event.type}: ${event.error}, message: '${event.message}'`
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export function sleep(timeInMs: number): Promise<undefined> {
|
export function sleep(timeInMs: number): Promise<void> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
setTimeout(resolve, timeInMs);
|
setTimeout(resolve, timeInMs);
|
||||||
});
|
});
|
||||||
|
@ -187,7 +187,7 @@
|
|||||||
}
|
}
|
||||||
.post {
|
.post {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 600px;
|
max-width: min(800px, 80vw);
|
||||||
margin-bottom: 1em;
|
margin-bottom: 1em;
|
||||||
border-bottom: 1px solid var(--color-border);
|
border-bottom: 1px solid var(--color-border);
|
||||||
padding: 1em;
|
padding: 1em;
|
||||||
|
@ -21,6 +21,7 @@ body {
|
|||||||
--color-link: var(--color-mauve);
|
--color-link: var(--color-mauve);
|
||||||
--color-link-visited: var(--color-lavender);
|
--color-link-visited: var(--color-lavender);
|
||||||
--color-bg: var(--color-grey-light);
|
--color-bg: var(--color-grey-light);
|
||||||
|
--color-bg-translucent: hsla(42, 7%, 72%, 0.5);
|
||||||
--color-button: var(--color-red-light);
|
--color-button: var(--color-red-light);
|
||||||
--color-button-shadow: var(--color-red-desat-dark);
|
--color-button-shadow: var(--color-red-desat-dark);
|
||||||
--color-button-hover: var(--color-red);
|
--color-button-hover: var(--color-red);
|
||||||
@ -48,6 +49,7 @@ a:visited {
|
|||||||
--color-link: var(--color-mauve);
|
--color-link: var(--color-mauve);
|
||||||
--color-link-visited: var(--color-lavender);
|
--color-link-visited: var(--color-lavender);
|
||||||
--color-bg: var(--color-blue);
|
--color-bg: var(--color-blue);
|
||||||
|
--color-bg-translucent: hsla(259, 82%, 26%, 0.5);
|
||||||
--color-button: var(--color-red-light);
|
--color-button: var(--color-red-light);
|
||||||
--color-button-shadow: var(--color-red-desat);
|
--color-button-shadow: var(--color-red-desat);
|
||||||
--color-button-hover: var(--color-red);
|
--color-button-hover: var(--color-red);
|
||||||
|
Reference in New Issue
Block a user