Module: SL

Includes:
URL
Defined in:
lib/searchlink/url.rb,
lib/searchlink/help.rb,
lib/searchlink/util.rb,
lib/searchlink/parse.rb,
lib/searchlink/config.rb,
lib/searchlink/config.rb,
lib/searchlink/output.rb,
lib/searchlink/output.rb,
lib/searchlink/search.rb,
lib/searchlink/semver.rb,
lib/searchlink/string.rb,
lib/searchlink/version.rb,
lib/searchlink/version.rb,
lib/searchlink/searches.rb,
lib/searchlink/exceptions.rb,
lib/searchlink/script_plugin.rb,
lib/searchlink/searches/hook.rb,
lib/searchlink/searches/tmdb.rb,
lib/searchlink/searches/popup.rb,
lib/searchlink/searches/amazon.rb,
lib/searchlink/searches/github.rb,
lib/searchlink/searches/google.rb,
lib/searchlink/searches/itunes.rb,
lib/searchlink/searches/lastfm.rb,
lib/searchlink/searches/lyrics.rb,
lib/searchlink/searches/setapp.rb,
lib/searchlink/searches/social.rb,
lib/searchlink/searches/history.rb,
lib/searchlink/searches/twitter.rb,
lib/searchlink/searches/youtube.rb,
lib/searchlink/searches/linkding.rb,
lib/searchlink/searches/pinboard.rb,
lib/searchlink/searches/software.rb,
lib/searchlink/searches/spelling.rb,
lib/searchlink/searches/shortener.rb,
lib/searchlink/searches/spotlight.rb,
lib/searchlink/searches/wikipedia.rb,
lib/searchlink/searches/applemusic.rb,
lib/searchlink/searches/definition.rb,
lib/searchlink/searches/duckduckgo.rb,
lib/searchlink/searches/duckduckgo.rb,
lib/searchlink/searches/stackoverflow.rb,
lib/searchlink/searches/helpers/safari.rb,
lib/searchlink/searches/helpers/firefox.rb,
lib/searchlink/searches/shorteners/isgd.rb,
lib/searchlink/searches/helpers/chromium.rb,
lib/searchlink/searches/shorteners/bitly.rb,
lib/searchlink/searches/shorteners/tinyurl.rb

Overview

Chromium (Chrome, Arc, Brave, Edge) search methods

Defined Under Namespace

Modules: Searches, URL, Util Classes: AmazonSearch, AppleMusicSearch, BitlySearch, DefinitionSearch, DuckDuckGoSearch, GitHubSearch, GoogleSearch, HistorySearch, HookSearch, ITunesSearch, IsgdSearch, LastFMSearch, LinkdingSearch, LyricsSearch, PinboardSearch, PluginError, PopupSearch, ScriptSearch, SearchLink, SemVer, SetappSearch, Shortener, SocialSearch, SoftwareSearch, SpellSearch, SpotlightSearch, StackOverflowSearch, TMDBSearch, TinyurlSearch, TwitterSearch, VersionError, WikipediaSearch, YouTubeSearch

Constant Summary collapse

VERSION =
'2.3.92'

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from URL

amazon_affiliatize, follow_redirects, only_url?, ref_title_for_url, title, url?, url_to_link, valid_link?

Class Attribute Details

.clipboardObject

Whether or not to copy results to clipbpard



20
21
22
# File 'lib/searchlink/output.rb', line 20

def clipboard
  @clipboard ||= false
end

.configObject



7
8
9
# File 'lib/searchlink/config.rb', line 7

def config
  @config ||= SL::SearchLink.new({ echo: true })
end

.error_countObject

Count of errors



15
16
17
# File 'lib/searchlink/output.rb', line 15

def error_count
  @error_count ||= 0
end

.errorsObject

Stores generated errors



65
66
67
# File 'lib/searchlink/output.rb', line 65

def errors
  @errors ||= {}
end

Stores the footer with reference links and footnotes



40
41
42
# File 'lib/searchlink/output.rb', line 40

def footer
  @footer ||= []
end

.line_numObject

Tracks the line number of each link match for debug output



45
46
47
# File 'lib/searchlink/output.rb', line 45

def line_num
  @line_num ||= 0
end

.match_columnObject

Tracks the column of each link match for debug output



50
51
52
# File 'lib/searchlink/output.rb', line 50

def match_column
  @match_column ||= 0
end

.match_lengthObject

Tracks the length of each link match for debug output



55
56
57
# File 'lib/searchlink/output.rb', line 55

def match_length
  @match_length ||= 0
end

.originputObject

Stores the original input



60
61
62
# File 'lib/searchlink/output.rb', line 60

def originput
  @originput ||= ""
end

.outputObject

Stores the generated output



30
31
32
# File 'lib/searchlink/output.rb', line 30

def output
  @output ||= []
end

.prev_configObject



11
12
13
# File 'lib/searchlink/config.rb', line 11

def prev_config
  @prev_config ||= {}
end

.printoutObject

Whether or not to echo results to STDOUT as they’re created



25
26
27
# File 'lib/searchlink/output.rb', line 25

def printout
  @printout ||= false
end

.reportObject

Stores the generated debug report



35
36
37
# File 'lib/searchlink/output.rb', line 35

def report
  @report ||= []
end

.shortenerObject

The shortener to use



75
76
77
# File 'lib/searchlink/output.rb', line 75

def shortener
  @shortener ||= :none
end

.titleizeObject

Whether or not to add a title to the output



10
11
12
# File 'lib/searchlink/output.rb', line 10

def titleize
  @titleize ||= false
end

Class Method Details

.add_error(type, str) ⇒ nil

Adds the given string to the errors.

Parameters:

  • type (Symbol)

    The type of error.

  • str (String)

    The string to add.

Returns:

  • (nil)


217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/searchlink/output.rb', line 217

def add_error(type, str)
  return unless SL.config["debug"]

  unless SL.line_num.nil?
    position = "#{SL.line_num}:"
    position += SL.match_column.nil? ? "0:" : "#{SL.match_column}:"
    position += SL.match_length.nil? ? "0" : SL.match_length.to_s
  end
  SL.errors[type] ||= []
  SL.errors[type].push("(#{position}): #{str}")
  SL.error_count ||= 0
  SL.error_count += 1
end

Adds the given string to the footer.

Parameters:

  • str (String)

    The string to add.

Returns:

  • (nil)


158
159
160
161
# File 'lib/searchlink/output.rb', line 158

def add_footer(str)
  SL.footer ||= []
  SL.footer.push(str.strip)
end

.add_output(str) ⇒ nil

Adds the given string to the output.

Parameters:

  • str (String)

    The string to add.

Returns:

  • (nil)


147
148
149
150
# File 'lib/searchlink/output.rb', line 147

def add_output(str)
  print str if SL.printout && !SL.clipboard
  SL.output << str
end

.add_query(hsh) ⇒ nil

Add to query string

Parameters:

  • hsh (Hash)

    The queries to add

Returns:

  • (nil)


234
235
236
237
# File 'lib/searchlink/output.rb', line 234

def add_query(hsh)
  SL.query ||= {}
  SL.query.merge!(hsh)
end

.add_report(str) ⇒ nil

Adds the given string to the report.

Parameters:

  • str (String)

    The string to add.

Returns:

  • (nil)


198
199
200
201
202
203
204
205
206
207
208
# File 'lib/searchlink/output.rb', line 198

def add_report(str)
  return unless SL.config["report"]

  unless SL.line_num.nil?
    position = "#{SL.line_num}:"
    position += SL.match_column.nil? ? "0:" : "#{SL.match_column}:"
    position += SL.match_length.nil? ? "0" : SL.match_length.to_s
  end
  SL.report.push("(#{position}): #{str}")
  warn "(#{position}): #{str}" unless SILENT
end

.ddg(search_terms, link_text = nil, timeout: , google: true, image: false) ⇒ SL::Searches::Result

Performs a DuckDuckGo search with the given search terms and link text. If link text is not provided, the first result will be returned. The search will timeout after the given number of seconds.

Parameters:

  • search_terms (String)

    The search terms to use

  • link_text (String) (defaults to: nil)

    The text of the link to search for

  • timeout (Integer) (defaults to: )

    The timeout for the search in seconds

  • google (Boolean) (defaults to: true)

    Use Google if API key installed

  • image (Boolean) (defaults to: false)

    Image search

Returns:

  • (SL::Searches::Result)

    The search result



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/searchlink/searches/duckduckgo.rb', line 161

def ddg(search_terms, link_text = nil, timeout: SL.config["timeout"], google: true, image: false)
  if google && SL::GoogleSearch.api_key?
    s_class = "google"
    s_type = image ? "img" : "gg"
  else
    s_class = "duckduckgo"
    s_type = image ? "ddgimg" : "g"
  end

  search = proc { SL::Searches.plugins[:search][s_class][:class].search(s_type, search_terms, link_text) }
  SL::Util.search_with_timeout(search, timeout)
end

.first_image(url) ⇒ Object



185
186
187
188
189
# File 'lib/searchlink/searches/duckduckgo.rb', line 185

def first_image(url)
  images = Curl::Html.new(url).images
  images.filter! { |img| img[:type] == "img" }
  images.first[:src] if images.any?
end

.google(search_terms, link_text = nil, timeout: , image: false) ⇒ Object

Performs a Google search if API key is available, otherwise defaults to DuckDuckGo

Parameters:

  • search_terms (String)

    The search terms

  • link_text (String) (defaults to: nil)

    The link text

  • timeout (Integer) (defaults to: )

    The timeout



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/searchlink/searches/duckduckgo.rb', line 138

def google(search_terms, link_text = nil, timeout: SL.config["timeout"], image: false)
  if SL::GoogleSearch.api_key?
    s_class = "google"
    s_type = image ? "img" : "gg"
  else
    s_class = "duckduckgo"
    s_type = image ? "ddgimg" : "g"
  end
  search = proc { SL::Searches.plugins[:search][s_class][:class].search(s_type, search_terms, link_text) }
  SL::Util.search_with_timeout(search, timeout)
end

Creates a link of the specified type with the given text, url, and title.

Parameters:

  • type (Symbol)

    The type of link to create.

  • text (String)

    The text of the link.

  • url (String)

    The URL of the link.

  • title (String) (defaults to: false)

    The title of the link.

  • force_title (Boolean) (defaults to: false)

    Whether to force the title to be included.

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/searchlink/output.rb', line 113

def make_link(type, text, url, title: false, force_title: false)
  url.sub!(%r{^//}, "https://")
  is_image = url =~ /(gif|jpe?g|png|webp)(?:\?.*?)?$/ ? true : false
  if is_image
    title = text || File.basename(url).sub(/\?.*$/, "")
    text = title if text.nil? || text.strip.empty?
  else
    title = title.gsub(/\P{Print}|\p{Cf}/, "") if title
    text = title || SL::URL.title(url) if SL.titleize && (!text || text.strip.empty?)
    text = text ? text.strip : title
  end
  title = title && (SL.config["include_titles"] || force_title) ? %( "#{title.clean}") : ""
  title = title.gsub(/[ \t]+/, " ")

  url.add_query_string!

  url = SL::Shortener.shorten(url, SL.shortener)

  case type.to_sym
  when :ref_title
    %(\n[#{text}]: #{url}#{title})
  when :ref_link
    %([#{text}][#{url}])
  when :inline
    %(#{is_image ? '!' : ''}[#{text}](#{url}#{title}))
  end
end

.new_version?Boolean

Check for a newer version than local copy using GitHub release tag

Returns:

  • (Boolean)

    false if no new version, or semantic version of latest release



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/searchlink/version.rb', line 42

def new_version?
  headers = {
    "Accept" => "application/vnd.github+json",
    "X-GitHub-Api-Version" => "2022-11-28"
  }
  if defined? Secrets::GH_AUTH_TOKEN
    headers["Authorization"] = "Bearer #{Secrets::GH_AUTH_TOKEN}"
  elsif SL.config["github_token"]
    headers["Authorization"] = "Bearer #{SL.config['github_token']}"
  end

  url = "https://api.github.com/repos/ttscoff/searchlink/releases/latest"
  page = Curl::Json.new(url, headers: headers)
  result = page.json

  if result
    latest_tag = result["tag_name"]

    return false unless latest_tag

    return false if latest_tag =~ /^#{Regexp.escape(SL::VERSION)}$/

    latest = SemVer.new(latest_tag)
    current = SemVer.new(SL::VERSION)

    return latest_tag if current.older_than(latest)
  else
    warn "Check for new version failed."
  end

  false
end

.notify(title, subtitle) ⇒ Object

Posts macOS notifications

Parameters:

  • title (String)

    The title of the notification

  • subtitle (String)

    The text of the notification



84
85
86
87
88
89
90
91
# File 'lib/searchlink/output.rb', line 84

def notify(title, subtitle)
  return unless SL.config["notifications"]

  title = title.gsub(/"/, '\\"')
  subtitle = subtitle.gsub(/"/, '\\"')

  `osascript -e 'display notification "SearchLink" with title "#{title}" subtitle "#{subtitle}"'`
end

Prints the errors.

Parameters:

  • type (String) (defaults to: "Errors")

    The type of errors.

Returns:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/searchlink/output.rb', line 258

def print_errors(type = "Errors")
  return if SL.errors.empty?

  out = ""
  inline = if SL.originput.split(/\n/).length > 1
             false
           else
             SL.config["inline"] || SL.originput.split(/\n/).length == 1
           end

  SL.errors.each do |k, v|
    next if v.empty?

    v.each_with_index do |err, i|
      out += "(#{k}) #{err}"
      out += if inline
               i == v.length - 1 ? " | " : ", "
             else
               "\n"
             end
    end
  end

  unless out == ""
    sep = inline ? " " : "\n"
    out.sub!(/\| /, "")
    out = "#{sep}<!-- #{type}:#{sep}#{out}-->#{sep}"
  end
  if SL.clipboard
    warn out
  else
    add_output out
  end
end

Prints the footer.

Returns:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/searchlink/output.rb', line 167

def print_footer
  unless SL.footer.empty?
    footnotes = []
    SL.footer.delete_if do |note|
      note.strip!
      case note
      when /^\[\^.+?\]/
        footnotes.push(note)
        true
      when /^\s*$/
        true
      else
        false
      end
    end

    output = SL.footer.sort.join("\n").strip
    output += "\n\n" if !output.empty? && !footnotes.empty?
    output += footnotes.join("\n\n") unless footnotes.empty?
    return output.gsub(/\n{3,}/, "\n\n")
  end

  ""
end

Prints or copies the given text.

Parameters:

  • text (String)

    The text to print or copy.

Returns:

  • (nil)


299
300
301
302
303
304
305
306
307
# File 'lib/searchlink/output.rb', line 299

def print_or_copy(text)
  # Process.exit unless text
  if SL.clipboard
    `echo #{Shellwords.escape(text)}|tr -d "\n"|pbcopy`
    print SL.originput
  else
    print text
  end
end

Prints the report.

Returns:



243
244
245
246
247
248
249
250
# File 'lib/searchlink/output.rb', line 243

def print_report
  return if (SL.config["inline"] && SL.originput.split(/\n/).length == 1) || SL.clipboard

  return if SL.report.empty?

  out = "\n<!-- Report:\n#{SL.report.join("\n")}\n-->\n"
  add_output out
end

.queryObject

Stores query parameters



70
71
72
# File 'lib/searchlink/output.rb', line 70

def query
  @query ||= {}
end

.site_search(site, search_terms, link_text) ⇒ Object

Perform a site-specific search

Parameters:

  • site (String)

    The site to search

  • search_terms (String)

    The search terms

  • link_text (String)

    The link text



181
182
183
# File 'lib/searchlink/searches/duckduckgo.rb', line 181

def site_search(site, search_terms, link_text)
  ddg("site:#{site} #{search_terms}", link_text)
end

.spell(phrase) ⇒ Object



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
# File 'lib/searchlink/searches/spelling.rb', line 28

def spell(phrase)
  aspell = if File.exist?("/usr/local/bin/aspell")
             "/usr/local/bin/aspell"
           elsif File.exist?("/opt/homebrew/bin/aspell")
             "/opt/homebrew/bin/aspell"
           else
             `which aspell`.strip
           end

  if aspell.nil? || aspell.empty?
    SL.add_error("Missing aspell", "Install aspell in to allow spelling corrections")
    return false
  end

  words = phrase.split(/\b/)
  output = ""
  words.each do |w|
    if w =~ /[A-Za-z]+/
      spell_res = `echo "#{w}" | #{aspell} --sug-mode=bad-spellers -C pipe | head -n 2 | tail -n 1`
      if spell_res.strip == "\*"
        output += w
      else
        spell_res.sub!(/.*?: /, "")
        results = spell_res.split(/, /).delete_if { |word| phrase =~ /^[a-z]/ && word =~ /[A-Z]/ }
        output += results[0]
      end
    else
      output += w
    end
  end
  output
end


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/searchlink/version.rb', line 75

def update_searchlink
  if `uname`.strip !~ /Darwin/
    add_output("Auto updating only available on macOS")
    return
  end

  new_version = SL.new_version?
  if new_version
    folder = File.expand_path("~/Downloads")
    services = File.expand_path("~/Library/Services")
    dl = File.join(folder, "SearchLink.zip")
    curl = TTY::Which.which("curl")
    `#{curl} -SsL -o "#{dl}" https://github.com/ttscoff/searchlink/releases/latest/download/SearchLink.zip`
    Dir.chdir(folder)
    `unzip -qo #{dl} -d #{folder}`
    FileUtils.rm(dl)

    ["SearchLink", "SearchLink File", "Jump to SearchLink Error"].each do |workflow|
      wflow = "#{workflow}.workflow"
      src = File.join(folder, "SearchLink Services", wflow)
      dest = File.join(services, wflow)
      if File.exist?(src) && File.exist?(dest)
        FileUtils.rm_rf(dest)
        FileUtils.mv(src, dest, force: true)
      end
    end
    add_output("Installed SearchLink #{new_version}")
    FileUtils.rm_rf("SearchLink Services")
  else
    add_output("Already up to date.")
  end
end

.version_checkObject



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
# File 'lib/searchlink/version.rb', line 10

def version_check
  cachefile = File.expand_path("~/.config/searchlink/cache/update.txt")

  FileUtils.mkdir_p(File.dirname(cachefile)) unless File.directory?(File.dirname(cachefile))

  if File.exist?(cachefile)
    last_check, latest_tag = IO.read(cachefile).strip.split(/\|/)
    last_time = Time.parse(last_check)
  else
    latest_tag = new_version?
    last_time = Time.now
  end

  if last_time + (24 * 60 * 60) < Time.now
    latest_tag = new_version?
    last_time = Time.now
  end

  latest_tag ||= SL::VERSION
  latest = SemVer.new(latest_tag)
  current = SemVer.new(SL::VERSION)

  File.open(cachefile, "w") { |f| f.puts("#{last_time.strftime('%c')}|#{latest}") }

  return "SearchLink v#{current}, #{latest} available. Run \"update\" to download." if latest_tag && current.older_than(latest)

  "SearchLink v#{current}"
end