<commit_before>(unless (eql:qrequire :network)
  (error "EQL module :network could not be found/loaded")
  (eql:qq))

(defpackage :download
  (:use :common-lisp :eql)
  (:export
   #:ini
   #:download))

(in-package :download)

(defvar *manager* (qnew "QNetworkAccessManager"))

(defun ini ()
  (qconnect *manager* "finished(QNetworkReply*)" 'download-finished))

(defun download (name)
  (qlet ((url "QUrl(QString)" name)
         (request "QNetworkRequest(QUrl)" url))
    (qfun *manager* "get" request)))

(defun download-finished (reply)
  (qfun reply "deleteLater") ; QNetworkReply*: heap result, delete manually
  (let ((data (qfun reply "readAll")))
    (save-data data)
    (format t "~%Downloaded ~D bytes, see \"download.data\".~%~%" (length data))
    (qq)))

(defun save-data (data)
  (with-open-file (s "download.data" :direction :output :if-exists :supersede
                     :element-type '(signed-byte 8))
    (write-sequence data s)))

(progn
  (ini)
  (download "http://planet.lisp.org/"))<commit_msg>Example 6 (download): add message-box (success/error)<commit_after>(unless (eql:qrequire :network)
  (error "EQL module :network could not be found/loaded")
  (eql:qq))

(defpackage :download
  (:use :common-lisp :eql)
  (:export
   #:ini
   #:download))

(in-package :download)

(defvar *manager* (qnew "QNetworkAccessManager"))

(defun ini ()
  (qconnect *manager* "finished(QNetworkReply*)" 'download-finished))

(defun download (name)
  (qlet ((url "QUrl(QString)" name)
         (request "QNetworkRequest(QUrl)" url))
    (qfun *manager* "get" request)))

(defun download-finished (reply)
  (qfun reply "deleteLater") ; QNetworkReply*: heap result, delete manually
  (let ((error (qfun reply "error")))
    (if (= |QNetworkReply.NoError| error)
        (let ((data (qfun reply "readAll")))
          (save-data data)
          (qfun "QMessageBox" "information" nil "EQL"
                (format nil (tr "~%Downloaded ~:D bytes, see \"download.data\".~%~%") (length data))))
        (show-error error)))
  (qq))

(defun save-data (data)
  (with-open-file (s "download.data" :direction :output :if-exists :supersede
                     :element-type '(signed-byte 8))
    (write-sequence data s)))

(defun show-error (error)
  (let (msg) 
    (do-external-symbols (symbol)
      (let ((name (symbol-name symbol)))
        (when (and (x:starts-with "QNetworkReply." name)
                   (x:ends-with "Error" name)
                   (= error (symbol-value symbol)))
          (setf msg (format nil (tr "Download error: <span style='color:red; font-weight:bold;'>~A</span>")
                            (subseq name (1+ (position #\. name)) (- (length name) (length "Error")))))
          (return))))
    (qfun "QMessageBox" "critical" nil "EQL" (if msg msg (tr "Unkown download error.")))))

(progn
  (ini)
  (download "http://planet.lisp.org/"))
{'fraction_non_alphanumeric': 0.1735048678720445, 'fraction_numerical': 0.0013908205841446453, 'mean_word_length': 3.3328313253012047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3360929', 'n_tokens_mistral': 998, 'n_tokens_neox': 968, 'n_words': 262}