Skip to content

Instantly share code, notes, and snippets.

@danilopolani
Created July 19, 2021 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilopolani/70eb92c6d3664fac85590d44d6768494 to your computer and use it in GitHub Desktop.
Save danilopolani/70eb92c6d3664fac85590d44d6768494 to your computer and use it in GitHub Desktop.
ZX YouTube Video Stats Tracker
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