Skip to content

Instantly share code, notes, and snippets.

@shrysjain
Created August 18, 2024 14:27
Show Gist options
  • Save shrysjain/0dfc8e009e7dfc2bf5b77b885d444b78 to your computer and use it in GitHub Desktop.
Save shrysjain/0dfc8e009e7dfc2bf5b77b885d444b78 to your computer and use it in GitHub Desktop.
A Node.js script to automatically start Hack Club Arcade sessions when you do not have internet access (such as when you are on a flight)
const fs = require('fs');
const axios = require('axios');
const moment = require('moment-timezone');
// Constants
const DEPARTURE_DATE = '2024-08-16'; // FILL IN
const DEPARTURE_TIME = '1:55 AM'; // FILL IN
const DEPARTURE_TIMEZONE = 'Etc/GMT-7'; // FILL IN
const ARRIVAL_DATE = '2024-08-16'; // FILL IN
const ARRIVAL_TIME = '7:15 AM'; // FILL IN
const ARRIVAL_TIMEZONE = 'America/New_York'; // FILL IN
const ARCADE_API_KEY = 'Your API key here'; // FILL IN
const WORK_MESSAGE = 'Working on project XYZ'; // FILL IN
const API_ENDPOINT = 'https://hackhour.hackclub.com/api/start/xyz';
const LOG_FILE = 'logs.txt';
// Log messages to a file
const log = (message) => {
const timestamp = moment().format('YYYY-MM-DD HH:mm:ss');
const logMessage = `${timestamp} - ${message}\n`;
fs.appendFileSync(LOG_FILE, logMessage, (err) => {
if (err) throw err;
});
console.log(logMessage);
};
// Send the POST request
const sendPostRequest = async () => {
log(`Sending POST request to ${API_ENDPOINT}`);
try {
const response = await axios.post(API_ENDPOINT, {
work: WORK_MESSAGE
}, {
headers: {
'Authorization': `Bearer ${ARCADE_API_KEY}`,
'Content-Type': 'application/json'
}
});
if (response.data.ok) {
log(`POST request successful. Session ID: ${response.data.data.id}`);
return true;
} else {
log(`POST request failed. Retrying in 5 minutes...`);
return false;
}
} catch (error) {
log(`Error sending POST request: ${error.message}. Retrying in 5 minutes...`);
return false;
}
};
// Main function
const startProcess = async () => {
const departureTime = moment.tz(DEPARTURE_TIME, 'hh:mm A', DEPARTURE_TIMEZONE);
const arrivalTime = moment.tz(ARRIVAL_TIME, 'hh:mm A', ARRIVAL_TIMEZONE);
log(`Script started. Departure time: ${departureTime.format()}, Arrival time: ${arrivalTime.format()}`);
// Wait until 30 minutes after the departure time
const timeUntilDeparture = moment.duration(departureTime.add(30, 'minutes').diff(moment()));
if (timeUntilDeparture > 0) {
log(`Waiting ${timeUntilDeparture.humanize()} until departure...`);
await new Promise(resolve => setTimeout(resolve, timeUntilDeparture.asMilliseconds()));
}
while (moment().isBefore(arrivalTime)) {
let success = false;
while (!success) {
success = await sendPostRequest();
if (!success) {
await new Promise(resolve => setTimeout(resolve, 5 * 60 * 1000)); // Wait 5 minutes before retrying
}
}
log(`Waiting 75 minutes before sending the next POST request...`);
await new Promise(resolve => setTimeout(resolve, 75 * 60 * 1000)); // Wait 75 minutes
}
log('Script ended as arrival time has been reached.');
};
// Run process
startProcess();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment