Mobile prettification

main
Eric Ihli 4 years ago
parent bfa65a6de4
commit 4907cfc196

@ -3,6 +3,7 @@
org.clojure/tools.deps.alpha
{:git/url "https://github.com/clojure/tools.deps.alpha.git"
:sha "d492e97259c013ba401c5238842cd3445839d020"}
digest {:mvn/version "1.4.9"}
hiccup {:mvn/version "1.0.5"}
com.taoensso/timbre {:mvn/version "5.1.0"}
com.taoensso/carmine {:mvn/version "3.0.1"}

@ -1,5 +1,6 @@
(ns user
(:require [clojure.tools.deps.alpha.repl :refer [add-lib]]
(:require [clojure.string :as string]
[clojure.tools.deps.alpha.repl :refer [add-lib]]
[clojure.tools.namespace.repl :refer [set-refresh-dirs]]
[integrant.repl :as repl]
[taoensso.carmine :as car]
@ -10,7 +11,10 @@
[integrant.core :as ig]
[hawk.core :as hawk]
[clojure.java.io :as io]
[com.darklimericks.util.identicon :as identicon]
[com.darklimericks.linguistics.core :as linguistics]
[com.darklimericks.server.limericks :as limericks]
[com.darklimericks.db.albums :as db.albums]
[com.darklimericks.server.util :as util]
[com.owoga.prhyme.limerick :as limerick]
[com.darklimericks.server.system]
@ -61,10 +65,22 @@
(comment
(init)
(auto-reset)
(-> state/system :worker/limerick-gen)
(car/wcar
(-> state/system :database.kv/connection)
(car-mq/enqueue "limericks" '((A 9) (A 9) (B 5) (B 5) (A 9))))
(let [db (-> state/system :database.sql/connection)
albums (db.albums/most-recent-albums db)]
(->> albums
(map :album/name)
(map #(string/replace % #" " "-"))
(map string/lower-case)
(map #(identicon/generate % 128))))
(do
(repeatedly
5
(fn []
(car/wcar
(-> state/system :database.kv/connection)
(car-mq/enqueue "limericks" '((A 8) (A 8) (B 4) (B 4) (A 8))))))
nil)
(car/wcar
(-> state/system :database.kv/connection)
@ -73,13 +89,20 @@
(car/set "baz" "buzz")
(car/get "baz"))
(limericks/get-artist-and-album-for-new-limerick (-> state/system :database.sql/connection))
(repeatedly
50
1
(fn []
(let [handler (handlers/limerick-generation-post-handler
(-> state/system :database.sql/connection)
(-> state/system :app/cache))]
(handler {:params {:scheme "A9 A9 B5 B5 A9" #_'((A 9) (A 9) (B 5) (B 5) (A 9))}}))))
(db.albums/num-albums
(-> state/system :database.sql/connection))
(limericks/get-artist-and-album-for-new-limerick
(-> state/system :database.sql/connection))
(reitit/match-by-path
(-> state/system :app/router)
@ -87,7 +110,9 @@
(let [router (-> state/system :app/router)]
(util/route-name->path {::reitit/router router}
:com.darklimericks.server.system/artist))
:com.darklimericks.server.system/artist
{:artist-id 1}))
(let [router (-> state/system :app/router)]
(util/route-name->path
{::reitit/router router}
@ -100,6 +125,4 @@
%
{:artist-id 1}))
(reitit/match->path)))
)

@ -14,7 +14,7 @@
(concat [s] params)))
(defn insert-album [db name artist-id]
(jdbc/execute! db (insert-album-sql name artist-id)))
(jdbc/execute-one! db (insert-album-sql name artist-id)))
(defn artist-albums-sql [artist-id]
(honey.sql/format
@ -41,3 +41,24 @@
(defn album [db id]
(jdbc.sql/get-by-id db :album id))
(defn most-recent-albums [db]
(->> {:select [:*]
:from [:album]
:order-by [[:album.id :desc]]}
honey.sql/format
(jdbc/execute! db)))
(defn num-albums [db]
(:count
(jdbc/execute-one!
db
(honey.sql/format
{:select [:%count.*]
:from [:album]}))))
(comment
(honey.sql/format
{:select :*
:from [:album]
:order-by [[:album.id :desc]]}))

@ -13,7 +13,7 @@
(concat [s] params)))
(defn insert-artist [db name]
(jdbc/execute! db (insert-artist-sql name)))
(jdbc/execute-one! db (insert-artist-sql name)))
(defn most-recent-artist-sql []
(-> {:select [:*]
@ -40,3 +40,11 @@
{:select [:*]
:from [:artist]
:where [:= :id id]})))
(defn num-artists [db]
(:count
(jdbc/execute-one!
db
(honey.sql/format
{:select [:%count.*]
:from [:artist]}))))

@ -2,7 +2,6 @@
(:require [taoensso.timbre :as timbre]
[hiccup.core :as hiccup]
[reitit.ring :as ring]
[reitit.core :as reitit]
[clojure.string :as string]
[clojure.core.async :as async]
[com.darklimericks.server.util :as util]
@ -12,11 +11,24 @@
[com.darklimericks.server.views :as views]
[com.darklimericks.server.limericks :as limericks]))
(defn home-handler [request]
(timbre/info "home-handler")
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (hiccup/html (views/home request))})
(defn home-handler
[db]
(fn [request]
(let [recent-albums (db.albums/most-recent-albums db)
artists-by-album (into
{}
(map
(fn [{:album/keys [id artist_id]}]
(vector id (db.artists/artist db artist_id)))
recent-albums))]
(timbre/info recent-albums artists-by-album)
(timbre/info "home-handler")
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (hiccup/html (views/home
request
recent-albums
artists-by-album))})))
(def resource-handler (ring/create-resource-handler {:allow-symlinks? true}))
@ -37,21 +49,14 @@
album-id))))
{:status 301
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (views/page "Dark Limericks"
[:h1 "Creating your limerick..."]
[:div "Submission processing..."]
[:h1 "Current limericks"]
(views/limerick-tasks tasks))})))
:body (views/wrapper
db
{}
[:h1 "Creating your limerick..."]
[:div "Submission processing..."]
[:h1 "Current limericks"]
(views/limerick-tasks tasks))})))
(comment
(let [c (async/thread
(Thread/sleep 1000)
200)]
(async/go
(let [val (async/<! c)]
(println (+ 20 val)))))
)
(defn limericks-get-handler [db cache]
(fn [request]
(let [artist-id (get-in request [:parameters :path :artist-id])
@ -61,8 +66,9 @@
limericks (db.limericks/album-limericks db album-id)]
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (views/page
"Dark Limericks"
:body (views/wrapper
db
{}
[:div.flex.items-center.flex-column
[:div.f3.pt4.light-yellow
(->> artist
@ -128,8 +134,9 @@
(let [artists (db.artists/artists-by-letter db (string/upper-case letter))]
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (views/page
"Dark Limericks"
:body (views/wrapper
db
{}
(when artists
(let [[right-hand-side-artists
left-hand-side-artists]
@ -151,13 +158,6 @@
{:href "#"}
(:artist/name artist)])]])))})))
(comment
(let [artists ["a"]]
(partition 1 1 nil artists))
((juxt #(take 1 %) #(drop 1 %)) [1 2 3 4])
(into [] (take 1) [1 2 3])
)
(defn artist-get-handler [db]
(fn [request]
(let [artist-id (get-in request [:parameters :path :artist-id])

@ -63,18 +63,31 @@
limericks (db.limericks/album-limericks db (:album/id (first albums)))]
(cond
(< (count limericks) 10)
[(:artist/id artist) (:album/id (first albums))]
(do
(when (or (nil? artist)
(nil? (first albums)))
(throw (ex-info "Nil artist or album" {:artist artist
:album (first albums)})))
[(:artist/id artist) (:album/id (first albums))])
(< (count albums) 5)
(let [album-name (linguistics/gen-album)
{album-id :album/id} (albums/insert-album db album-name (:id artist))]
[(:id artist) album-id])
{album-id :album/id} (albums/insert-album db album-name (:artist/id artist))]
(when (or (nil? (:artist/id artist))
(nil? album-id))
(throw (ex-info "Nil artist or album" {:artist artist
:album album-id})))
[(:artist/id artist) album-id])
:else
(let [artist-name (linguistics/gen-artist)
{artist-id :artist/id} (artists/insert-artist db artist-name)
album-name (linguistics/gen-album)
{album-id :album/id} (albums/insert-album db album-name artist-id)]
(when (or (nil? artist-id)
(nil? album-id))
(throw (ex-info "Nil artist or album" {:artist artist-id
:album album-id})))
[artist-id album-id]))))
(defn get-limerick-name [lines]

@ -15,14 +15,23 @@
[com.darklimericks.server.interceptors :as interceptors]
[com.darklimericks.server.db :as db]))
(def worker (atom nil))
(defmethod ig/init-key :worker/limerick-gen [_ {:keys [db kv]}]
(car-mq/worker
kv
"limericks"
{:handler
(fn [{:keys [message attempt]}]
(timbre/info "Received" message)
(limericks/generate-limerick-worker db message))}))
(when (nil? @worker)
(reset!
worker
(car-mq/worker
kv
"limericks"
{:handler
(fn [{:keys [message attempt]}]
(timbre/info "Received" message)
(limericks/generate-limerick-worker db message)
{:status :success})}))))
(defmethod ig/halt-key! :worker/limerick-gen [_ worker]
#_(.stop worker))
(defmethod ig/prep-key :database.kv/connection [_ config]
(let [pass (or (env :redis-pass) "dev")]
@ -62,7 +71,7 @@
(defmethod ig/init-key :app/router [_ {:keys [db cache]}]
(let [routes [["/" {:name ::home
:get {:handler handlers/home-handler}}]
:get {:handler (handlers/home-handler db)}}]
["/{letter}.html"
{:name ::artists-by-letter
:handler (handlers/artists-by-letter db)}]

@ -2,8 +2,77 @@
(:require [hiccup.form :as form]
[hiccup.page :as page]
[clojure.string :as string]
[com.darklimericks.db.albums :as db.albums]
[com.darklimericks.db.artists :as db.artists]
[com.darklimericks.server.util :as util]))
(defn wrapper
([db opts & body]
(let [default-opts {:title "Dark Limericks"}
opts (merge default-opts opts)
title (:title opts)
num-albums (db.albums/num-albums db)
num-artists (db.artists/num-artists db)]
(page/html5
[:head
[:meta {:charset "utf-8"}]
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
(page/include-css "/assets/tachyons.css")
[:title title]
[:link {:rel "shortcut icon" :href "/assets/favicon.ico"}]]
[:body.tc.washed-yellow.bg-near-black.avenir
[:h1
[:a.link.dim.washed-yellow {:href "/"} "DarkLimericks.com"]]
[:div.w-50-ns.w-90.center.bg-dark-gray.pa2
[:div.f6.lh-copy.flex.justify-between
[:a.washed-yellow.pl1 {:href "#"} "SUBMIT LIMERICKS"]
[:span.dark-yellow
(format
"METAL LIMERICKS - CURRENTLY %d ALBUMS FROM %d+ BANDS"
num-albums
num-artists)]
[:a.washed-yellow.pr1 {:href "#"} "LINKS"]]
[:div.flex.items-stretch.bg-near-black.flex-wrap.flex-nowrap-l.f6
(let [letters (map (comp str char) (range 97 123))]
(for [letter letters]
[:a.link.washed-yellow.bg-mid-gray.pv2.w1.w-100-ns.flex-auto
{:href (format "/%s.html" letter)
:style "margin: 1px;"}
[:strong (string/upper-case letter)]]))]
[:div.flex.items-center.justify-center.pv2
[:span.f6.ph2 "Search the darkness for limericks most heartless"]
[:form.ph2
{:method "GET" :action "#"}
[:input.bg-white.w4.w5-ns
{:type "text"
:name "search"
:id "search"
:value ""}]]]
[:div.bg-near-black.br4.pa2
body]
[:div.flex.items-center.justify-center.pv2
[:span.f6.ph2 "Search the darkness for limericks most heartless"]
[:form.ph2
{:method "GET" :action "#"}
[:input.bg-white.w4.w5-ns
{:type "text"
:name "search"
:id "search"
:value ""}]]]
[:div.flex.items-stretch.bg-near-black.flex-wrap.flex-nowrap-l.f6
(let [letters (map (comp str char) (range 97 123))]
(for [letter letters]
[:a.link.washed-yellow.bg-mid-gray.pv2.w1.w-100-ns.flex-auto
{:href (format "/%s.html" letter)
:style "margin: 1px;"}
[:strong (string/upper-case letter)]]))]
[:div.f6.lh-copy.flex.justify-between
[:a.washed-yellow.pr1 {:href "#"} "SUBMIT LIMERICKS"]
[:span.dark-yellow "METAL LIMERICKS - CURRENTLY 0 ALBUMS FROM 0+ BANDS"]
[:a.washed-yellow.pl1 {:href "#"} "LINKS"]]]]))))
(defn page [title & body]
(page/html5
[:head
@ -17,21 +86,21 @@
[:a.link.dim.washed-yellow {:href "/"} "DarkLimericks.com"]]
[:div.w-50-ns.w-90.center.bg-dark-gray.pa2
[:div.f6.lh-copy.flex.justify-between
[:a.washed-yellow {:href "#"} "SUBMIT LIMERICKS"]
[:a.washed-yellow.pl1 {:href "#"} "SUBMIT LIMERICKS"]
[:span.dark-yellow "METAL LIMERICKS - CURRENTLY 0 ALBUMS FROM 0+ BANDS"]
[:a.washed-yellow {:href "#"} "LINKS"]]
[:div.flex.items-stretch.w-100.bg-near-black
[:a.washed-yellow.pr1 {:href "#"} "LINKS"]]
[:div.flex.items-stretch.bg-near-black.flex-wrap.flex-nowrap-l.f6
(let [letters (map (comp str char) (range 97 123))]
(for [letter letters]
[:a.washed-yellow.bg-mid-gray.w-100.pv2.flex-grow-1
[:a.link.washed-yellow.bg-mid-gray.pv2.w1.w-100-ns.flex-auto
{:href (format "/%s.html" letter)
:style "margin: 1px;"}
(string/upper-case letter)]))]
[:strong (string/upper-case letter)]]))]
[:div.flex.items-center.justify-center.pv2
[:span.f6.ph2 "Search the darkness for limericks heartless"]
[:span.f6.ph2 "Search the darkness for limericks most heartless"]
[:form.ph2
{:method "GET" :action "#"}
[:input.bg-white
[:input.bg-white.w4.w5-ns
{:type "text"
:name "search"
:id "search"
@ -40,10 +109,10 @@
body]
[:div.flex.items-center.justify-center.pv2
[:span.f6.ph2 "Search the darkness for limericks heartless"]
[:span.f6.ph2 "Search the darkness for limericks most heartless"]
[:form.ph2
{:method "GET" :action "#"}
[:input.bg-white
[:input.bg-white.w4.w5-ns
{:type "text"
:name "search"
:id "search"
@ -56,14 +125,50 @@
{:href (format "/%s.html" letter)
:style "margin: 1px;"}
(string/upper-case letter)]))]
[:div.f6.lh-copy.flex.justify-between
[:a.washed-yellow {:href "#"} "SUBMIT LIMERICKS"]
[:a.washed-yellow.pl1 {:href "#"} "SUBMIT LIMERICKS"]
[:span.dark-yellow "METAL LIMERICKS - CURRENTLY 0 ALBUMS FROM 0+ BANDS"]
[:a.washed-yellow {:href "#"} "LINKS"]]]]))
[:a.washed-yellow.pr1 {:href "#"} "LINKS"]]]]))
(defn home [request]
(page "Dark Limericks"
(defn home [request recent-albums artists-by-album]
(page
"Dark Limericks"
[:div.f3.light-yellow.pb3
"Welcome to DARK LIMERICKS !"]
[:div.f6.washed-yellow.pb3
"Dark Limericks is the largest metal limericks archive on the Web."]
[:div.f6.washed-yellow.pb3
"( if you're looking for Dark Lyrics, go "
[:a.light-yellow {:href "http://darklyrics.com"} "here"]
" )"]
[:div.f4.light-yellow.pb3
"NEW ALBUMS"]
[:div.flex.flex-wrap.space-between
(for [album recent-albums]
[:div.pb4
{:style "flex: 1 24%"}
[:div.p1
[:img {:src (format
"/assets/images/%s-128.png"
(-> album
:album/name
string/lower-case
(string/replace #" " "-")))}]]
[:div.p2
[:a.light-yellow.f5
{:href (util/route-name->path
request
:com.darklimericks.server.system/artist
{:artist-id (:album/artist_id album)})}
(-> album :album/id (artists-by-album) :artist/name)]]
[:div.p2
[:a.link.washed-yellow.f6
{:href (util/route-name->path
request
:com.darklimericks.server.system/album
{:artist-id (:album/artist_id album)
:album-id (:album/id album)})}
(format "\"%s\"" (:album/name album))]]])]
(form/form-to
[:post (util/route-name->path request :com.darklimericks.server.system/limerick-generation-task)]
(form/text-field "scheme")

@ -0,0 +1,122 @@
(ns com.darklimericks.util.identicon
(:require [digest]
[clojure.string :as str])
(:import java.io.File
java.awt.Color
java.awt.image.BufferedImage
javax.imageio.ImageIO))
(def tiles-per-side 6)
(def total-tiles
(* tiles-per-side tiles-per-side))
(defn- get-color
[pos]
(nth '(
(3 7 30) ;; Darkness
(55 6 23) ;; Deep Red
(106 4 15) ;; Rosewood
(157 2 8) ;; Dark Red
(208 0 0) ;; Red
(220 47 2) ;; Vermillion
(232 93 4) ;; Persimmon
(244 140 6) ;; Carrot
(3 7 30) ;; Darkness
(55 6 23) ;; Deep Red
(106 4 15) ;; Rosewood
(157 2 8) ;; Dark Red
(208 0 0) ;; Red
(220 47 2) ;; Vermillion
(232 93 4) ;; Persimmon
(244 140 6) ;; Carrot
) pos))
(defn- from-hex-to-dec
"Convert a hex char to an int."
[character]
(cond
(= "a" character) 10
(= "b" character) 11
(= "c" character) 12
(= "d" character) 13
(= "e" character) 14
(= "f" character) 15
:else (Integer/parseInt character)))
(defn- to-numbers
"Convert a string of hex chars to a seq of ints."
[num_string]
(map from-hex-to-dec (rest (clojure.string/split num_string #""))))
(defn- to-bools
"Formulaically convert a seq of ints to bools."
[num_string]
(take (/ total-tiles 2) (cycle
(map #(> % 7) (to-numbers num_string)))))
(defn- in-row
"Return the row for a particular position in the seq."
[pos]
(quot pos (/ tiles-per-side 2)))
(defn- in-col
"Return the column for a particular position in the seq."
[pos]
(rem pos (/ tiles-per-side 2)))
(defn- draw-tile
"Fill in a tile at a particular position starting from the left of the image."
[draw tile-size pos]
(.fillRect draw
(* (in-col pos) tile-size)
(* (in-row pos) tile-size)
tile-size tile-size))
(defn- draw-mirror-tile
"Fill in a tile at a particular position starting from the right of the image."
[draw tile-size pos]
(.fillRect draw
(* (- tiles-per-side (in-col pos) 1) tile-size)
(* (in-row pos) tile-size)
tile-size tile-size))
(defn- draw-it
"Draw tiles on the image based on a seq of booleans to determine whether the tile is filled in or not."
[draw tile-size pos bools]
(when (first bools)
;; left half
(draw-tile draw tile-size pos)
;; right half
(draw-mirror-tile draw tile-size pos))
(when (> (count bools) 1)
(draw-it draw tile-size (inc pos) (rest bools))))
(defn- file-name
"Generate a file name."
[name size]
(format "resources/public/images/%s-%s.png" name size))
(defn- fill-background
"Fill in the background with white."
[draw size]
(.setColor draw (Color/BLACK))
(.fillRect draw 0 0 size size))
(defn generate
"Make a new avatar."
[identifier size]
(let
[tile-size (quot size tiles-per-side)
md5 (digest/md5 identifier)
icon (BufferedImage. size size BufferedImage/TYPE_INT_RGB)
[r g b] (get-color (first (to-numbers md5)))
color (Color. r g b)
draw (.createGraphics icon)]
(fill-background draw size)
(.setColor draw color)
(draw-it draw tile-size 0 (to-bools md5))
(ImageIO/write icon "png" (File. (file-name identifier size)))))
(comment
(generate "foobar-bazz" 128))
Loading…
Cancel
Save