diff --git a/src/clj/pmagnus/elprice/web/controllers/prices.clj b/src/clj/pmagnus/elprice/web/controllers/prices.clj index b4d193f..6261d04 100644 --- a/src/clj/pmagnus/elprice/web/controllers/prices.clj +++ b/src/clj/pmagnus/elprice/web/controllers/prices.clj @@ -15,11 +15,11 @@ (defn- build-url "Build the Energi Data Service API URL for day-ahead prices." - [start-date end-date] + [start-date end-date price-area] (str "https://api.energidataservice.dk/dataset/DayAheadPrices" "?start=" start-date "&end=" end-date - "&filter=" (URLEncoder/encode (json/write-str {"PriceArea" ["DK1" "DK2"]}) "UTF-8") + "&filter=" (URLEncoder/encode (json/write-str {"PriceArea" [price-area]}) "UTF-8") "&sort=TimeDK%20asc" "&limit=200")) @@ -36,19 +36,23 @@ (when (= 200 (.statusCode response)) (json/read-str (.body response) :key-fn keyword)))) -(defn fetch-prices-from-api - "Fetch today's and tomorrow's prices from Energi Data Service." - [] - (let [today (LocalDate/now dk-zone) - tomorrow (.plusDays today 1) - ;; Fetch from start of today to end of tomorrow - start (.format today date-fmt) - end (.format (.plusDays tomorrow 1) date-fmt) - url (build-url start end)] - (log/info "Fetching day-ahead prices from" url) +(defn- fetch-area + "Fetch today's and tomorrow's prices for a single price area." + [start end price-area] + (let [url (build-url start end price-area)] + (log/info "Fetching day-ahead prices for" price-area "from" url) (when-let [data (fetch-json url)] (:records data)))) +(defn fetch-prices-from-api + "Fetch today's and tomorrow's prices from Energi Data Service for DK1 and DK2." + [] + (let [today (LocalDate/now dk-zone) + start (.format today date-fmt) + end (.format (.plusDays today 2) date-fmt)] + (concat (fetch-area start end "DK1") + (fetch-area start end "DK2")))) + (defn- parse-timestamp "Parse an ISO timestamp string to java.sql.Timestamp." [s]