Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 66 lines (49 sloc) 1.6 KB
#!/usr/bin/env ruby
# frozen_string_literal: true
# newsbeuter-yt-feed.rb
# Author: William Woodruff
# ------------------------
# Adds a YouTube channel to newsbeuter.
# Works with both old (/user/) and new (/channel/)-style YouTube channels.
# ------------------------
# This code is licensed by William Woodruff under the MIT License.
# http://opensource.org/licenses/MIT
require "uri"
DEPS = [
"newsbeuter",
].freeze
NEWSBEUTER_URL_FILES = [
# is this is right precedence?
File.expand_path("~/.config/newsbeuter/urls"),
File.expand_path("~/.newsbeuter/urls"),
].freeze
YOUTUBE_FEED_URLS = {
chan: "https://www.youtube.com/feeds/videos.xml?channel_id=%<id>s",
user: "https://www.youtube.com/feeds/videos.xml?user=%<id>s",
}.freeze
def which?(cmd)
ENV["PATH"].split(File::PATH_SEPARATOR).any? do |path|
File.executable?(File.join(path, cmd))
end
end
def add_feed!(feed_url)
url_file = NEWSBEUTER_URL_FILES.find { |f| File.exist? f }
File.open(url_file, "a") { |io| io.puts "#{feed_url} \"YouTube\"" }
end
abort "Usage: #{$PROGRAM_NAME} <channel url>" if ARGV.empty?
DEPS.each { |d| abort "Fatal: Missing '#{d}'." unless which? d }
chan_url = URI(ARGV.shift)
if chan_url.host.nil? || /youtube/i !~ chan_url.host || chan_url.path.empty?
abort "Fatal: Not a valid channel URL."
end
type, id = chan_url.path.split("/")[1..2]
case type
when "channel"
feed_url = YOUTUBE_FEED_URLS[:chan] % { id: id }
when "user"
feed_url = YOUTUBE_FEED_URLS[:user] % { id: id }
else
abort "Fatal: Ambiguous channel URL (or not a channel URL)."
end
add_feed!(feed_url)
puts "Added #{feed_url} to newsbeuter."