Counting Up to 1 Billion YouTube Views on “Sweet Child O’ Mine” for Guns N’ Roses

Building a Realtime YouTube Views API. Sorta.

Image for post
Image for post
1 BILLION
const axios = require('axios')
const AWS = require('aws-sdk')
const s3 = new AWS.S3()
exports.handler = (event, context, callback) => {
axios.get(`https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${process.env.video}&key=${process.env.key}`)
.then(response => {
let newViews = response.data.items[0].statistics.viewCount

s3.putObject({
Bucket: process.env.bucket,
Key: 'views.json',
Body: JSON.stringify({
views: newViews
}),
ContentType: "application/json"
}, (error, response) => {
if (error) {
callback(null, {
statusCode: 500,
body: JSON.stringify({
status: false
})
})
} else {
callback(null, {
statusCode: 200,
body: JSON.stringify({
status: true
})
})
}
})
})
}
let timeChange = Date.now() - lastUpdate
let viewRate = (newViews - oldViews) / timeChange
setInterval(() => {
views += rate * 1000
}, 1000)
let counter = {
views: oldCount
}
anime({
targets: counter,
views: newCount,
easing: 'linear',
round: 1,
duration: 1000,
delay: 0,
update: () => {
let el = document.getElementsByClassName('views')[0]
el.innerHTML = counter.views
}
}

Written by

I develop websites for rock 'n' roll bands and get paid in sex and drugs. Previously Silva Artist Management, SoundCloud, and Songkick. Currently: Available

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store