utils.lisp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. (in-package :cl-user)
  2. (defpackage chad-music.utils
  3. (:use :cl #:audio-streams #:alexandria)
  4. (:export #:*standard-optimize-settings*
  5. #:text-tag
  6. #:bit-rate
  7. #:is-vbr
  8. #:duration
  9. #:publisher
  10. #:country
  11. #:keyify
  12. #:dekeyify
  13. #:http-request
  14. #:xml-request
  15. #:select-text
  16. #:json-request
  17. #:smart-f
  18. #:format-interval))
  19. (in-package :chad-music.utils)
  20. (eval-when (:compile-toplevel :load-toplevel :execute)
  21. #+dbg
  22. (defvar *standard-optimize-settings* '(optimize (debug 3)))
  23. #-dbg
  24. (defvar *standard-optimize-settings* '(optimize (speed 3) (safety 0) (space 0) (debug 0))))
  25. (defgeneric text-tag (stream desc) (:method ((object t) desc) nil))
  26. (defmethod text-tag ((mp3 id3:mp3-file) desc)
  27. (declare #.*standard-optimize-settings*)
  28. (loop for frame in (id3:get-frames mp3 '("TXXX"))
  29. when (string-equal (id3:desc frame) desc)
  30. do (return-from text-tag (id3:val frame))))
  31. (defgeneric bit-rate (stream) (:method ((object t)) nil))
  32. (defgeneric is-vbr (stream) (:method ((object t)) nil))
  33. (defgeneric duration (stream) (:method ((object t)) nil))
  34. (defgeneric publisher (stream) (:method ((object t)) nil))
  35. (defgeneric country (stream) (:method ((object t)) nil))
  36. (defmethod bit-rate ((mp3 id3:mp3-file))
  37. (let ((info (id3:audio-info mp3)))
  38. (when info
  39. (round (mpeg::bit-rate info) 1000))))
  40. (defmethod is-vbr ((mp3 id3:mp3-file))
  41. (let ((info (id3:audio-info mp3)))
  42. (when info
  43. (mpeg::is-vbr info))))
  44. (defmethod duration ((mp3 id3:mp3-file))
  45. (let ((info (id3:audio-info mp3)))
  46. (when info
  47. (mpeg::len info))))
  48. (defmethod publisher ((mp3 id3:mp3-file))
  49. (declare #.utils:*standard-optimize-settings*)
  50. (let ((frames (id3:get-frames mp3 '("TPUB" "TPB"))))
  51. (when frames
  52. (return-from publisher (id3:info (first frames)))))
  53. nil)
  54. (defmethod country ((mp3 id3:mp3-file))
  55. (text-tag mp3 "MusicBrainz Album Release Country"))
  56. (defmethod bit-rate ((m4a m4a:mp4-file))
  57. (let ((info (m4a:audio-info m4a)))
  58. (when info
  59. (round (m4a::avg-bit-rate info) 1000))))
  60. (defmethod duration ((m4a m4a:mp4-file))
  61. (let ((info (m4a:audio-info m4a)))
  62. (when info
  63. (m4a::seconds info))))
  64. (defun keyify (key)
  65. (intern (string-upcase (substitute #\- #\_ key)) :keyword))
  66. (defun dekeyify (keyword &optional preserve-dash)
  67. (let ((text (string-downcase (string keyword))))
  68. (if preserve-dash text (substitute #\_ #\- text))))
  69. (defun http-default (url &optional parameters)
  70. (let* ((uri (quri:uri url))
  71. (userinfo (quri:uri-userinfo uri)))
  72. (when parameters
  73. (let ((query (quri:url-encode-params parameters :encoding :utf-8)))
  74. (setf (quri:uri-query uri)
  75. (if (and (quri:uri-query uri)
  76. (string-not-equal (quri:uri-query uri) ""))
  77. (concatenate 'string (quri:uri-query uri) "&" query)
  78. query))))
  79. (when userinfo
  80. (setf (quri:uri-userinfo uri) nil))
  81. (unless (quri:uri-scheme uri)
  82. (setf (quri:uri-scheme uri) "http"))
  83. (values uri userinfo)))
  84. (defun http-request (url &rest args &key method version parameters content headers basic-auth cookie-jar keep-alive use-connection-pool (max-redirects 5) timeout force-binary want-stream ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent)
  85. (declare (ignore method version content basic-auth cookie-jar keep-alive use-connection-pool max-redirects timeout force-binary want-stream ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path))
  86. (multiple-value-bind (uri userinfo)
  87. (http-default url parameters)
  88. (when userinfo
  89. (push (cons :authorization (concatenate 'string "Basic "
  90. (base64:string-to-base64-string userinfo)))
  91. headers))
  92. (when user-agent
  93. (push (cons :user-agent user-agent) headers)
  94. (remf args :user-agent))
  95. (remf args :parameters)
  96. (remf args :headers)
  97. (apply #'dex:request uri :headers headers args)))
  98. ;; XML processing
  99. (defun xml-request (url &rest args &key method parameters content headers basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent encoding)
  100. (declare (ignore method parameters headers content basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent))
  101. (remf args :encoding)
  102. (multiple-value-bind (raw-body status headers uri)
  103. (apply #'http-request url :force-binary t args)
  104. (let ((encoding
  105. (or
  106. ;; 1. Provided encoding
  107. encoding
  108. ;; 2. Content-type header
  109. (ignore-errors
  110. (let ((ct (gethash "content-type" headers)))
  111. (subseq ct (1+ (position #\= ct)))))
  112. ;; 3. Parse first 1000 bytes
  113. (ignore-errors
  114. (let ((dom (plump:parse (flex:octets-to-string
  115. (subseq raw-body 0 (1+ (position (char-code #\>) raw-body :start 1000)))))))
  116. (or
  117. ;; 3.1 Content-type from http-equiv
  118. (ignore-errors
  119. (let ((ct (loop for meta in (get-by-tag dom "meta")
  120. for http-equiv = (plump:get-attribute meta "http-equiv")
  121. for content = (plump:get-attribute meta "content")
  122. when (equal http-equiv "Content-Type")
  123. return content)))
  124. (subseq ct (1+ (position #\= ct)))))
  125. ;; 3.2 'content' xml node attribute
  126. (ignore-errors (plump:get-attribute (plump:first-child dom) "encoding")))))
  127. ;; 4. Default 'utf-8'
  128. "utf-8")))
  129. (values
  130. (handler-bind ((flex:external-format-encoding-error
  131. (lambda (c) (use-value #\? c))))
  132. (plump:parse
  133. (flex:octets-to-string raw-body :external-format (intern encoding 'keyword))))
  134. status headers uri))))
  135. (defun get-by-tag (node tag)
  136. (nreverse (org.shirakumo.plump.dom::get-elements-by-tag-name node tag)))
  137. (defun select-text (node &optional selector)
  138. (ignore-errors
  139. (when selector (setf node (elt (clss:select selector node) 0)))
  140. (plump:traverse node #'(lambda (n) (setf (plump:text n) ""))
  141. :test #'plump:comment-p)
  142. (plump:text (plump:strip node))))
  143. (defun trim-nil (text)
  144. (when text
  145. (let ((text (string-trim " " text)))
  146. (unless (zerop (length text))
  147. text))))
  148. (defun text-with-cdata (node)
  149. "Compiles all text nodes within the nesting-node into one string."
  150. (with-output-to-string (stream)
  151. (labels ((r (node)
  152. (loop for child across (plump:children node)
  153. do (typecase child
  154. (plump:text-node (write-string (plump:text child) stream))
  155. (plump:cdata (write-string (plump:text child) stream))
  156. (plump:nesting-node (r child))))))
  157. (r node))))
  158. (defun child-text (node tag)
  159. (alexandria:when-let (child (car (get-by-tag node tag)))
  160. (trim-nil (text-with-cdata child))))
  161. (defun clean-text (text)
  162. (when text (trim-nil (plump:text (plump:parse text)))))
  163. (defun json-request (url &rest args &key method parameters content headers basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent (as :plist))
  164. (declare (ignore method parameters basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent))
  165. (remf args :as)
  166. (when content
  167. (push (cons :content-type "application/json") headers))
  168. (remf args :headers)
  169. (multiple-value-bind (body status headers uri)
  170. (apply #'http-request url :headers headers args)
  171. (unless (stringp body)
  172. (setf body (trivial-utf-8:utf-8-bytes-to-string body)))
  173. (values (jojo:parse body :as as) status headers uri)))
  174. (defun smart-f (arg &optional digits)
  175. (with-output-to-string (s)
  176. (prin1 (cond ((= (round arg) arg) (round arg))
  177. (digits (float (/ (round (* arg (expt 10 digits)))
  178. (expt 10 digits))))
  179. (t arg))
  180. s)))
  181. (defun format-interval (seconds)
  182. (cond
  183. ((< seconds 60) (format nil "~A sec" seconds))
  184. ((< seconds (* 60 60)) (format nil "~A mins" (smart-f (/ seconds 60) 1)))
  185. ((< seconds (* 60 60 24)) (format nil "~A hours" (smart-f (/ seconds (* 60 60)) 1)))
  186. ((< seconds (* 60 60 24 7)) (format nil "~A days" (smart-f (/ seconds (* 60 60 24)) 1)))
  187. ((< seconds (* 60 60 24 7 54)) (format nil "~A weeks" (smart-f (/ seconds (* 60 60 24 7)) 1)))
  188. (:otherwise (format nil "~A years" (smart-f (/ seconds (* 60 60 24 365.25)) 1)))))