65 lines
1.5 KiB
Plaintext
65 lines
1.5 KiB
Plaintext
|
#!/usr/bin/env ruby
|
||
|
# frozen_string_literal: true
|
||
|
|
||
|
# newsboat-yt-feed.rb
|
||
|
# Author: William Woodruff
|
||
|
# ------------------------
|
||
|
# Adds a YouTube channel to newsboat.
|
||
|
# 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 = [
|
||
|
"newsboat",
|
||
|
].freeze
|
||
|
|
||
|
NEWSBOAT_URL_FILES = [
|
||
|
# is this is right precedence?
|
||
|
File.expand_path("~/.config/newsboat/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 = NEWSBOAT_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 newsboat."
|