fix: redirects + update root cdn url

This commit is contained in:
Cory Dransfeldt 2024-06-09 11:17:20 -07:00
parent f533a16bdc
commit 693fcbd2b4
No known key found for this signature in database
24 changed files with 0 additions and 1006 deletions

View file

@ -1,8 +0,0 @@
# Scrobble worker
```bash
wrangler deploy --env production
wrangler secret put SUPABASE_URL --env production
wrangler secret put SUPABASE_KEY --env production
wrangler secret put ACCOUNT_ID_PLEX --env production
```

View file

@ -1,129 +0,0 @@
import { createClient } from '@supabase/supabase-js';
import { DateTime } from 'luxon';
import slugify from 'slugify';
const sanitizeMediaString = (str) => {
const sanitizedString = str.normalize('NFD').replace(/[\u0300-\u036f\u2010\-\.\?\(\)\[\]\{\}]/g, '').replace(/\.{3}/g, '');
return slugify(sanitizedString, {
replacement: '-',
remove: /[#,&,+()$~%.'":*?<>{}]/g,
lower: true,
});
};
export default {
async fetch(request, env) {
const SUPABASE_URL = env.SUPABASE_URL;
const SUPABASE_KEY = env.SUPABASE_KEY;
const ACCOUNT_ID_PLEX = env.ACCOUNT_ID_PLEX;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const url = new URL(request.url);
const params = url.searchParams;
const id = params.get('id');
if (!id) {
return new Response(JSON.stringify({ status: 'Bad request' }), { headers: { "Content-Type": "application/json" } });
}
if (id !== ACCOUNT_ID_PLEX) {
return new Response(JSON.stringify({ status: 'Forbidden' }), { headers: { "Content-Type": "application/json" } });
}
const contentType = request.headers.get("Content-Type") || '';
if (!contentType.includes("multipart/form-data")) {
return new Response(JSON.stringify({ status: 'Bad request', message: 'Invalid Content-Type. Expected multipart/form-data.' }), { headers: { "Content-Type": "application/json" } });
}
try {
const data = await request.formData();
const payload = JSON.parse(data.get('payload'));
if (payload?.event === 'media.scrobble') {
const artist = payload['Metadata']['grandparentTitle'];
const album = payload['Metadata']['parentTitle'];
const track = payload['Metadata']['title'];
const listenedAt = Math.floor(DateTime.now().toSeconds());
const artistKey = sanitizeMediaString(artist);
const albumKey = `${artistKey}-${sanitizeMediaString(album)}`;
let { data: artistData, error: artistError } = await supabase
.from('artists')
.select('*')
.ilike('name_string', artist)
.single();
if (artistError && artistError.code === 'PGRST116') {
const { error: insertArtistError } = await supabase.from('artists').insert([
{
mbid: null,
image: `/artists/${artistKey}.jpg`,
key: artistKey,
name: artist,
tentative: true
}
]);
if (insertArtistError) {
return new Response(JSON.stringify({ status: 'error', message: insertArtistError.message }), { headers: { "Content-Type": "application/json" } });
}
({ data: artistData, error: artistError } = await supabase
.from('artists')
.select('*')
.ilike('name_string', artist)
.single());
} else if (artistError) {
return new Response(JSON.stringify({ status: 'error', message: artistError.message }), { headers: { "Content-Type": "application/json" } });
}
let { data: albumData, error: albumError } = await supabase
.from('albums')
.select('*')
.ilike('key', albumKey)
.single();
if (albumError && albumError.code === 'PGRST116') {
const { error: insertAlbumError } = await supabase.from('albums').insert([
{
mbid: null,
image: `/albums/${albumKey}.jpg`,
key: albumKey,
name: album,
tentative: true
}
]);
if (insertAlbumError) {
return new Response(JSON.stringify({ status: 'error', message: insertAlbumError.message }), { headers: { "Content-Type": "application/json" } });
}
({ data: albumData, error: albumError } = await supabase
.from('albums')
.select('*')
.ilike('key', albumKey)
.single());
} else if (albumError) {
return new Response(JSON.stringify({ status: 'error', message: albumError.message }), { headers: { "Content-Type": "application/json" } });
}
const { error: listenError } = await supabase.from('listens').insert([
{
artist_name: artistData.name_string,
album_name: albumData.name,
track_name: track,
listened_at: listenedAt,
album_key: albumKey
}
]);
if (listenError) {
return new Response(JSON.stringify({ status: 'error', message: listenError.message }), { headers: { "Content-Type": "application/json" } });
}
}
return new Response(JSON.stringify({ status: 'success' }), { headers: { "Content-Type": "application/json" } });
} catch (e) {
return new Response(JSON.stringify({ status: 'error', message: e.message }), { headers: { "Content-Type": "application/json" } });
}
}
};

View file

@ -1,179 +0,0 @@
{
"name": "scrobble-worker",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"@supabase/supabase-js": "^2.43.4",
"luxon": "^3.4.4",
"slugify": "^1.6.6"
}
},
"node_modules/@supabase/auth-js": {
"version": "2.64.2",
"resolved": "https://registry.npmjs.org/@supabase/auth-js/-/auth-js-2.64.2.tgz",
"integrity": "sha512-s+lkHEdGiczDrzXJ1YWt2y3bxRi+qIUnXcgkpLSrId7yjBeaXBFygNjTaoZLG02KNcYwbuZ9qkEIqmj2hF7svw==",
"license": "MIT",
"dependencies": {
"@supabase/node-fetch": "^2.6.14"
}
},
"node_modules/@supabase/functions-js": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/@supabase/functions-js/-/functions-js-2.3.1.tgz",
"integrity": "sha512-QyzNle/rVzlOi4BbVqxLSH828VdGY1RElqGFAj+XeVypj6+PVtMlD21G8SDnsPQDtlqqTtoGRgdMlQZih5hTuw==",
"license": "MIT",
"dependencies": {
"@supabase/node-fetch": "^2.6.14"
}
},
"node_modules/@supabase/node-fetch": {
"version": "2.6.15",
"resolved": "https://registry.npmjs.org/@supabase/node-fetch/-/node-fetch-2.6.15.tgz",
"integrity": "sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==",
"license": "MIT",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
}
},
"node_modules/@supabase/postgrest-js": {
"version": "1.15.2",
"resolved": "https://registry.npmjs.org/@supabase/postgrest-js/-/postgrest-js-1.15.2.tgz",
"integrity": "sha512-9/7pUmXExvGuEK1yZhVYXPZnLEkDTwxgMQHXLrN5BwPZZm4iUCL1YEyep/Z2lIZah8d8M433mVAUEGsihUj5KQ==",
"license": "MIT",
"dependencies": {
"@supabase/node-fetch": "^2.6.14"
}
},
"node_modules/@supabase/realtime-js": {
"version": "2.9.5",
"resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-2.9.5.tgz",
"integrity": "sha512-TEHlGwNGGmKPdeMtca1lFTYCedrhTAv3nZVoSjrKQ+wkMmaERuCe57zkC5KSWFzLYkb5FVHW8Hrr+PX1DDwplQ==",
"license": "MIT",
"dependencies": {
"@supabase/node-fetch": "^2.6.14",
"@types/phoenix": "^1.5.4",
"@types/ws": "^8.5.10",
"ws": "^8.14.2"
}
},
"node_modules/@supabase/storage-js": {
"version": "2.5.5",
"resolved": "https://registry.npmjs.org/@supabase/storage-js/-/storage-js-2.5.5.tgz",
"integrity": "sha512-OpLoDRjFwClwc2cjTJZG8XviTiQH4Ik8sCiMK5v7et0MDu2QlXjCAW3ljxJB5+z/KazdMOTnySi+hysxWUPu3w==",
"license": "MIT",
"dependencies": {
"@supabase/node-fetch": "^2.6.14"
}
},
"node_modules/@supabase/supabase-js": {
"version": "2.43.4",
"resolved": "https://registry.npmjs.org/@supabase/supabase-js/-/supabase-js-2.43.4.tgz",
"integrity": "sha512-/pLPaxiIsn5Vaz3s32HC6O/VNwfeddnzS0bZRpOW0AKcPuXroD8pT9G8mpiBlZfpKsMmq6k7tlhW7Sr1PAQ1lw==",
"license": "MIT",
"dependencies": {
"@supabase/auth-js": "2.64.2",
"@supabase/functions-js": "2.3.1",
"@supabase/node-fetch": "2.6.15",
"@supabase/postgrest-js": "1.15.2",
"@supabase/realtime-js": "2.9.5",
"@supabase/storage-js": "2.5.5"
}
},
"node_modules/@types/node": {
"version": "20.14.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.2.tgz",
"integrity": "sha512-xyu6WAMVwv6AKFLB+e/7ySZVr/0zLCzOa7rSpq6jNwpqOrUbcACDWC+53d4n2QHOnDou0fbIsg8wZu/sxrnI4Q==",
"license": "MIT",
"dependencies": {
"undici-types": "~5.26.4"
}
},
"node_modules/@types/phoenix": {
"version": "1.6.4",
"resolved": "https://registry.npmjs.org/@types/phoenix/-/phoenix-1.6.4.tgz",
"integrity": "sha512-B34A7uot1Cv0XtaHRYDATltAdKx0BvVKNgYNqE4WjtPUa4VQJM7kxeXcVKaH+KS+kCmZ+6w+QaUdcljiheiBJA==",
"license": "MIT"
},
"node_modules/@types/ws": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz",
"integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==",
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/luxon": {
"version": "3.4.4",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.4.4.tgz",
"integrity": "sha512-zobTr7akeGHnv7eBOXcRgMeCP6+uyYsczwmeRCauvpvaAltgNyTbLH/+VaEAPUeWBT+1GuNmz4wC/6jtQzbbVA==",
"license": "MIT",
"engines": {
"node": ">=12"
}
},
"node_modules/slugify": {
"version": "1.6.6",
"resolved": "https://registry.npmjs.org/slugify/-/slugify-1.6.6.tgz",
"integrity": "sha512-h+z7HKHYXj6wJU+AnS/+IH8Uh9fdcX1Lrhg1/VMdf9PwoBQXFcXiAdsy2tSK0P6gKwJLXp02r90ahUCqHk9rrw==",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT"
},
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
"license": "MIT"
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause"
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/ws": {
"version": "8.17.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.17.0.tgz",
"integrity": "sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}

View file

@ -1,7 +0,0 @@
{
"dependencies": {
"@supabase/supabase-js": "^2.43.4",
"luxon": "^3.4.4",
"slugify": "^1.6.6"
}
}

View file

@ -1,12 +0,0 @@
name = "scrobble-worker"
main = "./index.js"
compatibility_date = "2023-01-01"
account_id = ""
workers_dev = true
[env.production]
name = "scrobble-worker-production"
routes = [
{ pattern = "coryd.dev/api/scrobble*", zone_id = "" }
]