Commits

Jeffrey Stoffers  committed a803756

Added some bad comments and fixed an error in the parser.

Fixed an error that cause the parser to lose metadata for files that had more than one download.

  • Participants
  • Parent commits ebb4562

Comments (0)

Files changed (2)

File src/hb_client/.#core.clj

-necronian@x230.3587:1370507468

File src/hb_client/core.clj

             [digest :as digest]
             [clojure.java.io :as io]))
 
-(def hb-login-url "https://www.humblebundle.com/login")
-(def hb-games-url "https://www.humblebundle.com/home")
-(def username "")
-(def password "")
-
-(def hb-dl-path "")
+(defonce hb-login-url "https://www.humblebundle.com/login")
+(defonce hb-games-url "https://www.humblebundle.com/home")
 
 (defn enlive-parse
+  "Parse html with enlive"
   [s]
   (html/html-resource (java.io.StringReader. s)))
 
 (defn retrieve-games-page
-  []
+  "Log into and grab the humble bundle library page"
+  [username password]
   (binding [clj-http.core/*cookie-store* (clj-http.cookies/cookie-store)]
     (client/post hb-login-url
                  {:form-params {:username username :password password}})
     (client/get hb-games-url)))
 
-(defonce eh (enlive-parse (:body (retrieve-games-page))))
-
-(defonce rows (html/select eh [:div.row]))
-
 (defn parse-icn
+  "Parse the game icon url from the hmtl"
   [coll]
   (get-in (first (html/select coll [:div.icn :img])) [:attrs :src]))
 
 (defn parse-title
+  "Parse the game title from the html"
   [coll]
   (html/text (first (html/select coll [:div.gameinfo :div.title :a]))))
 
-(defn parse-subtitle
+(defn parse-developer
+  "Parse the developer/author from the html"
   [coll]
   (html/text (first (html/select coll [:div.gameinfo :div.subtitle :a]))))
 
 (defn parse-dl-type
+  "Get the download category of the download"
   [coll]
   (second (clojure.string/split (get-in coll [:attrs :class]) #" ")))
 
+(defn parse-dl-title
+  [coll]
+  (html/text (first (html/select coll [:div.flexbtn :a.a]))))
+
 (defn parse-dl-size
   [coll]
   (first (:content (first (html/select coll [:div.dlsize :span.mbs])))))
   [coll]
   (get-in (first (html/select coll [:div.flexbtn :a.a])) [:attrs :data-bt]))
 
-(defn parse-dl-title
-  [coll]
-  (html/text (first (html/select coll [:div.flexbtn :a.a]))))
-
 (defn parse-download
+  "Parse a download row of data for the metadata"
   [rows]
-  (first (merge (map (fn [coll]
-                       {:size (parse-dl-size coll)
-                        :md5 (parse-dl-md5 coll)
-                        :web (parse-dl-web coll)
-                        :torrent (parse-dl-torrent coll)
-                        :title (parse-dl-title coll)
-                        :date (parse-dl-date coll)})
-                     (html/select rows [:div.download])))))
+  (map (fn [coll]
+         {:size (parse-dl-size coll)
+          :md5 (parse-dl-md5 coll)
+          :web (parse-dl-web coll)
+          :torrent (parse-dl-torrent coll)
+          :title (parse-dl-title coll)
+          :date (parse-dl-date coll)})
+       (html/select rows [:div.download])))
 
 (defn parse-downloads
+  "Given an html row of humble bundle game data parse all the download types"
   [row]
   (merge (map (fn [coll]
                 {:dl-type (parse-dl-type coll)
               (html/select row [:div.downloads]))))
 
 (defn parse-row
+  "Given a list of humble bundle game rows return a map of game metadata"
   [rows]
   (map
    (fn [coll]
      {:title (parse-title coll)
-      :subtitle (parse-subtitle coll)
+      :developer (parse-developer coll)
       :icon (parse-icn coll)
       :downloads (parse-downloads coll)})
    rows))
 
 (defn parse-downloaded-md5
   []
-  (map (fn [f] {:file f :md5 (digest/md5 f)}) (filter (comp not directory?) (file-seq (io/file hb-dl-path)))))
-
-(defonce humble-data (parse-row rows))
-
-(defonce file-data (parse-downloaded-md5))
+  (map (fn [f]
+         {:file f :md5 (digest/md5 f)})
+       (filter (comp not directory?) (file-seq (io/file hb-dl-path)))))
 
 (defn -main
-  []
-  (:body (retrieve-games-page)))
+  [username password]
+  (let [library (-> (retrieve-games-page username password)
+                    (get-in [:body])
+                    (enlive-parse)
+                    (html/select [:div#regular_download_list])
+                    (html/select [:div.row])
+                    parse-row)]
+    
+    ))