-
Notifications
You must be signed in to change notification settings - Fork 9
/
newsbeuter-yt-feed
executable file
·65 lines (49 loc) · 1.6 KB
/
newsbeuter-yt-feed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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."