Prettier and a new readme

This commit is contained in:
skidoodle 2022-08-26 21:27:32 +02:00
parent 146b35d1d3
commit f21d8f8086
16 changed files with 352 additions and 2594 deletions

View file

@ -1,44 +1,47 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest, NextApiResponse } from "next";
import aws from 'aws-sdk'
import aws from "aws-sdk";
const { BUCKET, ACCESS_KEY, SECRET_KEY, ENDPOINT, REGION } = process.env
const { BUCKET, ACCESS_KEY, SECRET_KEY, ENDPOINT, REGION } = process.env;
export default async function(req: NextApiRequest, res: NextApiResponse) {
aws.config.s3 = ({
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
region: REGION,
endpoint: ENDPOINT,
signatureVersion: 'v4'
})
export default async function (req: NextApiRequest, res: NextApiResponse) {
aws.config.s3 = {
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
region: REGION,
endpoint: ENDPOINT,
signatureVersion: "v4",
};
let isTruncated: boolean | undefined = true
let startAfter
let isTruncated: boolean | undefined = true;
let startAfter;
let objects = 0
let size = 0
let objects = 0;
let size = 0;
const s3 = new aws.S3()
const s3 = new aws.S3();
while(isTruncated) {
let params: any = { Bucket: BUCKET }
if(startAfter) {
params.StartAfter = startAfter
}
const data = await s3.listObjectsV2(params).promise()
while (isTruncated) {
let params: any = { Bucket: BUCKET };
data.Contents?.forEach((object: any) => {
objects++
size += object.Size! / 1024 / 1024 / 1024
})
isTruncated = data.IsTruncated
if (isTruncated) {
startAfter = data.Contents!.slice(-1)[0].Key;
}
if (startAfter) {
params.StartAfter = startAfter;
}
res.setHeader('Cache-Control', 'public, s-maxage=10, stale-while-revalidate=59');
res.json({ object: objects, size: Number(size.toFixed(2)) })
}
const data = await s3.listObjectsV2(params).promise();
data.Contents?.forEach((object: any) => {
objects++;
size += object.Size! / 1024 / 1024 / 1024;
});
isTruncated = data.IsTruncated;
if (isTruncated) {
startAfter = data.Contents!.slice(-1)[0].Key;
}
}
res.setHeader(
"Cache-Control",
"public, s-maxage=10, stale-while-revalidate=59"
);
res.json({ object: objects, size: Number(size.toFixed(2)) });
}

View file

@ -1,26 +1,28 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest, NextApiResponse } from "next";
export default async function(req: NextApiRequest, res: NextApiResponse) {
const { LASTFM_USERNAME, LASTFM_API } = process.env
export default async function (req: NextApiRequest, res: NextApiResponse) {
const { LASTFM_USERNAME, LASTFM_API } = process.env;
const { recenttracks: response } = await fetch(`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${LASTFM_USERNAME}&api_key=${LASTFM_API}&format=json&limit=1`).then((res) => res.json())
const { recenttracks: response } = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${LASTFM_USERNAME}&api_key=${LASTFM_API}&format=json&limit=1`
).then((res) => res.json());
const { track } = response
const { artist, name, url, image } = track[0]
const { track } = response;
const { artist, name, url, image } = track[0];
let nowplaying = Boolean(track[0]['@attr']?.nowplaying)
let nowplaying = Boolean(track[0]["@attr"]?.nowplaying);
if(nowplaying) {
return res.status(200).json({
nowplaying,
song: {
artist: artist['#text'],
title: name,
url: url,
image: image[2]['#text'],
}
})
}
if (nowplaying) {
return res.status(200).json({
nowplaying,
song: {
artist: artist["#text"],
title: name,
url: url,
image: image[2]["#text"],
},
});
}
return res.status(200).json({ nowplaying })
}
return res.status(200).json({ nowplaying });
}