Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions otus-18/src/otus_18/homework/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(ns otus-18.homework.core
(:require [clojure.core.async :as a :refer [<! <!! go]]
[otus-18.homework.requests :as r]))

(def pokemons-seq (r/pokemons-seq))
(def types (r/pokemon-types-seq))

(defn extract-pokemon-name [pokemon]
(:name pokemon))

(defn extract-pokemon-types-url [pokemon]
(let [p (r/fetch-pokemon (:url pokemon))]
(map #(get-in % [:type :url]) (:types p))))


(defn extract-type-name [pokemon-type lang]
(->> (:names pokemon-type)
(filter (fn [type-name] (= lang (-> type-name :language :name))))
(first)
:name))

(defn process-type [url lang]
(extract-type-name (r/fetch-type url) lang))

(defn process-pokemon [pokemon lang]
(let [pokemon-name (extract-pokemon-name pokemon)
type-urls (extract-pokemon-types-url pokemon)
types (map #(process-type % lang) type-urls)]
(println types)
{:name pokemon-name
:types types}))


(defn get-pokemons [limit lang]
(let [pokemons (take limit pokemons-seq)]
(map #(process-pokemon % lang) pokemons)))

(comment
(take 10 types)
(get-pokemons 4 "en")
(take 2 pokemons-seq)
)


;;; async solution

(defn process-pokemon-async [pokemon lang]
(let [pokemon-name (extract-pokemon-name pokemon)
type-urls (extract-pokemon-types-url pokemon)
type-channels (mapv #(go (process-type % lang)) type-urls)
types-chan (a/into [] (a/merge type-channels))]
(go
(let [types (<! types-chan)]
{:name pokemon-name
:types types}))))

(defn get-pokemons-async [limit lang]
(let [pokemons-ch (go (take limit pokemons-seq))]
(go
(let [pokemos (<! pokemons-ch)
processed-ch (mapv #(process-pokemon-async % lang) pokemos)]
(<! (a/into [] (a/merge processed-ch)))))))

(comment
(let [p (first pokemons-seq)]
(<!! (process-pokemon-async p "en")))

(<!! (get-pokemons-async 10 "en")))

(comment
(retrieve-pokemons! 4 "en")

(let [c (go
(process-type "https://pokeapi.co/api/v2/type/5/" "en"))]
(<!! c))

)
22 changes: 7 additions & 15 deletions otus-18/src/otus_18/homework/pokemons.clj
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
(ns otus-18.homework.pokemons)

(def base-url "https://pokeapi.co/api/v2")
(def pokemons-url (str base-url "/pokemon"))
(def type-path (str base-url "/type"))

(defn extract-pokemon-name [pokemon]
(:name pokemon))

(defn extract-type-name [pokemon-type lang]
(->> (:names pokemon-type)
(filter (fn [type-name] (= lang (-> type-name :language :name))))
(first)
:name))
(ns otus-18.homework.pokemons
(:require [clojure.core.async :as a :refer [<!!]]
[otus-18.homework.core :as c]))

(defn get-pokemons
"Асинхронно запрашивает список покемонов и название типов в заданном языке. Возвращает map, где ключами являются
имена покемонов (на английском английский), а значения - коллекция названий типов на заданном языке."
[& {:keys [limit lang] :or {limit 50 lang "ja"}}])
[& {:keys [limit lang] :or {limit 50 lang "ja"}}]
(println (<!! (c/get-pokemons-async limit lang))))

(comment (println (get-pokemons)))
83 changes: 83 additions & 0 deletions otus-18/src/otus_18/homework/requests.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(ns otus-18.homework.requests
(:require [clj-http.client :as c]
[clojure.core.memoize :as memo]))

(def ^:private base-url "https://pokeapi.co/api/v2")
(def ^:private pokemons-url (str base-url "/pokemon"))
(def ^:private type-path (str base-url "/type"))

(defn- fetch-page
[url]
(println (str "Fetching: " url))
(try
(let [response (c/get url {:as :json})
{:keys [count next previous results]} (:body response)]
{:url url
:count count
:next next
:previous previous
:results results})
(catch Exception e
(throw (ex-info "Failed to fetch page"
{:url url
:error (.getMessage e)})))))

(defn- fetch-item [url]
(println (str "Fetching: " url))
(try
(:body (c/get url {:as :json}))
(catch Exception e
(throw (ex-info "Failed to fetch item"
{:url url
:error (.getMessage e)})))))
(def fetch-item-memoized
"Memoized version of fetch-item with time-based caching."
(memo/ttl fetch-item
:ttl/threshold 3600000))


(def fetch-page-memoized
"Memoized version of fetch-page with time-based caching."
(memo/ttl fetch-page
:ttl/threshold 3600000)) ; 1-hour cache in milliseconds

(defn fetch-pokemon [url]
(fetch-item-memoized url))

(defn fetch-type [url]
(fetch-item-memoized url))

(defn pokemon-types-seq
([]
(pokemon-types-seq type-path))
([initial-url]
(let [page (fetch-page-memoized initial-url)]
(lazy-seq
(concat
(:results page)
(when-let [next-url (:next page)]
(pokemon-types-seq next-url)))))))

(defn pokemons-seq ([]
(pokemons-seq pokemons-url))
([initial-url]
(let [page (fetch-page-memoized initial-url)]
(lazy-seq
(concat
(:results page)
(when-let [next-url (:next page)]
(pokemons-seq next-url)))))))

(comment
(filter #(= "shadow" (:name %)) (take 50 (pokemon-types-seq)))

(take 1 (pokemons-seq))

(:types (fetch-pokemon "https://pokeapi.co/api/v2/pokemon/1/"))

(:names (fetch-item-memoized "https://pokeapi.co/api/v2/type/4/"))

(:names (:body (c/get "https://pokeapi.co/api/v2/type/10002/" {:as :json})))

(fetch-pokemon "https://pokeapi.co/api/v2/pokemon/44/")
)