summaryrefslogtreecommitdiff
path: root/bin/bookmark
blob: f6d389bfeff83f95992e10b8b3d7b66127077197 (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
44
45
46
47
48
49
50
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'
require 'uri'
require 'nokogiri'
require 'open-uri'
require 'openssl'
require 'yaml'

Bookmark = Struct.new(:url, :title, :date, :host)
bookmark = Bookmark.new(date: Time.now.strftime('%Y-%m-%d'))
OptionParser.new do |opts|
  opts.banner = 'Usage: bookmark [options]'

  opts.on('-uURL', '--url=URL', 'Url of the new bookmark') do |n|
    bookmark[:url] = n
  end
end.parse!

bookmark.host = URI.parse(bookmark[:url]).host.gsub(/^www\./, '')

URI.parse(bookmark[:url]).open({ ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE }) do |page|
  noko = Nokogiri::HTML(page)
  title = noko.xpath('//title').first.content

  # remove known page title craps
  title = case bookmark.host
          when 'brainbaking.com' then title.gsub(/\s\|\sBrain Baking$/, '')
          when 'vermaden.wordpress.com' then title.gsub(/\s\|\s𝚟𝚎𝚛𝚖𝚊𝚍𝚎𝚗$/, '')
          when 'youtube.com' then title.gsub(/\s-|\sYouTube$/, '')
          when 'blog.thechases.com' then title.gsub(/\s\|\sTim's blog$/, '')
          when 'arstechnica.com' then title.gsub(/\s-\sArs Technica$/, '')
          else title
          end

  bookmark.title = title
end

site_dir = File.expand_path('..', __dir__)
data_path = "#{site_dir}/assets/more/bookmarks.yml"

bookmarks = YAML.load_file(
  data_path,
  permitted_classes: [Date]
)
bookmarks['bookmarks'] << bookmark.to_h.transform_keys(&:to_s)
File.open(data_path, 'w') do |f|
  f.write bookmarks.to_yaml
end