diff options
| -rw-r--r-- | lib/listenbrainz.atd | 68 | ||||
| -rw-r--r-- | lib/listenbrainz_util.ml | 27 | 
2 files changed, 76 insertions, 19 deletions
| diff --git a/lib/listenbrainz.atd b/lib/listenbrainz.atd index d45136d..cf4e09a 100644 --- a/lib/listenbrainz.atd +++ b/lib/listenbrainz.atd @@ -1,5 +1,7 @@ -type mbid = string +type mbid = string <ocaml valid="Listenbrainz_util.validate_mbid">  type isrc = string +type unixtime = float <json repr="int"> +type tm = unixtime wrap <ocaml module="Unix" t="Unix.tm" wrap="Unix.gmtime" unwrap="Listenbrainz_util.tm_unwrap">  type additional_info = {    ?artist_mbids : mbid list option; @@ -29,30 +31,62 @@ type listen_type = [    | Import <json name="import">  ] -type listen_payload = { -  listened_at : int; -  track_metadata : track_metadata; +type similar_user = { +  user_name : string; +  similarity : float;  } -type submission = { -  listen_type : listen_type; -  payload : listen_payload list; -} <ocaml valid="Listenbrainz_util.validate_submission"> +type payload_listen = { +  ?listened_at : tm option; +  track_metadata : track_metadata; +} -type response = { +type payload_listen_count = {    count : int; -  user_id : mbid; -  listens : listen_payload list; -} <ocaml valid="Listenbrainz_util.validate_response"> +} -type validate_token = { +type post_submit_listens = { +  listen_type : listen_type; +  payload : payload_listen list; +} <ocaml valid="Listenbrainz_util.validate_post_submit_listens"> + +type get_validate_token = {    code : int;    message : string;    valid : bool; -  user: string; +  ?user: string option; +} <ocaml valid="Listenbrainz_util.validate_get_validate_token"> + +type post_delete_listen = { +  listened_at : tm; +  recording_msid : mbid; +} + +type get_user_similar_users = similar_user list + +type get_user_listen_count = { +  payload : payload_listen_count; +} + +type get_user_playing_now = { +  count : int; +  user_id : mbid; +  listens : payload_listen list; +} <ocaml valid="Listenbrainz_util.validate_get_user_playing_now"> + +type get_user_similar_to = similar_user + +type get_user_listens = { +  count : int; +  user_id : mbid; +  listens : payload_listen list; +} <ocaml valid="Listenbrainz_util.validate_get_user_listens"> + +type get_latest_import = { +  musicbrainz_id : mbid; +  latest_import : tm;  } -type delete_listen = { -  listened_at : int; -  recording_msid : string; +type post_latest_import = { +  ts : tm;  } diff --git a/lib/listenbrainz_util.ml b/lib/listenbrainz_util.ml index 35a7af8..5056c09 100644 --- a/lib/listenbrainz_util.ml +++ b/lib/listenbrainz_util.ml @@ -1,9 +1,32 @@  open Listenbrainz_t -let validate_submission s = +let uuid_v4_re = +  (* UUIDs are 8-4-4-4-12 hex digits. +     * The third block of version 4 UUIDS starts with a 4. *) +  let four_hex = "[0-9a-f][0-9a-f][0-9a-f][0-9a-f]" +  in Str.regexp_case_fold ("^" ^ four_hex ^ four_hex +                           ^ "-" ^ four_hex +                           ^ "-4[0-9a-f][0-9a-f][0-9a-f]" +                           ^ "-" ^ four_hex +                           ^ "-" ^ four_hex ^ four_hex ^ four_hex ^ "$") +let tm_unwrap tm = +  let (f, _) = Unix.mktime tm in f + +let validate_mbid mbid = Str.string_match uuid_v4_re mbid 0 + +let validate_post_submit_listens s =    match s.listen_type with    | `Single -> List.length s.payload = 1    | _ -> true -let validate_response r = +let validate_get_validate_token r = +  r.code = 200 || r.code = 400 + +let validate_get_user_playing_now r = +  match r.listens with +  | [] -> true +  | [l] -> l.listened_at = None +  | _ -> false + +let validate_get_user_listens r =    List.length r.listens = r.count | 
