shorten-url allows you to insert a shortened alternative for a given URL. Can be useful for chatting.
;;; shorten-url.el --- URL shortener -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Bad Blue Bull
;; Author: Bad Blue Bull <bad_blue_bull@yandex.ru>
;; Keywords: irc, icq
;; Version: 1.0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see
;; <https://www.gnu.org/licenses/>.
;;; Commentary:
;; shorten-url allows you to insert a shortened alternative for a given
;; URL. Can be useful for chatting.
;;
;; A request for a URL produced by concatenation of shorten-url-base
;; with a URL passed as an argument is fulfilled and data from HTTP
;; response gets inserted into current buffer.
;;
;; At the moment of this release TIMEOUT optional variable of
;; url-retrieve-synchronously may break execution of this function
;; (bug #34607), so it's unused.
;;
;; Also url-retrieve-synchronously takes long time to retrieve a URL
;; that hasn't been retrieved before in some interval of time in same
;; Emacs session (bug #34652).
;; Configuration:
;; You can customize shorten-url-base to use diffirent URL shortener web
;; service.
;; Usage:
;; Call shorten-url from Lisp or execute it as command with URL you want
;; to shorten passed as an argument.
;;
;; Example:
;;
;; M-x shorten-url https://debbugs.gnu.org/cgi/bugreport.cgi?bug=34607
;;
;; result inserted: https://qps.ru/MjrtW
;;
;; For convenient usage bind a key sequence so that it will insert
;; shortened version of URL stored in kill ring.
;; Here is example how you can bind C-c C-y key sequence so that when it
;; is used in ERC it inserts a shortened URL corresponding to element of
;; kill ring that the yank command would insert:
;;
;; (add-hook 'erc-mode-hook (lambda ()
;; (local-set-key (kbd "C-c C-y")
;; (lambda (&optional arg)
;; (interactive "*P")
;; (shorten-url (current-kill
;; (cond
;; ((listp arg)
;; 0)
;; ((eq arg '-)
;; -2)
;; (t
;; (1- arg)))
;; ))))))
;;; Code:
(provide 'shorten-url)
(defcustom shorten-url-base "https://qps.ru/api?url="
"base for URL shortener.
short-url will concatenate it with URL passed as argument"
:type '(choice (const :tag "https://qps.ru/api?url="
"https://qps.ru/api?url=")
(const :tag "https://clck.ru/--?url="
"https://clck.ru/--?url=")
(string :tag "custom" "")))
(defun shorten-url (url)
"Insert shortened URL (passed as argument).
Contact a URL produced by concatenation shorten-url-base and the
argument and insert data from response from the server into current
buffer."
(interactive "sURL to shorten: ")
(insert (with-current-buffer
(url-retrieve-synchronously
(concat shorten-url-base url) nil 't)
(goto-char (point-min))
(search-forward "\n\n" nil 't)
(unwind-protect
(buffer-substring (point) (point-max))
(kill-buffer (current-buffer)))))
(message "inserted shortened URL for %s" url))
;;; shorten-url.el ends here