1
0

utils.lisp 8.0 KB

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