<commit_before>(ns paulkrake.datacenter
  (:require [clojure.data.json :as json]))

(defn to-clj-unmemoized [uri]
  (->> uri slurp json/read-str ))

(def to-clj (memoize to-clj-unmemoized))

(defn competitions []
  (as-> "http://sportsapi.sport1.de/competition/co12" x
        (to-clj x)))

(defn team-stats-by-match [match-id team-id]
  (as-> "http://sportsapi.sport1.de/team-stats-by-match/ma%s/te%s" x
        (format x match-id team-id)
        (to-clj x)
        (first x)))

(defn season-by-competition [saison]
  (let [name (format "20%s/20%s" 
                (apply str (take 2 (str saison)))
                (apply str (drop 2 (str saison))))]
    (as-> "http://sportsapi.sport1.de/seasons-by-competition/co12" x
          (to-clj x)
          (get x "season")
          (filter #(= name (get % "name")) x)
          (first x))))

(defn rounds-by-season [saison]
  (as-> (format "http://sportsapi.sport1.de/rounds-by-season/se%s" (str saison)) x
        (to-clj x)))

(defn matches-by-season
  ([]
     (to-clj "http://sportsapi.sport1.de/matches-by-season/co12"))

  ([saison-id]
      (as-> (format "http://sportsapi.sport1.de/matches-by-season/co12/se%s" (str saison-id)) x
            (to-clj x)))

  ([saison-id spieltag]
     (as-> (format "http://sportsapi.sport1.de/matches-by-season/co12/se%s/md%s"
                   (str saison-id)
                   (str spieltag)) x
            (to-clj x))))

(defn to-num [x]
  (cond (number? x) x
        (= "-" x)   x
        true        (Integer/parseInt x)))

(defn split-ergebnis [ergebnis]
  (->> (clojure.string/split ergebnis #":")
       (map to-num)))

(defn spieltag [saison spieltag-nr]
  (as-> (season-by-competition saison) x
        (get x "id")
        (matches-by-season x spieltag-nr)
        (get x "round")
        (first x)
        (get x "match")
        (map (fn [m] (let [home     (get-in m ["home" "name"])
                          away     (get-in m ["away" "name"])
                          finished (= "yes" (get m "finished"))
                          hg       (if finished (as-> m y
                                                      (get y "match_result")
                                                      (filter #(and (= "home" (get % "place"))
                                                                    (= "0" (get % "match_result_at"))) y)
                                                      (map #(get % "match_result") y)
                                                      (first y)))
                          ag       (if finished (as-> m y
                                                      (get y "match_result")
                                                      (filter #(and (= "away" (get % "place"))
                                                                    (= "0" (get % "match_result_at"))) y)
                                                      (map #(get % "match_result") y)
                                                      (first y)))]
                      (vector home away hg ag)))
             x)))

(defn map-inverse [m]
  (as-> m x
        (map (fn [[k v]] [v k]) x)
        (into {} x)))

(defn data [saison spieltag-nr kategorie]
  (as-> (season-by-competition saison) x
        (get x "id")
        (matches-by-season x spieltag-nr)
        (get x "round")
        (first x)
        (get x "match")
        (map (fn [m] (let [home     (get-in m ["home" "name"])
                          away     (get-in m ["away" "name"])
                          home-id  (get-in m ["home" "id"]) 
                          away-id  (get-in m ["away" "id"])
                          match-id (get m "id")
                          hg       (-> (team-stats-by-match match-id home-id)
                                       (get kategorie))
                          ag       (-> (team-stats-by-match match-id away-id)
                                       (get kategorie))]
                      (vector home away hg ag)))
             x)))

(defn shots-on-goal [saison spieltag]
  (data saison spieltag "shots"))

(defn tore [saison spieltag]
  (data saison spieltag "tore"))

(defn gegentore [saison spieltag]
  (data saison spieltag "gegentore"))
<commit_msg>unify calls to sportsapi<commit_after>(ns paulkrake.datacenter
  (:require [clojure.data.json :as json]))

(defn to-clj-unmemoized [uri]
  (->> uri slurp json/read-str ))

(def to-clj (memoize to-clj-unmemoized))

(defn competitions []
  (as-> "http://sportsapi.sport1.de/competition/co12" x
        (to-clj x)))

(defn team-stats-by-match [match-id team-id]
  (as-> "http://sportsapi.sport1.de/team-stats-by-match/ma%s/te%s" x
        (format x match-id team-id)
        (to-clj x)
        (first x)))

(defn season-by-competition [saison]
  (let [name (format "20%s/20%s" 
                (apply str (take 2 (str saison)))
                (apply str (drop 2 (str saison))))]
    (as-> "http://sportsapi.sport1.de/seasons-by-competition/co12" x
          (to-clj x)
          (get x "season")
          (filter #(= name (get % "name")) x)
          (first x))))

(defn rounds-by-season [saison]
  (as-> (format "http://sportsapi.sport1.de/rounds-by-season/se%s" (str saison)) x
        (to-clj x)))

(defn matches-by-season
  ([]
     (to-clj "http://sportsapi.sport1.de/matches-by-season/co12"))

  ([saison-id]
      (as-> (format "http://sportsapi.sport1.de/matches-by-season/co12/se%s" (str saison-id)) x
            (to-clj x)))

  ([saison-id spieltag]
     (as-> (format "http://sportsapi.sport1.de/matches-by-season/co12/se%s/md%s"
                   (str saison-id)
                   (str spieltag)) x
            (to-clj x))))

(defn to-num [x]
  (cond (number? x) x
        (= "-" x)   x
        true        (Integer/parseInt x)))

(defn split-ergebnis [ergebnis]
  (->> (clojure.string/split ergebnis #":")
       (map to-num)))

(defn map-inverse [m]
  (as-> m x
        (map (fn [[k v]] [v k]) x)
        (into {} x)))

(defn data [saison spieltag-nr kategorie]
  (as-> (season-by-competition saison) x
        (get x "id")
        (matches-by-season x spieltag-nr)
        (get x "round")
        (first x)
        (get x "match")
        (map (fn [m] (let [home     (get-in m ["home" "name"])
                          away     (get-in m ["away" "name"])
                          home-id  (get-in m ["home" "id"]) 
                          away-id  (get-in m ["away" "id"])
                          match-id (get m "id")
                          hg       (as-> (team-stats-by-match match-id home-id) y
                                     (get y kategorie)
                                     (Double/valueOf y))
                          ag       (as-> (team-stats-by-match match-id away-id) y
                                     (get y kategorie)
                                     (Double/valueOf y))]
                      (vector home away hg ag)))
             x)))

(defn spieltag [saison spieltag-nr]
  (data saison spieltag-nr "score"))

(defn score [saison spieltag-nr]
  (data saison spieltag-nr "score"))

(defn shots-on-goal [saison spieltag-nr]
  (data saison spieltag-nr "shots"))

(defn passes-complete-percentage [saison spieltag-nr]
  (data saison spieltag-nr "passes_complete_percentage"))


{'fraction_non_alphanumeric': 0.15568119139547712, 'fraction_numerical': 0.006618863761720904, 'mean_word_length': 1.3882120513664802, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1050150', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2431, 'n_words': 586}