67 lines
1.7 KiB
OCaml
67 lines
1.7 KiB
OCaml
open String
|
|
open Str
|
|
|
|
|
|
let is_url url =
|
|
let regexp = regexp "\\(https?\\://\\)?\\(www\\.\\)?.+\\..+" in
|
|
string_match regexp url 0
|
|
|
|
let is_imgur_url url =
|
|
let regexp = regexp "\\(https?\\://\\)?i\\.imgur\\.com/.+\\..+" in
|
|
string_match regexp url 0
|
|
|
|
let get_imgur_gallery_url url =
|
|
if not (is_imgur_url url) then
|
|
None
|
|
else begin
|
|
(* Remove i. from the beginning *)
|
|
let prefix = regexp "i\\.imgur\\.com" in
|
|
let s = replace_first prefix "imgur.com" url in
|
|
|
|
(* Remove image extension *)
|
|
let len = length s in
|
|
let ext = regexp "\\.[^\\.]+$" in
|
|
let ext_pos = search_backward ext s (len - 1) in
|
|
Some (string_before s ext_pos)
|
|
end
|
|
|
|
let is_youtube_url url =
|
|
let regexp = regexp "\\(https?\\://\\)?\\(www\\.\\)?\\(youtube\\.com\\)\\|\\(youtu\\.be\\)/.+" in
|
|
string_match regexp url 0
|
|
|
|
|
|
let remove_spaces str =
|
|
match str with
|
|
| Some s ->
|
|
let spaces = regexp "\\( *\\\n\\)+ *" in
|
|
Some (global_replace spaces "" s)
|
|
| None -> None
|
|
|
|
let format_title str =
|
|
"[ " ^ str ^ " ]"
|
|
|
|
|
|
(* Maybe have a list of functions and
|
|
* iter on all the items in the list *)
|
|
let evaluate str =
|
|
match str with
|
|
| str when is_imgur_url str ->
|
|
begin
|
|
match get_imgur_gallery_url str with
|
|
| None -> None
|
|
| Some url ->
|
|
begin
|
|
let title = SimpleHttp.get_http_title (SimpleHttp.get_body url) |> remove_spaces in
|
|
match title with
|
|
| Some t -> Some (format_title t)
|
|
| None -> None
|
|
end
|
|
end
|
|
| str when is_url str ->
|
|
(let title = SimpleHttp.get_http_title (SimpleHttp.get_body str) |> remove_spaces in
|
|
match title with
|
|
| Some t -> Some (format_title t)
|
|
| None -> None)
|
|
| "o/" -> Some "o/"
|
|
| _ -> None
|