OCaml library for JSONfeed parsing and creation
1(** Attachments for JSON Feed items. *)
2
3type t = {
4 url : string;
5 mime_type : string;
6 title : string option;
7 size_in_bytes : int64 option;
8 duration_in_seconds : int option;
9}
10
11let create ~url ~mime_type ?title ?size_in_bytes ?duration_in_seconds () =
12 { url; mime_type; title; size_in_bytes; duration_in_seconds }
13
14let url t = t.url
15let mime_type t = t.mime_type
16let title t = t.title
17let size_in_bytes t = t.size_in_bytes
18let duration_in_seconds t = t.duration_in_seconds
19
20let equal a b =
21 a.url = b.url &&
22 a.mime_type = b.mime_type &&
23 a.title = b.title &&
24 a.size_in_bytes = b.size_in_bytes &&
25 a.duration_in_seconds = b.duration_in_seconds
26
27let pp ppf t =
28 (* Extract filename from URL *)
29 let filename =
30 try
31 let parts = String.split_on_char '/' t.url in
32 List.nth parts (List.length parts - 1)
33 with _ -> t.url
34 in
35
36 Format.fprintf ppf "%s (%s" filename t.mime_type;
37
38 (match t.size_in_bytes with
39 | Some size ->
40 let mb = Int64.to_float size /. (1024. *. 1024.) in
41 Format.fprintf ppf ", %.1f MB" mb
42 | None -> ());
43
44 (match t.duration_in_seconds with
45 | Some duration ->
46 let mins = duration / 60 in
47 let secs = duration mod 60 in
48 Format.fprintf ppf ", %dm%ds" mins secs
49 | None -> ());
50
51 Format.fprintf ppf ")"