<commit_before>(ns usedbooks-search.core
  (:require [net.cgrand.enlive-html :as html])
  (:gen-class))

(def base-url "http://off.aladin.co.kr/usedstore/wsearchresult.aspx")

(def offline-shops
  ["gangnam"
   "geondae"
   "gwangju"
   "nowon"
   "daegu"
   "daejeon"
   "daehakro"
   "busan"
   "bucheon"
   "bundang"
   "sanbon"
   "suwon"
   "sillim"
   "sinchon"
   "yeonsinnae"
   "ulsan"
   "ilsan"
   "sincheon"
   "jeonju"
   "jongno"
   "cheonan"
   "cheongju"])

(defn usedstore-url [shop encoded-query]
  (str base-url "?offcode=" shop "&SearchWord=" encoded-query))

(defn str->euckr-seq [^String s]
  (seq (.getBytes s "euc-kr")))

(defn encode-aladin-percent [s]
  (map (fn [c]
         (cond
           (= c 0x20) "+"      ; ' '
           (= c 0x5c) ""       ; '\'
           (or (< 0x2f c 0x4a) ; 0-9
               (< 0x40 c 0x5b) ; A-Z
               (< 0x60 c 0x7b) ; a-z
               (= c 0x2a)      ; '*'
               (= c 0x2d)      ; '-'
               (= c 0x2e)      ; '.'
               (= c 0x5f)) (char c) ; '_'
           :else (str "%" (format "%02X" (if (< c 0) (+ c 256) c))))) s))

(defn encode-aladin-query [qry]
  (clojure.string/join
    (encode-aladin-percent (str->euckr-seq qry))))

(defn fetch-url [url]
  (html/html-resource (java.net.URL. url)))

(defn parse-titles [q]
  (html/select
    (fetch-url (usedstore-url "jongno"
                              (encode-aladin-query q)))
    [:a.bo_l :b]))

(defn parse-each-prices [q]
  (html/select
    (fetch-url (usedstore-url "jongno"
                              (encode-aladin-query q)))
    [:span.ss_p2]))

(defn parse-each-nbooks [q]
  (html/select
    (fetch-url (usedstore-url "jongno"
                              (encode-aladin-query q)))
    [:span.ss_p4]))

(defn parse-total-nbooks [q]
  (html/select
    (fetch-url (usedstore-url "jongno"
                              (encode-aladin-query q)))
    [:span.ss_f_g_l]))

(defn find-total-nbooks [q]
  (let [result
        (first (:content (first (parse-total-nbooks q))))]
    (if (nil? result)
      0
      result)))

(defn -main [& args]
  (if (= (count args) 0)
    (println "USAGE: usedbooks-search <query-string>")
    (println (find-total-nbooks (first args)))))<commit_msg>move running point of fetch-url<commit_after>(ns usedbooks-search.core
  (:require [net.cgrand.enlive-html :as html])
  (:gen-class))

(def base-url "http://off.aladin.co.kr/usedstore/wsearchresult.aspx")

(def offline-shops
  ["gangnam"
   "geondae"
   "gwangju"
   "nowon"
   "daegu"
   "daejeon"
   "daehakro"
   "busan"
   "bucheon"
   "bundang"
   "sanbon"
   "suwon"
   "sillim"
   "sinchon"
   "yeonsinnae"
   "ulsan"
   "ilsan"
   "sincheon"
   "jeonju"
   "jongno"
   "cheonan"
   "cheongju"])

(defn usedstore-url [shop encoded-query]
  (str base-url "?offcode=" shop "&SearchWord=" encoded-query))

(defn str->euckr-seq [^String s]
  (seq (.getBytes s "euc-kr")))

(defn encode-aladin-percent [s]
  (map (fn [c]
         (cond
           (= c 0x20) "+"      ; ' '
           (= c 0x5c) ""       ; '\'
           (or (< 0x2f c 0x4a) ; 0-9
               (< 0x40 c 0x5b) ; A-Z
               (< 0x60 c 0x7b) ; a-z
               (= c 0x2a)      ; '*'
               (= c 0x2d)      ; '-'
               (= c 0x2e)      ; '.'
               (= c 0x5f)) (char c) ; '_'
           :else (str "%" (format "%02X" (if (< c 0) (+ c 256) c))))) s))

(defn encode-aladin-query [qry]
  (clojure.string/join
    (encode-aladin-percent (str->euckr-seq qry))))

(defn fetch-url [url]
  (html/html-resource (java.net.URL. url)))

(defn html-select [html element]
  (html/select html element))

(defn select-titles [html]
  (html-select html [:a.bo_l :b]))

(defn select-each-prices [html]
  (html-select html [:span.ss_p2]))

(defn select-each-nbooks [html]
  (html-select html [:span.ss_p4]))

(defn select-total-nbooks [html]
  (html-select html [:span.ss_f_g_l]))

(defn find-total-nbooks [q]
  (let [url    (usedstore-url "jongno" (encode-aladin-query q))
        html   (fetch-url url)
        result (first (:content (first (select-total-nbooks html))))]
    (if (nil? result)
      0
      result)))

(defn -main [& args]
  (if (= (count args) 0)
    (println "USAGE: usedbooks-search <query-string>")
    (println (find-total-nbooks (first args)))))
{'fraction_non_alphanumeric': 0.18154276392449312, 'fraction_numerical': 0.01817758098345374, 'mean_word_length': 2.1169208424110386, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6799041', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1758, 'n_words': 364}