|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Required parameters: |
| 4 | +# @raycast.schemaVersion 1 |
| 5 | +# @raycast.title YouTube Statistics |
| 6 | +# @raycast.mode inline |
| 7 | +# @raycast.refreshTime 1h |
| 8 | + |
| 9 | +# Optional parameters: |
| 10 | +# @raycast.packageName YouTube Statistics |
| 11 | +# @raycast.icon images/youtube.png |
| 12 | + |
| 13 | +# Documentation: |
| 14 | +# @raycast.description Shows YouTube Subscribers, Views and Videos |
| 15 | +# @raycast.author Astrit |
| 16 | +# @raycast.authorURL https://github.com/astrit |
| 17 | + |
| 18 | +require "json" |
| 19 | +require "net/http" |
| 20 | +require "uri" |
| 21 | + |
| 22 | +# Channel ID is required |
| 23 | +# To find your ID please follow instructions here: https://support.google.com/youtube/answer/3250431?hl=en |
| 24 | +CHANNEL_ID = "" |
| 25 | + |
| 26 | +# Channel KEY is required |
| 27 | +# To create your credentials please follow instructions here: https://developers.google.com/youtube/registering_an_application |
| 28 | +CHANNEL_KEY = "" |
| 29 | + |
| 30 | +uri = URI("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=#{CHANNEL_ID}&key=#{CHANNEL_KEY}&part=snippet") |
| 31 | + |
| 32 | +req = Net::HTTP::Get.new(uri) |
| 33 | + |
| 34 | +req_options = { |
| 35 | + use_ssl: uri.scheme == "https", |
| 36 | +} |
| 37 | + |
| 38 | +res = Net::HTTP.start(uri.hostname, uri.port, req_options) { |http| |
| 39 | + http.request(req) |
| 40 | +} |
| 41 | + |
| 42 | +if res.code == "200" |
| 43 | + result = JSON.parse(res.body) |
| 44 | + item = result["items"][0] |
| 45 | + statistics = item["statistics"] |
| 46 | + |
| 47 | + title = item["snippet"]["title"] |
| 48 | + subs = statistics["subscriberCount"] |
| 49 | + views = statistics["viewCount"] |
| 50 | + videos = statistics["videoCount"] |
| 51 | + |
| 52 | + puts "Channel: #{title}, Subs: #{subs}, Views: #{views}, Videos: #{videos}" |
| 53 | +else |
| 54 | + puts "Failed loading statistics" |
| 55 | + exit(1) |
| 56 | +end |
0 commit comments