Skip to content

Instantly share code, notes, and snippets.

@schappim
Created March 12, 2025 23:11
Show Gist options
  • Save schappim/bac64a811e37d1156c9137ca44098f9c to your computer and use it in GitHub Desktop.
Save schappim/bac64a811e37d1156c9137ca44098f9c to your computer and use it in GitHub Desktop.
ATP.fm on air / off air status...
import https from 'https';
export const handler = async (event) => {
try {
// Get the livestream status
const status = await getLivestreamStatus();
// Check if it contains "Currently off the air"
const isOffAir = status.includes('Currently off the air');
// Construct the HTML response with the appropriate image
const htmlResponse = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ATP Livestream Status</title>
<style>
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
</style>
</head>
<body style="width:800px; height:480px; padding:0!important;">
<img style="width:800px; height:480px; padding:0!important;" src="${isOffAir
? 'https://files.littlebird.com.au/off-air-b2CyZh.png'
: 'https://files.littlebird.com.au/on-air-cRA5hm.png'}"
alt="${isOffAir ? 'Off Air' : 'On Air'}" />
</body>
</html>
`;
// Return the HTML response
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: htmlResponse,
};
} catch (error) {
console.error('Error:', error);
// Return an error response
return {
statusCode: 500,
headers: {
'Content-Type': 'text/html',
},
body: `<html><body><h1>Error</h1><p>${error.message}</p></body></html>`,
};
}
};
// Helper function to get the livestream status
function getLivestreamStatus() {
return new Promise((resolve, reject) => {
https.get('https://atp.fm/livestream_status', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
resolve(data);
console.log('🍓')
console.log(data); // Printing the response body here
} catch (error) {
reject(new Error(`Failed to parse JSON: ${error.message}`));
}
});
}).on('error', (error) => {
reject(new Error(`Request failed: ${error.message}`));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment