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