Change cover fetching logic to a MUCH simpler one

This commit is contained in:
Aaron Fischer 2021-03-04 20:59:24 +01:00
parent d4cedfe929
commit dba27acd69
3 changed files with 29 additions and 18 deletions

View file

@ -1,13 +1,13 @@
(defproject buchdesmonats "1.7" (defproject buchdesmonats "1.8"
:description "A simple tool to fetch covers of the month from the okoyono.de project." :description "A simple tool to fetch covers of the month from the okoyono.de project."
:url "https://git.okoyono.de/mezzomix/buch_des_monats" :url "https://git.okoyono.de/mezzomix/buch_des_monats"
:license {:name "MIT License" :license {:name "MIT License"
:url "http://opensource.org/licenses/MIT"} :url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.10.0"] :dependencies [[org.clojure/clojure "1.10.2"]
[org.clojure/tools.logging "0.4.1"] [org.clojure/tools.logging "1.1.0"]
[enlive "1.1.6"] [enlive "1.1.6"]
[me.raynes/fs "1.4.6"] ;; R.I.P. Anthony [me.raynes/fs "1.4.6"] ;; R.I.P. Anthony
[clj-http "3.9.1"]] [clj-http "3.12.1"]]
:main ^:skip-aot buchdesmonats.core :main ^:skip-aot buchdesmonats.core
:profiles {:uberjar {:aot :all}}) :profiles {:uberjar {:aot :all}})

View file

@ -1,4 +1,4 @@
;;; Copyright (C) 2014-2020 Aaron Fischer <mail@aaron-fischer.net> Permission ;;; Copyright (C) 2014-2021 Aaron Fischer <mail@aaron-fischer.net> Permission
;;; is hereby granted, free of charge, to any person obtaining a copy of this ;;; is hereby granted, free of charge, to any person obtaining a copy of this
;;; software and associated documentation files (the "Software"), to deal in the ;;; software and associated documentation files (the "Software"), to deal in the
;;; Software without restriction, including without limitation the rights to ;;; Software without restriction, including without limitation the rights to
@ -26,18 +26,21 @@
[buchdesmonats.sources.lovelybooks :as lovelybooks])) [buchdesmonats.sources.lovelybooks :as lovelybooks]))
(defn bookurl->imageurl [bookurl] (defn bookurl->imageurl [bookurl]
(let [url (java.net.URL. bookurl)] (let [url (java.net.URL. bookurl)
(case (.getHost url) cover-url (case (.getHost url)
"www.lovelybooks.de" (lovelybooks/find-cover-image bookurl) "www.lovelybooks.de" (lovelybooks/find-cover-image bookurl)
"lovelybooks.de" (lovelybooks/find-cover-image bookurl) "lovelybooks.de" (lovelybooks/find-cover-image bookurl)
"mojoreads.com" (mojoreads/find-cover-image bookurl) "mojoreads.com" (mojoreads/find-cover-image bookurl)
"mojoreads.de" (mojoreads/find-cover-image bookurl)))) "mojoreads.de" (mojoreads/find-cover-image bookurl))]
(log/info "Using cover URL: " cover-url " ...")
cover-url))
(defn imgurl->bytes [url] (defn imgurl->bytes [url]
(let [url-to-fetch (bookurl->imageurl url) (let [url-to-fetch (bookurl->imageurl url)
stream (http-client/get url-to-fetch {:as :byte-array})] stream (http-client/get url-to-fetch {:as :byte-array})]
(:body stream))) (:body stream)))
; TODO: isbn statt hash verwenden
(defn url->file [url target-dir] (defn url->file [url target-dir]
(io/file target-dir (str (hash/md5 url) ".jpg"))) (io/file target-dir (str (hash/md5 url) ".jpg")))
@ -55,7 +58,7 @@
(with-open [out (io/output-stream target-file)] (with-open [out (io/output-stream target-file)]
(.write out bytes))) (.write out bytes)))
(catch Exception e (catch Exception e
(log/info "Problem with " url ":" (get-in e [:via :message]) ". Skip it.")))) (log/info "Problem with " url ":" e ". Skip it."))))
(defn find-missing-covers [books-url target-dir] (defn find-missing-covers [books-url target-dir]
(remove #(fs/exists? (url->file % target-dir)) (remove #(fs/exists? (url->file % target-dir))
@ -80,6 +83,9 @@
(.write out content)))) (.write out content))))
(defn -main [& args] (defn -main [& args]
(if (empty? args)
(do (log/fatal "Please give a cover type (comic/book)")
(System/exit 1)))
(let [type (first args) (let [type (first args)
datasource-url (str "https://git.okoyono.de/mezzo/buch_des_monats/raw/master/" (clojure.string/upper-case type) ".mkd") datasource-url (str "https://git.okoyono.de/mezzo/buch_des_monats/raw/master/" (clojure.string/upper-case type) ".mkd")
target-dir (io/file "public" (str type "-covers"))] target-dir (io/file "public" (str type "-covers"))]

View file

@ -1,8 +1,13 @@
(ns buchdesmonats.sources.mojoreads (ns buchdesmonats.sources.mojoreads)
(:require [net.cgrand.enlive-html :as html]))
(defn find-cover-image [url] (defn find-cover-image [url]
(-> (html/html-resource (java.net.URL. url)) (let [isbn (re-find #"[0-9]+" url)]
(html/select [:div.mojoreads-page-content-container :img]) (str "https://medien.ubitweb.de/bildzentrale_original/"
(first) (subs isbn 0 3)
(get-in [:attrs :src]))) "/"
(subs isbn 3 6)
"/"
(subs isbn 6 9)
"/"
(subs isbn 9)
".jpg")))