-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiMedia.ml
203 lines (181 loc) · 5.65 KB
/
apiMedia.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(* ************************************************************************** *)
(* Project: Life - the game, Official OCaml SDK *)
(* Author: db0 ([email protected], http://db0.fr/) *)
(* Latest Version is on GitHub: https://github.com/Life-the-game/SDK-OCaml *)
(* ************************************************************************** *)
open Yojson.Basic.Util
open ApiTypes
(* ************************************************************************** *)
(* Tools *)
(* ************************************************************************** *)
(* todo check what happens if the exetions are raised *)
let extension filename =
let start = try (String.rindex filename '.') + 1 with Not_found -> 0
in try String.sub filename start ((String.length filename) - start)
with Invalid_argument s -> ""
let extension_of_path path =
try extension (List.hd (List.rev path))
with Failure _ -> ""
let checker l contenttype = List.exists ((=) contenttype) l
let guess_contenttype_from_extension extension =
match String.lowercase extension with
| "jpg" | "jpeg" | "jpe" -> "image/jpeg"
| "png" -> "image/png"
| "bmp" -> "image/bmp"
| "gif" -> "image/gif"
| "mp4" | "mp4v" | "mpg4" -> "video/mp4"
| "mpeg" | "mpg" | "mpe" | "m1v" | "m2v" -> "video/mpeg"
| _ -> "text/plain"
let guess_contenttype filename =
guess_contenttype_from_extension (extension filename)
let guess_contenttype_from_path path =
guess_contenttype_from_extension (extension_of_path path)
(* ************************************************************************** *)
(* Picture *)
(* ************************************************************************** *)
module type PICTURE =
sig
type t =
{
info : Info.t;
url_small : url;
url_big : url;
}
val from_json : Yojson.Basic.json -> t
val contenttypes : contenttype list
val checker : contenttype -> bool
end
module Picture : PICTURE =
struct
type t =
{
info : Info.t;
url_small : url;
url_big : url;
}
let from_json c =
{
info = Info.from_json c;
url_small = c |> member "url_small" |> to_string;
url_big = c |> member "url_big" |> to_string;
}
let contenttypes = [
"image/jpeg";
"image/png";
"image/bmp";
]
let checker = checker contenttypes
end
(* ************************************************************************** *)
(* Video *)
(* ************************************************************************** *)
module type VIDEO =
sig
type t =
{
info : Info.t;
url : url;
thumbnail : Picture.t;
}
val from_json : Yojson.Basic.json -> t
val contenttypes : contenttype list
val checker : contenttype -> bool
end
module Video : VIDEO =
struct
type t =
{
info : Info.t;
url : url;
thumbnail : Picture.t;
}
let from_json c =
{
info = Info.from_json c;
url = c |> member "url" |> to_string;
thumbnail = Picture.from_json (c |> member "thumbnail");
}
let contenttypes = [
"video/mp4";
]
let checker = checker contenttypes
end
module type EXTERNALVIDEO =
sig
type provider =
| Youtube
| DailyMotion
| Vimeo
| Unknown
type t =
{
info : Info.t;
provider : provider;
video_url : url;
thumbnail : Picture.t;
}
val from_json : Yojson.Basic.json -> t
val provider_to_string : provider -> string
val provider_of_string : string -> provider
end
module ExternalVideo : EXTERNALVIDEO =
struct
type provider =
| Youtube
| DailyMotion
| Vimeo
| Unknown
type t =
{
info : Info.t;
provider : provider;
video_url : url;
thumbnail : Picture.t;
}
let provider_to_string = function
| Youtube -> "youtube"
| DailyMotion -> "dailymotion"
| Vimeo -> "vimeo"
| Unknown -> "unknown"
let provider_of_string = function
| "youtube" -> Youtube
| "dailymotion" -> DailyMotion
| "vimeo" -> Vimeo
| _ -> Unknown
let from_json c =
{
info = Info.from_json c;
provider = provider_of_string (c |> member "provider" |> to_string);
video_url = c |> member "url" |> to_string;
thumbnail = Picture.from_json (c |> member "thumbnail");
}
end
(* ************************************************************************** *)
(* Media *)
(* ************************************************************************** *)
type t =
| Picture of Picture.t
| Video of Video.t
| ExternalVideo of ExternalVideo.t
| Media of (string * string)
| Id of string
let from_json c = (* todo match type with *)
try Picture (Picture.from_json c)
with
| Yojson.Basic.Util.Type_error (msg, tree) -> Id (c |> to_string)
let checker = checker (Picture.contenttypes @ Video.contenttypes)
let thumbnail = function
| Picture p -> p.Picture.url_small
| Video v -> v.Video.thumbnail.Picture.url_small
| ExternalVideo v -> v.ExternalVideo.thumbnail.Picture.url_small
| _ -> ""
let url = function
| Picture p -> p.Picture.url_big
| Video v -> v.Video.thumbnail.Picture.url_big
| ExternalVideo v -> v.ExternalVideo.thumbnail.Picture.url_big
| _ -> ""
let id = function
| Picture p -> p.Picture.info.Info.id
| Video v -> v.Video.info.Info.id
| ExternalVideo v -> v.ExternalVideo.info.Info.id
| _ -> ""