Created
December 24, 2023 21:10
-
-
Save ttscoff/88928980fa420dc700ab45d532c0cb73 to your computer and use it in GitHub Desktop.
Bitlyize script for use in Bitlyize Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'yaml' | |
# Settings is a YAML file containing `:token:` and `:domain:` keys. | |
settings = YAML.load(IO.read(File.expand_path('~/.config/bitly/config.yaml'))) | |
bitly_key = settings[:token] | |
bitly_domain = settings[:domain] | |
debug = false | |
function = :auto # :shorten, :lengthen, or :auto | |
output = :text # :text or :urls | |
# String helpers | |
class ::String | |
def scrub | |
encode('utf-16', invalid: :replace).encode('utf-8') | |
end | |
def scrub! | |
replace scrub | |
end | |
end | |
Encoding.default_external = Encoding::UTF_8 | |
Encoding.default_internal = Encoding::UTF_8 | |
input = $stdin.read.force_encoding('utf-8').scrub | |
# Bit.ly linking | |
class BitlyLink | |
attr_writer :debug | |
def initialize(apikey) | |
@apikey = apikey | |
end | |
def long_link(options) | |
res = lengthen_link({ short_url: options[:url] }) | |
return false unless res | |
res['long_url'] | |
end | |
def lengthen_link(data = {}) | |
url = data[:short_url].sub(%r{^https://}, '') | |
cmd = [ | |
%(curl -SsL -H 'Authorization: Bearer #{@apikey}'), | |
'-X GET', "https://api-ssl.bitly.com/v4/bitlinks/#{url}" | |
] | |
JSON.parse(`#{cmd.join(' ')}`.strip) | |
end | |
def short_link(options) | |
domain = options.key?(:domain) ? options[:domain] : 'bit.ly' | |
raise 'No url provided' unless options.key?(:url) | |
res = shorten_link({ long_url: options[:url], domain: domain }) | |
return false unless res | |
res['link'] | |
rescue StandardError | |
raise 'Error shortening link' | |
end | |
def shorten_link(data = {}) | |
cmd = [ | |
%(curl -SsL -H 'Authorization: Bearer #{@apikey}'), | |
%(-H 'Content-Type: application/json'), | |
'-X POST', %(-d '{ "long_url": "#{data[:long_url]}", "domain": "#{data[:domain]}" }'), 'https://api-ssl.bitly.com/v4/shorten' | |
] | |
JSON.parse(`#{cmd.join(' ')}`.strip) | |
end | |
end | |
bl = BitlyLink.new(bitly_key) | |
bl.debug = debug | |
input.gsub!(/((?:http|https):\/\/[\w\-_]+(?:\.[\w\-_]+)+(?:[\w\-\.,@?^=%&:\/~\+#]*[\w\-\@^=%&\/~\+#])?)/).each do |url| | |
case function | |
when :shorten | |
if url =~ %r{https://bit.ly/\S+} || url =~ %r{https://#{bitly_domain}/\S+} | |
url | |
else | |
short_url = bl.short_link({ url: url, domain: bitly_domain }) | |
short_url || url | |
end | |
when :lengthen | |
if url =~ %r{https://bit.ly/\S+} || url =~ %r{https://#{bitly_domain}/\S+} | |
long_url = bl.long_link({ url: url }) | |
long_url || url | |
else | |
url | |
end | |
when :auto | |
if url =~ %r{https://bit.ly/\S+} || url =~ %r{https://#{bitly_domain}/\S+} | |
long_url = bl.long_link({ url: url }) | |
long_url || url | |
else | |
short_url = bl.short_link({ url: url, domain: bitly_domain }) | |
short_url || url | |
end | |
end | |
end | |
if output == :text | |
print input | |
else | |
puts input.scan(/((?:http|https):\/\/[\w\-_]+(?:\.[\w\-_]+)+(?:[\w\-\.,@?^=%&:\/~\+#]*[\w\-\@^=%&\/~\+#])?)/) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment