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
51
52
53
54
55
56
57
58
59
|
#!/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, :source_url, :source_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
opts.on('-sURL', '--source=URL', 'Url of the source') do |n|
bookmark[:source_url] = n
end
end.parse!
bookmark.host = URI.parse(bookmark[:url]).host.gsub(/^www\./, '')
bookmark.source_host = URI.parse(bookmark[:source_url]).host.gsub(/^www\./, '') if bookmark[:source_url]
URI.parse(bookmark[: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", " ")
# remove known page title craps
title = case bookmark.host
when 'arstechnica.com' then title.gsub(/\s-\sArs Technica$/, '')
when 'blog.thechases.com' then title.gsub(/\s\|\sTim's blog$/, '')
when 'brainbaking.com' then title.gsub(/\s\|\sBrain Baking$/, '')
when 'freebsd.org' then title.gsub(/\s\|\sThe FreeBSD Project$/, '')
when 'servethehome.com' then title.gsub(/\s-\sServeTheHome$/, '')
when 'theverge.com' then title.gsub(/\s-\sThe Verge$/, '')
when 'vermaden.wordpress.com' then title.gsub(/\s\|\s𝚟𝚎𝚛𝚖𝚊𝚍𝚎𝚗$/, '')
when 'youtube.com' then title.gsub(/\s-|\sYouTube$/, '')
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
|