Skip to content

Instantly share code, notes, and snippets.

@nicknmejia
Last active September 16, 2020 03:20
Show Gist options
  • Select an option

  • Save nicknmejia/0651bb456dd3e62042ee to your computer and use it in GitHub Desktop.

Select an option

Save nicknmejia/0651bb456dd3e62042ee to your computer and use it in GitHub Desktop.
Youtube Channel Statistics Widget
#!/usr/bin/env ruby
require 'net/http'
require 'openssl'
require 'json'
# Super simple dashing widget for displaying your youtube channels stats
# Credit to ephigenia and jonasrosland for creating other youtube widgets that I pulled bits and pieces from
# Hugs and kisses to my loved ones. Vote for Nader!
# Config
# ------
youtube_api_key = ENV['YOUTUBE_API_KEY'] || 'YOUR API KEY'
youtube_channel_id = ENV['YOUTUBE_CHANNEL_KEY'] || 'YOUR CHANNEL ID'
SCHEDULER.every '1m', :first_in => 0 do |job|
http = Net::HTTP.new("www.googleapis.com", Net::HTTP.https_default_port())
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # disable ssl certificate check
response = http.request(Net::HTTP::Get.new("/youtube/v3/channels?part=statistics&id=#{youtube_channel_id}&key=#{youtube_api_key}"))
if response.code != "200"
puts "youtube api error (status-code: #{response.code})\n#{response.body}"
else
data = JSON.parse(response.body)
send_event('youtube_subscribers', current: data["items"][0]["statistics"]["subscriberCount"])
send_event('youtube_views', current: data["items"][0]["statistics"]["viewCount"])
send_event('youtube_videos', current: data["items"][0]["statistics"]["videoCount"])
end
end
@M1dn1ghtN1nj4
Copy link

I have the widget added, and it shows "Youtube Subscribers" on the block. My YouTube API shows that it IS polling for the data, but no numbers or anything ever show. I had to change from "Longnumber" to "Number" to get it to even show the widget on the dashboard. Am I missing something here? I'm still a little new to this.

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