summaryrefslogtreecommitdiff
path: root/bin/link.rb
blob: 4f23532ff93af9b23fbe9f430bff68f874ee223d (plain)
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
#!/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