Skip to content

Instantly share code, notes, and snippets.

@M1dn1ghtN1nj4
Forked from nicknmejia/README.md
Last active September 16, 2020 03:22
Show Gist options
  • Select an option

  • Save M1dn1ghtN1nj4/c2dc693c39355e0a5093bf3d5883318e to your computer and use it in GitHub Desktop.

Select an option

Save M1dn1ghtN1nj4/c2dc693c39355e0a5093bf3d5883318e to your computer and use it in GitHub Desktop.
Youtube Channel Statistics Widget

YouTube Channel Statistics Widget

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!

    <li data-row="1" data-col="1" data-sizex="3" data-sizey="1">
      <div data-id="youtube_subscribers" data-view="Number" data-title="Youtube Subscribers" style="background-color:#e52d27;"></div>
    </li>
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment