Skip to content

Instantly share code, notes, and snippets.

@jah2488
Created April 16, 2018 17:06
Show Gist options
  • Save jah2488/504db7491cbf5e3a07a85c4bbe26d029 to your computer and use it in GitHub Desktop.
Save jah2488/504db7491cbf5e3a07a85c4bbe26d029 to your computer and use it in GitHub Desktop.
# Sample usage
# cd to book folder
# ruby feed_seeder.rb && dropcaster > index.rss && aws s3 sync . "s3://audiobooks-rss/My Book Name" --acl public-read
require 'colorize'
require 'optparse'
DEFAULT_DESCRIPTION = "Books are cool"
DEFAULT_IMAGE = "https://pbs.twimg.com/profile_images/378800000448533787/c32fb13e160ee7cd17848e8cacbbcfc5_400x400.jpeg"
DEFAULT_PATH = "."
DOMAIN = "https://s3.amazonaws.com"
BUCKET = "audiobooks-rss"
def write_to_file(path, string)
File.write(path, string)
end
def read_arguments
args = {
description: DEFAULT_DESCRIPTION,
image_url: DEFAULT_IMAGE,
path: DEFAULT_PATH,
}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-d", "--description DESCRIPTION", "An optional description for the book") { |n| args[:description] = n }
opts.on("-i", "--image IMAGE", "An optional image url for the book") { |n| args[:image_url] = n }
opts.on("-p", "--path PATH", "The path of the folder to create a feed of") { |n| args[:path] = n }
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
args
end
def format_title(title, separator)
title.gsub(/\b('?[a-z])/) { $1.capitalize }
.split(" ")
.join(separator)
end
def generate_url(title, file_format, suffix)
if file_format.to_s.empty?
file_format = format_title(title, " ")
end
formatted_title = format_title(title, "+")
folder = formatted_title
path = "#{file_format}-#{suffix}.mp3"
[ DOMAIN, BUCKET, folder, path ].join("/")
end
def generate_template(title, file_url, image_url, description)
return %Q(
:title: '#{title}'
:subtitle: 'Subtitles are useful'
:url: '#{file_url}'
:language: 'en-us'
:copyright: '© mergesort'
:author: 'NYPL Books'
# iTunes prefers square .jpg images that are at least 600 x 600 pixels
#
# If the URL does not start with http: or https:, it will be prefixed with the channel url.
#
:image_url: '#{image_url}'
:description: '#{description}'
:owner:
:name: 'Joe Fabisevich'
:email: '[email protected]'
:categories: ['Education']
:explicit: No
# Keywords
#
# Apple recommends to use not more than 12 keywords
#
:keywords: []
)
end
def get(prompt, optional = false)
begin
puts prompt
msg = gets.chomp
end while msg.to_s.empty? || optional
msg
end
def start
title = get("Enter a title:".green)
file_format = get("Enter file format (optional):".blue, :optional)
suffix = get("Enter a suffix:".green)
arguments = read_arguments
formatted_title = format_title(title, " ")
url = generate_url(title, file_format, suffix)
channel_yml = generate_template(formatted_title, url, arguments[:image_url], arguments[:description])
write_to_file("#{arguments[:path]}/channel.yml", channel_yml)
end
start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment