diff options
author | mms <git@sapka.me> | 2024-11-24 22:30:39 +0100 |
---|---|---|
committer | mms <git@sapka.me> | 2024-11-24 22:31:00 +0100 |
commit | c7f5572beb42c073ee6550816cd9e1f31cfa7092 (patch) | |
tree | c3aa312d0c65713d49da4436e22be071612a89ec /bin | |
parent | 95e0330c60e67128950f0b8e135488217a54923b (diff) |
feat: bookmarks
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/bookmark | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/bin/bookmark b/bin/bookmark new file mode 100755 index 0000000..f6d389b --- /dev/null +++ b/bin/bookmark @@ -0,0 +1,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 |