From 764581f5051ac765b699af61d94c0c1d2aa900db Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Sun, 9 Mar 2014 16:02:18 +0100 Subject: [PATCH] Add Evaluate module. The Evaluate module contains the logic for cheesebot actions. --- ocaml/evaluate.ml | 20 ++++++++++++++++++++ ocaml/shell.ml | 30 +++--------------------------- 2 files changed, 23 insertions(+), 27 deletions(-) create mode 100644 ocaml/evaluate.ml diff --git a/ocaml/evaluate.ml b/ocaml/evaluate.ml new file mode 100644 index 0000000..e4e8046 --- /dev/null +++ b/ocaml/evaluate.ml @@ -0,0 +1,20 @@ +open Str + + +let is_url url = + let regexp = regexp "\\(https?\\://\\)?www\\..+\\..+" in + string_match regexp url 0 + + +let is_youtube_url url = + let regexp = regexp "\\(https?\\://\\)?\\(www\\.\\)?\\(youtube\\.com\\)\\|\\(youtu\\.be\\)/.+" in + string_match regexp url 0 + + +(* Maybe have a list of functions and + * iter on all the items in the list *) +let evaluate str = + match str with + | str when is_url str -> + SimpleHttp.get_http_title (SimpleHttp.get_body str) + | _ -> None diff --git a/ocaml/shell.ml b/ocaml/shell.ml index 31dd7d3..076c242 100644 --- a/ocaml/shell.ml +++ b/ocaml/shell.ml @@ -1,38 +1,14 @@ open Str -let is_url url = - let regexp = regexp "\\(https?\\://\\)?www\\..+\\..+" in - string_match regexp url 0 - - -let is_youtube_url url = - let regexp = regexp "\\(https?\\://\\)?\\(www\\.\\)?\\(youtube\\.com\\)\\|\\(youtu\\.be\\)/.+" in - string_match regexp url 0 - - -(* Maybe have a list of functions and - * iter on all the items in the list *) -let evaluate str = - match str with - | str when is_youtube_url str -> - ( - let title = SimpleHttp.get_http_title (SimpleHttp.get_body str) in - match title with - | Some s -> s - | None -> "" - ) - | str when is_url str -> str - | _ -> "" - - let () = try while true do print_string "> "; let line = read_line () in - let answer = evaluate line in - print_endline answer + match Evaluate.evaluate line with + | Some answer -> print_endline answer + | _ -> () done with End_of_file -> print_endline "Bye!";;