Skip to content

Instantly share code, notes, and snippets.

@rmdes
Created January 26, 2025 15:14
Show Gist options
  • Save rmdes/2724abe42d04c65422c70156517041cb to your computer and use it in GitHub Desktop.
Save rmdes/2724abe42d04c65422c70156517041cb to your computer and use it in GitHub Desktop.
N8N Function countdown Trump
// JavaScript code for N8N to calculate and post daily countdowns with a graphical loading bar
// add this code to the Code aka function node of N8N
// Define the target dates
const midtermsDate = new Date('2026-11-03T00:00:00Z'); // Next USA Midterms
const presidentialElectionDate = new Date('2028-11-07T00:00:00Z'); // Next Presidential Election
const inaugurationDate = new Date('2029-01-20T00:00:00Z'); // Next Inauguration Day
// Get the current date
const currentDate = new Date();
// Calculate the remaining days and total days for the countdowns
const totalDaysToMidterms = Math.ceil((midtermsDate - new Date('2024-11-05T00:00:00Z')) / (1000 * 60 * 60 * 24));
const daysUntilMidterms = Math.ceil((midtermsDate - currentDate) / (1000 * 60 * 60 * 24));
const midtermsProgress = Math.min(100, Math.floor(((totalDaysToMidterms - daysUntilMidterms) / totalDaysToMidterms) * 100));
const totalDaysToPresidentialElection = Math.ceil((presidentialElectionDate - new Date('2024-11-05T00:00:00Z')) / (1000 * 60 * 60 * 24));
const daysUntilPresidentialElection = Math.ceil((presidentialElectionDate - currentDate) / (1000 * 60 * 60 * 24));
const presidentialProgress = Math.min(100, Math.floor(((totalDaysToPresidentialElection - daysUntilPresidentialElection) / totalDaysToPresidentialElection) * 100));
const totalDaysToInauguration = Math.ceil((inaugurationDate - new Date('2025-01-20T00:00:00Z')) / (1000 * 60 * 60 * 24));
const daysUntilInauguration = Math.ceil((inaugurationDate - currentDate) / (1000 * 60 * 60 * 24));
const inaugurationProgress = Math.min(100, Math.floor(((totalDaysToInauguration - daysUntilInauguration) / totalDaysToInauguration) * 100));
// Create a graphical loading bar function
function createLoadingBar(percentage) {
const totalBars = 10; // Length of the loading bar
const filledBars = Math.floor((percentage / 100) * totalBars);
const emptyBars = totalBars - filledBars;
return `${'█'.repeat(filledBars)}${'▒'.repeat(emptyBars)} ${percentage}%`;
}
// Generate the loading bars
const midtermsLoadingBar = createLoadingBar(midtermsProgress);
const presidentialLoadingBar = createLoadingBar(presidentialProgress);
const inaugurationLoadingBar = createLoadingBar(inaugurationProgress);
// Generate the message
const message = `There are ${daysUntilMidterms} days until the Midterms, ${daysUntilPresidentialElection} days until the next Presidential Election, and ${daysUntilInauguration} days until the next Inauguration Day.\n\n` +
`Midterms Progress: \n${midtermsLoadingBar}\n\n` +
`Presidential Election Progress: \n${presidentialLoadingBar}\n\n` +
`Until Next Inauguration: \n${inaugurationLoadingBar}`;
// Output the message
return [{
json: {
message,
},
}];
{
"nodes": [
{
"parameters": {
"postText": "={{ $json.message }} #Trump"
},
"type": "@muench-dev/n8n-nodes-bluesky.bluesky",
"typeVersion": 2,
"position": [
360,
-20
],
"id": "eec781ef-614e-466f-9023-c00d537330fb",
"name": "Bluesky",
"credentials": {
"blueskyApi": {
"id": "toVly1K3j1l8q2SC",
"name": "Bluesky account"
}
}
},
{
"parameters": {
"rule": {
"interval": [
{
"triggerAtHour": 14,
"triggerAtMinute": 1
}
]
}
},
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1.2,
"position": [
-300,
-20
],
"id": "52093e37-8e35-4e4d-bf9c-62b70b9e3eef",
"name": "Schedule Trigger"
},
{
"parameters": {
"jsCode": "// JavaScript code for N8N to calculate and post daily countdowns with a graphical loading bar\n\n// Define the target dates\nconst midtermsDate = new Date('2026-11-03T00:00:00Z'); // Next USA Midterms\nconst presidentialElectionDate = new Date('2028-11-07T00:00:00Z'); // Next Presidential Election\nconst inaugurationDate = new Date('2029-01-20T00:00:00Z'); // Next Inauguration Day\n\n// Get the current date\nconst currentDate = new Date();\n\n// Calculate the remaining days and total days for the countdowns\nconst totalDaysToMidterms = Math.ceil((midtermsDate - new Date('2024-11-05T00:00:00Z')) / (1000 * 60 * 60 * 24));\nconst daysUntilMidterms = Math.ceil((midtermsDate - currentDate) / (1000 * 60 * 60 * 24));\nconst midtermsProgress = Math.min(100, Math.floor(((totalDaysToMidterms - daysUntilMidterms) / totalDaysToMidterms) * 100));\n\nconst totalDaysToPresidentialElection = Math.ceil((presidentialElectionDate - new Date('2024-11-05T00:00:00Z')) / (1000 * 60 * 60 * 24));\nconst daysUntilPresidentialElection = Math.ceil((presidentialElectionDate - currentDate) / (1000 * 60 * 60 * 24));\nconst presidentialProgress = Math.min(100, Math.floor(((totalDaysToPresidentialElection - daysUntilPresidentialElection) / totalDaysToPresidentialElection) * 100));\n\nconst totalDaysToInauguration = Math.ceil((inaugurationDate - new Date('2025-01-20T00:00:00Z')) / (1000 * 60 * 60 * 24));\nconst daysUntilInauguration = Math.ceil((inaugurationDate - currentDate) / (1000 * 60 * 60 * 24));\nconst inaugurationProgress = Math.min(100, Math.floor(((totalDaysToInauguration - daysUntilInauguration) / totalDaysToInauguration) * 100));\n\n// Create a graphical loading bar function\nfunction createLoadingBar(percentage) {\n const totalBars = 10; // Length of the loading bar\n const filledBars = Math.floor((percentage / 100) * totalBars);\n const emptyBars = totalBars - filledBars;\n return `${'█'.repeat(filledBars)}${'▒'.repeat(emptyBars)} ${percentage}%`;\n}\n\n// Generate the loading bars\nconst midtermsLoadingBar = createLoadingBar(midtermsProgress);\nconst presidentialLoadingBar = createLoadingBar(presidentialProgress);\nconst inaugurationLoadingBar = createLoadingBar(inaugurationProgress);\n\n// Generate the message\nconst message = `There are ${daysUntilMidterms} days until the Midterms, ${daysUntilPresidentialElection} days until the next Presidential Election, and ${daysUntilInauguration} days until the next Inauguration Day.\\n\\n` +\n `Midterms Progress: \\n${midtermsLoadingBar}\\n\\n` +\n `Presidential Election Progress: \\n${presidentialLoadingBar}\\n\\n` +\n `Until Next Inauguration: \\n${inaugurationLoadingBar}`;\n\n// Output the message\nreturn [{\n json: {\n message,\n },\n}];\n"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
20,
-20
],
"id": "2b6d90b4-9c89-4492-9f0d-c9e2c1f50dd7",
"name": "Code"
}
],
"connections": {
"Schedule Trigger": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
}
]
]
},
"Code": {
"main": [
[
{
"node": "Bluesky",
"type": "main",
"index": 0
}
]
]
}
},
"pinData": {},
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "58fd5c3ff393ef21f618201de491e9a03a72661d4848a2ad337fc80dc260a4d9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment