Created
July 19, 2021 18:16
-
-
Save danilopolani/70eb92c6d3664fac85590d44d6768494 to your computer and use it in GitHub Desktop.
ZX YouTube Video Stats Tracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const res = await fetch('https://www.googleapis.com/youtube/v3/videos?part=statistics&id=VIDEO_ID&key=KEY') | |
const data = await res.json() | |
// Retrieve new data | |
const count = Number(data.items[0].statistics.viewCount) | |
// Format up to milions | |
const countFormatted = (count / 1000 / 1000).toFixed(2) | |
// Calculate stats | |
const oldCount = await $`cat .video_stats` | |
const diffCount = count - oldCount | |
const diffPerc = (diffCount / oldCount * 100).toFixed(2) | |
const diffCountFormatted = new Intl.NumberFormat('it-IT').format(diffCount) | |
// Update with new count | |
await $`echo ${count} > .video_stats` | |
// Notify discord | |
await fetch('https://discord.com/api/webhooks/WEBHOOK_TOKEN', { | |
method: 'POST', | |
body: JSON.stringify({ | |
content: 'The video reached **${countFormatted} M** views (**+${diffCountFormatted}**, **+${diffPerc}%** :chart_with_upwards_trend:)`, | |
}), | |
headers: {'Content-Type': 'application/json'} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment