diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/link.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/bin/link.rb b/bin/link.rb new file mode 100755 index 0000000..4f23532 --- /dev/null +++ b/bin/link.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'uri' +require 'nokogiri' +require 'open-uri' +require 'openssl' +require 'yaml' + +require "tty-prompt" + +prompt = TTY::Prompt.new + +Link = Struct.new(:url, :title, :date, :description, :button, :video) +link = Link.new(date: Time.now.strftime('%Y-%m-%d')) + +link.url = prompt.ask("url:") + +title = URI.parse(link[:url]).open({ ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE }) do |page| + noko = Nokogiri::HTML(page) + title = noko.xpath('//title').first.content + title = title.gsub("\n", " ") +end + +link.title = prompt.ask("title:", value: title) +link.description = prompt.ask("description:") +link.button = prompt.ask("button:") +link.video = prompt.ask("video site?:") + +# SAVE + +site_dir = File.expand_path('..', __dir__) +data_path = "#{site_dir}/assets/more/links.yml" + +links = YAML.load_file( + data_path, + permitted_classes: [Date] +) +links['links'] << link.to_h.transform_keys(&:to_s) +File.open(data_path, 'w') do |f| + f.write links.to_yaml +end + |