From dba27acd6921aad57724b7598fecea2d38b5214a Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Thu, 4 Mar 2021 20:59:24 +0100 Subject: [PATCH] Change cover fetching logic to a MUCH simpler one --- project.clj | 8 ++++---- src/buchdesmonats/core.clj | 22 ++++++++++++++-------- src/buchdesmonats/sources/mojoreads.clj | 17 +++++++++++------ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/project.clj b/project.clj index bbdaa86..493db06 100644 --- a/project.clj +++ b/project.clj @@ -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." :url "https://git.okoyono.de/mezzomix/buch_des_monats" :license {:name "MIT License" :url "http://opensource.org/licenses/MIT"} - :dependencies [[org.clojure/clojure "1.10.0"] - [org.clojure/tools.logging "0.4.1"] + :dependencies [[org.clojure/clojure "1.10.2"] + [org.clojure/tools.logging "1.1.0"] [enlive "1.1.6"] [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 :profiles {:uberjar {:aot :all}}) diff --git a/src/buchdesmonats/core.clj b/src/buchdesmonats/core.clj index 06cb693..c67caef 100644 --- a/src/buchdesmonats/core.clj +++ b/src/buchdesmonats/core.clj @@ -1,4 +1,4 @@ -;;; Copyright (C) 2014-2020 Aaron Fischer Permission +;;; Copyright (C) 2014-2021 Aaron Fischer Permission ;;; 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 without restriction, including without limitation the rights to @@ -26,18 +26,21 @@ [buchdesmonats.sources.lovelybooks :as lovelybooks])) (defn bookurl->imageurl [bookurl] - (let [url (java.net.URL. bookurl)] - (case (.getHost url) - "www.lovelybooks.de" (lovelybooks/find-cover-image bookurl) - "lovelybooks.de" (lovelybooks/find-cover-image bookurl) - "mojoreads.com" (mojoreads/find-cover-image bookurl) - "mojoreads.de" (mojoreads/find-cover-image bookurl)))) + (let [url (java.net.URL. bookurl) + cover-url (case (.getHost url) + "www.lovelybooks.de" (lovelybooks/find-cover-image bookurl) + "lovelybooks.de" (lovelybooks/find-cover-image bookurl) + "mojoreads.com" (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] (let [url-to-fetch (bookurl->imageurl url) stream (http-client/get url-to-fetch {:as :byte-array})] (:body stream))) +; TODO: isbn statt hash verwenden (defn url->file [url target-dir] (io/file target-dir (str (hash/md5 url) ".jpg"))) @@ -55,7 +58,7 @@ (with-open [out (io/output-stream target-file)] (.write out bytes))) (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] (remove #(fs/exists? (url->file % target-dir)) @@ -80,6 +83,9 @@ (.write out content)))) (defn -main [& args] + (if (empty? args) + (do (log/fatal "Please give a cover type (comic/book)") + (System/exit 1))) (let [type (first args) 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"))] diff --git a/src/buchdesmonats/sources/mojoreads.clj b/src/buchdesmonats/sources/mojoreads.clj index ce9d898..cba31e4 100644 --- a/src/buchdesmonats/sources/mojoreads.clj +++ b/src/buchdesmonats/sources/mojoreads.clj @@ -1,8 +1,13 @@ -(ns buchdesmonats.sources.mojoreads - (:require [net.cgrand.enlive-html :as html])) +(ns buchdesmonats.sources.mojoreads) (defn find-cover-image [url] - (-> (html/html-resource (java.net.URL. url)) - (html/select [:div.mojoreads-page-content-container :img]) - (first) - (get-in [:attrs :src]))) + (let [isbn (re-find #"[0-9]+" url)] + (str "https://medien.ubitweb.de/bildzentrale_original/" + (subs isbn 0 3) + "/" + (subs isbn 3 6) + "/" + (subs isbn 6 9) + "/" + (subs isbn 9) + ".jpg")))