#!/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