Created
April 17, 2015 12:05
-
-
Save edbond/40925895e72f9e4a1447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns kittens.core | |
(:require [environ.core :refer (env)] | |
[clj-http.client :as http] | |
[clojure.java.io :as io] | |
[clojure.core.async :as async :refer (go go-loop <!)])) | |
(def api-key (-> env :dev :flickr :api-key)) | |
(def rest-api-url "https://api.flickr.com/services/rest/") | |
(def folder "photos") | |
(defn photo-map->url [m] | |
"https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg | |
b - large, 1024 on longest side | |
k - large 2048, 2048 on longest side | |
" | |
(str "https://farm" (:farm m) ".staticflickr.com/" | |
(:server m) "/" (:id m) "_" (:secret m) "_b.jpg")) | |
(defn search | |
([query] (search query 1 500)) | |
([query page per-page] | |
(-> (http/get rest-api-url | |
{:as :json | |
:query-params | |
{:method "flickr.photos.search" | |
:api_key api-key | |
:text query | |
:format "json" | |
:sort "interestingness-desc" | |
:nojsoncallback 1 | |
:content_type 1 | |
:page page | |
:extras "original_format" | |
:per_page per-page | |
:license "1,2,3"}}) | |
:body :photos :photo))) | |
;; https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=86d&text=kittens&license=&format=json&nojsoncallback=1 | |
(defn store | |
"Store photo unless already exists" | |
[m] | |
(let [fn (str folder "/" (:id m) ".jpg")] | |
(when-not (.exists (io/as-file fn)) | |
(let [url (photo-map->url m) | |
response (http/get url {:as :byte-array | |
:follow-redirects false})] | |
(when (= (:status response) 200) | |
(with-open [o (io/output-stream fn)] | |
(.write o | |
(:body response)))))))) | |
(defonce kittens (search "kittens")) | |
(defn writer | |
[c] | |
(go-loop [] | |
(when-let [p (<! c)] | |
(store p) | |
(recur)))) | |
(defn run [] | |
(let [n 100 | |
c (async/chan n) | |
ws (dotimes [_ n] | |
(writer c)) | |
c2 (async/onto-chan c kittens)] | |
(time | |
(async/<!! c2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment