dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

commit 0498df6a2983bb831ae19f3af3cdf778dce12e9d
parent 6508ee1042668bff4cfb0beb3ffaf681ef9be620
Author: Alex Balgavy <a.balgavy@gmail.com>
Date:   Wed,  7 Oct 2020 16:13:56 +0200

hypothesis-annotations: extract them to markdown

Former-commit-id: cfd377b6685e38ffda07db905a569102805a8c2b
Diffstat:
Ascripts/hypothesis-annotations | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+), 0 deletions(-)

diff --git a/scripts/hypothesis-annotations b/scripts/hypothesis-annotations @@ -0,0 +1,85 @@ +#!/usr/bin/env ruby +require 'open-uri' +require 'json' +class Hypothesis + class Annotation + attr_reader :title, :uri + def initialize(text,comment,title,uri) + @text = text + @comment = comment + @title = title + @uri = uri + end + + def to_markdown + str = @text.split("\n").reduce("") { |acc, line| acc += "> #{line}\n" } + str += "\n" + str += "#{@comment}\n" if @comment + str + end + + def to_markdown_title_url + "[#{@title}](#{@uri})\n" + end + end + def initialize + ['HYPOTHESIS_API_KEY', 'HYPOTHESIS_USERNAME'].each do |var| + if ENV[var].nil? + warn "Please set the #{var} environment variable." + exit 1 + end + end + + @apikey = ENV['HYPOTHESIS_API_KEY'] + @username = ENV['HYPOTHESIS_USERNAME'] + @headers = {'Host' => 'hypothes.is', 'Accept' => 'application/json', 'Authorization' => "Bearer #{@apikey}"} + @baseurl = "https://hypothes.is/api" + + begin + URI.open("#{@baseurl}/", @headers) + rescue + warn "Could not access the API #{@baseurl}" + exit 1 + end + end + + def request(endpoint) + begin + URI.open(@baseurl+endpoint, @headers) do |response| + JSON.parse(response.read)['rows'] + end + rescue + warn "Error getting data from #{endpoint}" + [] + end + end + + def annotations(uri_search_str) + data = request "/search?user=acct:#{@username}@hypothes.is&#{uri_search_str}&limit=200&order=asc" + data.reduce([]) do |arr, annot| + arr << Annotation.new(annot['target'].first['selector'].select {|f| f['type'] == 'TextQuoteSelector'}.first['exact'], + annot['text'].empty? ? nil : annot['text'], + annot['document']['title'].first, + annot['uri']) + end + + end + + def search_host(host) + annotations "wildcard_uri=http://#{host}/*" + end + + def search_uri(uri) + annotations "uri=#{uri}" + end +end + +if ARGV.empty? + puts "Argument required: URL or host" + exit 1 +end + +h = Hypothesis.new +site = ARGV.first +annotations = (site.start_with? "http") ? h.search_uri(site) : h.search_host(site) +puts annotations.first.to_markdown_title_url+"\n"+annotations.reduce("") { |str, annot| str += annot.to_markdown }