Skip to content

Instantly share code, notes, and snippets.

@GavinJoyce
Created July 24, 2017 08:36
Show Gist options
  • Save GavinJoyce/5bf4dae0baf72433078fb43a9dad3160 to your computer and use it in GitHub Desktop.
Save GavinJoyce/5bf4dae0baf72433078fb43a9dad3160 to your computer and use it in GitHub Desktop.
Simple YouTube subscriber and view count script for MacBook Pro Touch Bar (see https://www.youtube.com/watch?v=gsRTtO-8ofM)
#!/usr/bin/env ruby
# See https://www.youtube.com/watch?v=gsRTtO-8ofM for details on how to use this
require 'net/http'
require 'json'
CHANNEL_ID = '<CHANNEL_ID_HERE>'
API_KEY = '<YOUTUBE_API_KEY_HERE>'
YOUTUBE_URL = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=#{CHANNEL_ID}&key=#{API_KEY}"
def add_commas(number)
number.to_s.reverse.scan(/\d{3}|.+/).join(',').reverse
end
def get_stats
response = Net::HTTP.get(URI(YOUTUBE_URL))
data = JSON.parse(response)
{
subscriber_count: add_commas(data['items'][0]['statistics']['subscriberCount']),
view_count: add_commas(data['items'][0]['statistics']['viewCount'])
}
end
stats = get_stats
puts "#{stats[:subscriber_count]} subs"
puts "#{stats[:view_count]} views"
@kawenke
Copy link

kawenke commented Apr 24, 2018

can I use this in extention?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment