First commit

This commit is contained in:
2024-02-21 18:47:21 +01:00
commit 0b025587bd
13 changed files with 2291 additions and 0 deletions

48
Modules/cronManager.js Normal file
View File

@@ -0,0 +1,48 @@
import cron from 'node-cron';
const daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
const hoursOfDay = [...Array(24).keys()].map(hour => hour.toString());
const minutesOfHour = [...Array(60).keys()].map(minute => minute.toString());
function toCronExpression(str) {
switch (str.toLowerCase()) {
case 'everyfiveseconds': {
return '*/5 * * * * *';
}
case 'everythirtyseconds': {
return '*/30 * * * * *';
}
case 'everyminute': {
return '* * * * *';
}
case 'everyfiveminutes': {
return '*/5 * * * *';
}
case 'everyfifteenminutes': {
return '*/15 * * * *';
}
case 'everythirtyminutes': {
return '*/30 * * * *';
}
case 'everyday': {
return '0 0 * * *';
}
case 'weekdays': {
return '0 0 * * 1-5';
}
case 'weekends': {
return '0 0 * * 6,7';
}
default: {
const [day, hour, minute] = str.toLowerCase().split(' ');
const dayIndex = daysOfWeek.indexOf(day);
const hourIndex = hoursOfDay.indexOf(hour);
const minuteIndex = minutesOfHour.indexOf(minute);
return `${minuteIndex} ${hourIndex} * * ${dayIndex}`;
}
}
}
export function createCron(rate, exec) {
cron.schedule(toCronExpression(rate), exec);
}

71
Modules/fetchManager.js Normal file
View File

@@ -0,0 +1,71 @@
import fetch from 'node-fetch';
import { error } from './logManager';
async function get(url, token) {
const options = {
method: 'GET',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
async function post(url, body, token) {
const options = {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
body: JSON.stringify(body),
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
async function patch(url, body, token) {
const options = {
method: 'PATCH',
mode: 'cors',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
body: JSON.stringify(body),
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
async function put(url, body, token) {
const options = {
method: 'PUT',
mode: 'cors',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
body: JSON.stringify(body),
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
export {
get,
post,
patch,
put,
};

86
Modules/htmlManager.js Normal file
View File

@@ -0,0 +1,86 @@
import fs from 'fs';
import { log } from './logManager';
function stylizeHTML(html) {
return `<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
div {
background-color: #fff;
padding: 20px;
border: 1px solid #000;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
h2 {
color: #333;
font-size: 20px;
}
p {
color: #666;
font-size: 14px;
}
img {
max-width: 100%;
height: auto;
}
a {
color: #007bff;
text-decoration: none;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #333;
color: #fff;
}
div {
background-color: #444;
border-color: #000;
}
h2 {
color: #fff;
}
p {
color: #ccc;
}
a {
color: #007bff;
}
}
</style>
</head>
<body>
${html}
</body>
</html>`;
}
function generatePostHtml(posts) {
return posts.map(post => `
<div>
<h2>${post.title}</h2>
<p>Date: ${post.date}</p>
<p>Flair: ${post.flair}</p>
<p>Linked: <a href="${post.linked}">${post.linked}</a></p>
<p>${post.description}</p>
${post.tldr ? `<p>TLDR: ${post.tldr}</p>` : ''}
${post.image ? `<img src="${post.image}" /><br><br>` : ''}
<a href="${post.url}">Post Link</a>
</div>
`).join('');
}
function saveHtmlToFile(html) {
fs.writeFileSync('index.html', html);
log('HTML file saved.');
}
export function generateHtml(posts) {
const html = stylizeHTML(generatePostHtml(posts));
saveHtmlToFile(html);
}

19
Modules/logManager.js Normal file
View File

@@ -0,0 +1,19 @@
import pino from 'pino';
const logger = pino();
export function log(x) {
logger.info(x);
}
export function debug(x) {
logger.debug(x);
}
export function warn(x) {
logger.warn(x);
}
export function error(x) {
logger.error(x);
}