aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Kavanagh <rak@rak.ac>2021-10-26 23:02:08 -0400
committerRyan Kavanagh <rak@rak.ac>2021-10-26 23:03:41 -0400
commit2157ffb8298afe8a5d1d0bdf71d2ad256c90fceb (patch)
tree260d07c28803d55a82cad5b1f71773188358ac6c
parentInitial import (diff)
Initial json parsing and validation
-rw-r--r--dune-project14
-rw-r--r--lib/dune16
-rw-r--r--lib/listenbrainz.atd58
-rw-r--r--lib/listenbrainz_util.ml9
4 files changed, 97 insertions, 0 deletions
diff --git a/dune-project b/dune-project
new file mode 100644
index 0000000..51a3105
--- /dev/null
+++ b/dune-project
@@ -0,0 +1,14 @@
+(lang dune 2.5)
+(name ocamllb)
+
+(generate_opam_files true)
+
+(license BSD-3-Clause)
+(authors "Ryan Kavanagh <rak@rak.ac>")
+
+(package
+ (name ocamllb)
+ (synopsis "ListenBrainz library")
+ (depends
+ (atdgen (>= 2.0.0))
+ (yojson (>= 1.5.0))))
diff --git a/lib/dune b/lib/dune
new file mode 100644
index 0000000..2f9cfb7
--- /dev/null
+++ b/lib/dune
@@ -0,0 +1,16 @@
+(library
+ (name ocamllb)
+ (public_name ocamllb)
+ (libraries atdgen))
+
+(rule
+ (targets listenbrainz_j.ml
+ listenbrainz_j.mli)
+ (deps listenbrainz.atd)
+ (action (run atdgen -j -j-std %{deps})))
+
+(rule
+ (targets listenbrainz_t.ml
+ listenbrainz_t.mli)
+ (deps listenbrainz.atd)
+ (action (run atdgen -t %{deps})))
diff --git a/lib/listenbrainz.atd b/lib/listenbrainz.atd
new file mode 100644
index 0000000..d45136d
--- /dev/null
+++ b/lib/listenbrainz.atd
@@ -0,0 +1,58 @@
+type mbid = string
+type isrc = string
+
+type additional_info = {
+ ?artist_mbids : mbid list option;
+ ?release_group_mbid : mbid list option;
+ ?release_mid : mbid option;
+ ?recording_mbid : mbid option;
+ ?track_mbid : mbid option;
+ ?work_mbids : mbid list option;
+ ?tracknumber : int option;
+ ?isrc : isrc option;
+ ?spotify_id : string option;
+ ?tags : string list option;
+ ?listening_from : string option;
+}
+
+type track_metadata = {
+ artist_name : string;
+ track_name : string;
+ ?listened_at : int option;
+ ?additional_info : additional_info option;
+ ?release_name : string option;
+}
+
+type listen_type = [
+ Single <json name="single">
+ | Playing_now <json name="playing_now">
+ | Import <json name="import">
+]
+
+type listen_payload = {
+ listened_at : int;
+ track_metadata : track_metadata;
+}
+
+type submission = {
+ listen_type : listen_type;
+ payload : listen_payload list;
+} <ocaml valid="Listenbrainz_util.validate_submission">
+
+type response = {
+ count : int;
+ user_id : mbid;
+ listens : listen_payload list;
+} <ocaml valid="Listenbrainz_util.validate_response">
+
+type validate_token = {
+ code : int;
+ message : string;
+ valid : bool;
+ user: string;
+}
+
+type delete_listen = {
+ listened_at : int;
+ recording_msid : string;
+}
diff --git a/lib/listenbrainz_util.ml b/lib/listenbrainz_util.ml
new file mode 100644
index 0000000..35a7af8
--- /dev/null
+++ b/lib/listenbrainz_util.ml
@@ -0,0 +1,9 @@
+open Listenbrainz_t
+
+let validate_submission s =
+ match s.listen_type with
+ | `Single -> List.length s.payload = 1
+ | _ -> true
+
+let validate_response r =
+ List.length r.listens = r.count