diff --git a/.gitignore b/.gitignore index 3ded535..1579ebd 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ cabal-dev # OCaml *.cmo *.cmi +_build +*.native diff --git a/ocaml/_tags b/ocaml/_tags new file mode 100644 index 0000000..87ab315 --- /dev/null +++ b/ocaml/_tags @@ -0,0 +1 @@ +true: package(ssl,netclient,netstring,equeue-ssl) diff --git a/ocaml/build.sh b/ocaml/build.sh deleted file mode 100755 index 43b434d..0000000 --- a/ocaml/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -ocamlfind ocamlc -o cheesebot -linkpkg \ - -package netclient,ssl,equeue-ssl,netstring \ - str.cma \ - main.ml diff --git a/ocaml/main.ml b/ocaml/main.ml deleted file mode 100644 index a04c3ca..0000000 --- a/ocaml/main.ml +++ /dev/null @@ -1,43 +0,0 @@ -open Str -open Ssl -open Http_client.Convenience -open Https_client;; - -Ssl.init(); -Http_client.Convenience.configure_pipeline -(fun p -> - let ctx = Ssl.create_context Ssl.TLSv1 Ssl.Client_context in - let tct = Https_client.https_transport_channel_type ctx in - p # configure_transport Http_client.https_cb_id tct -) - - -(* Regexp required here *) -let is_youtube_url url = - let regexp = Str.regexp "\\(https?\\://\\)?\\(www\\.\\)?\\(youtube\\.com\\)\\|\\(youtu\\.be\\)/.+" in - Str.string_match regexp url 0 - - -let get_body url = - try Http_client.Convenience.http_get url with - | Http_client.Http_error e -> "http error /o\\" - | Failure f -> "http fail lol" - - -(* 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 -> get_body str - | _ -> str;; - - -try - while true do - print_string "> "; - let line = read_line () in - let answer = evaluate line in - print_endline answer - done -with - End_of_file -> print_endline "Bye!";; diff --git a/ocaml/shell.ml b/ocaml/shell.ml new file mode 100644 index 0000000..f989b7b --- /dev/null +++ b/ocaml/shell.ml @@ -0,0 +1,50 @@ +open Str +open String +open Printf + +(*open SimpleHttp*) + +open Ssl +open Http_client.Convenience +open Https_client +open Nethtml +open Netchannels;; + +Ssl.init(); +Http_client.Convenience.configure_pipeline +(fun p -> + let ctx = Ssl.create_context Ssl.TLSv1 Ssl.Client_context in + let tct = Https_client.https_transport_channel_type ctx in + p # configure_transport Http_client.https_cb_id tct +) + + +let is_url url = + let regexp = Str.regexp "\\(https?\\://\\)?www\\..+\\..+" in + Str.string_match regexp url 0 + + +let is_youtube_url url = + let regexp = Str.regexp "\\(https?\\://\\)?\\(www\\.\\)?\\(youtube\\.com\\)\\|\\(youtu\\.be\\)/.+" in + Str.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 -> SimpleHttp.get_http_title (SimpleHttp.get_body str); str + | str when is_url str -> SimpleHttp.get_http_title (SimpleHttp.get_body str); str + | _ -> str + + +let () = + try + while true do + print_string "> "; + let line = read_line () in + let answer = evaluate line in + print_endline answer + done + with + End_of_file -> print_endline "Bye!";; diff --git a/ocaml/shell.native b/ocaml/shell.native new file mode 120000 index 0000000..1177578 --- /dev/null +++ b/ocaml/shell.native @@ -0,0 +1 @@ +/Users/fab/Code/cheesebot/ocaml/_build/shell.native \ No newline at end of file diff --git a/ocaml/simpleHttp.ml b/ocaml/simpleHttp.ml new file mode 100644 index 0000000..461aefa --- /dev/null +++ b/ocaml/simpleHttp.ml @@ -0,0 +1,41 @@ +open Str +open String +open Printf +open Ssl +open Http_client.Convenience +open Https_client +open Nethtml +open Netchannels;; + +Ssl.init(); +Http_client.Convenience.configure_pipeline +(fun p -> + let ctx = Ssl.create_context Ssl.TLSv1 Ssl.Client_context in + let tct = Https_client.https_transport_channel_type ctx in + p # configure_transport Http_client.https_cb_id tct +) + + +let extract_string_value document = + match document with + | Nethtml.Data(s) -> s + | _ -> "" + + +let rec get_title_element document = + match document with + | Nethtml.Element(e, args, sub) -> Printf.printf "%s: %s" "Element\n" e + | Nethtml.Data(s) -> Printf.printf "%s: %s" "Data\n" s + + +let get_http_title body = + let ch = new Netchannels.input_string body in + let doc = Nethtml.parse ch in + get_title_element (List.hd doc) + + +(* TODO: Log errors *) +let get_body url = + try Http_client.Convenience.http_get url with + | Http_client.Http_error e -> "http error /o\\" + | Failure f -> "http fail lol"