Created
July 24, 2017 08:36
-
-
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)
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
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can I use this in extention?