broadcast.ml 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. type timestamp = Ptime.t * int
  2. type t = {
  3. author : string;
  4. description : string;
  5. identifier : string;
  6. image : string;
  7. language : string;
  8. modified : timestamp;
  9. source : string;
  10. subject : string;
  11. timeend : timestamp;
  12. timestart : timestamp;
  13. title : string;
  14. title_episode : string;
  15. title_series : string;
  16. }
  17. let empty : t =
  18. let nul = "" and zero = (Ptime.min, 0) in
  19. {
  20. author = nul;
  21. description = nul;
  22. identifier = nul;
  23. image = nul;
  24. language = nul;
  25. modified = zero;
  26. source = nul;
  27. subject = nul;
  28. timeend = zero;
  29. timestart = zero;
  30. title = nul;
  31. title_episode = nul;
  32. title_series = nul;
  33. }
  34. let to_signals bc =
  35. let dur_iso ((t0, z0), (t1, z1)) =
  36. let s_per_m = 60 in
  37. "PT"
  38. ^ (z0 - z1
  39. + ((Ptime.diff t1 t0 |> Ptime.Span.to_int_s |> Option.get) / s_per_m)
  40. |> Int.to_string)
  41. ^ "M"
  42. and to_rfc3339 (t, tz_s) = t |> Ptime.to_rfc3339 ~tz_offset_s:tz_s
  43. and lf l = l |> List.cons (`Text [ "\n" ]) in
  44. let meta show k v l =
  45. match show with
  46. | false -> l
  47. | true ->
  48. l |> lf
  49. |> List.cons (`Text [ " " ])
  50. |> List.cons
  51. (`Start_element
  52. ( ("", "meta"),
  53. [ (("", "content"), v); (("", "name"), "DC." ^ k) ] ))
  54. |> List.cons `End_element
  55. in
  56. []
  57. |> List.cons
  58. (`Comment
  59. " unorthodox relative namespace to enable \
  60. http://www.w3.org/TR/grddl-tests/#sq2 without a central server ")
  61. |> lf
  62. |> List.cons
  63. (`Start_element
  64. ( ("", "broadcast"),
  65. [
  66. (("", "xml:lang"), bc.language);
  67. (("", "xmlns"), "../../../../../assets/2013/radio-pi.rdf");
  68. (("", "modified"), bc.modified |> to_rfc3339);
  69. ] ))
  70. |> meta true "identifier" bc.identifier
  71. |> meta true "scheme" "/app/pbmi2003-recmod2012/"
  72. |> meta true "language" bc.language
  73. |> meta true "title" bc.title
  74. |> meta (not (String.equal "" bc.title_series)) "title.series" bc.title_series
  75. |> meta
  76. (not (String.equal "" bc.title_episode))
  77. "title.episode" bc.title_episode
  78. |> meta true "subject" bc.subject
  79. |> meta true "format.timestart" (bc.timestart |> to_rfc3339)
  80. |> meta true "format.timeend" (bc.timeend |> to_rfc3339)
  81. |> meta true "format.duration" ((bc.timestart, bc.timeend) |> dur_iso)
  82. |> meta true "image" bc.image
  83. |> meta true "description" bc.description
  84. |> meta true "author" bc.author
  85. |> meta false "creator" "-" |> meta false "publisher" "-"
  86. |> meta true "source" bc.source
  87. |> lf
  88. |> List.cons `End_element
  89. |> lf |> List.rev
  90. let to_xml dst bc =
  91. bc |> to_signals |> Markup.of_list |> Markup.write_xml
  92. |> Markup.to_channel dst;
  93. 0