Line data Source code
1 4 : (ns matchscore.domain.event.source-link
2 : "Where an imported event comes from: the original tournament page on the
3 : source site, plus the PSA tournament id (for its match videos)."
4 : (:require
5 : [clojure.string :as str]
6 : [honey.sql :as sql]
7 : [next.jdbc :as jdbc]
8 : [next.jdbc.result-set :as rs]))
9 :
10 :
11 4 : (def ^:private mirror-tables
12 : "Mirror table holding the source tournament (with `url` and `event_id`)."
13 20 : {"psa" :psa_tournaments
14 : "esf" :esf_tournaments})
15 :
16 :
17 4 : (defn- parse-uuid*
18 : [id]
19 5 : (cond (uuid? id) id
20 6 : (string? id) (parse-uuid id)))
21 :
22 :
23 4 : (defn- rankedin-url
24 : [rankedin-id]
25 0 : (when (and (integer? rankedin-id) (pos? rankedin-id))
26 0 : (str "https://rankedin.com/en/tournament/" rankedin-id "/")))
27 :
28 :
29 4 : (defn source-url
30 : "URL of the original tournament for an imported event, or nil (native
31 : events, or no source row/url)."
32 : [ds {:keys [uuid source rankedin-id]}]
33 9 : (let [source (some->
34 1 : source
35 1 : name)]
36 4 : (if (= "rankedin" source)
37 0 : (rankedin-url rankedin-id)
38 13 : (when-let [table (and ds (get mirror-tables source))]
39 9 : (when-let [event-id (parse-uuid* uuid)]
40 1 : (let [url
41 2 : (:url
42 2 : (jdbc/execute-one!
43 1 : ds
44 2 : (sql/format
45 9 : {:select [:url]
46 2 : :from [table]
47 10 : :where [:and [:= :event_id event-id] [:<> :url nil]]
48 4 : :order-by [[:url :asc]]
49 : :limit 1})
50 3 : {:builder-fn rs/as-unqualified-kebab-maps}))]
51 6 : (when-not (str/blank? url) url)))))))
52 :
53 :
54 4 : (defn psa-id
55 : "PSA tournament id of a PSA event, or nil."
56 : [ds {:keys [uuid source]}]
57 2 : (when
58 5 : (and
59 1 : ds
60 : (=
61 : "psa"
62 8 : (some->
63 1 : source
64 1 : name)))
65 9 : (when-let [event-id (parse-uuid* uuid)]
66 2 : (:psa-id
67 2 : (jdbc/execute-one!
68 1 : ds
69 2 : (sql/format
70 9 : {:select [:psa_id]
71 2 : :from [:psa_tournaments]
72 4 : :where [:= :event_id event-id]
73 4 : :order-by [[:psa_id :asc]]
74 : :limit 1})
75 3 : {:builder-fn rs/as-unqualified-kebab-maps})))))
|