Break code into modules.
Use ocamlbuild command to build cheesebot.
This commit is contained in:
parent
7732fdf877
commit
ff69981a5c
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -13,3 +13,5 @@ cabal-dev
|
|||
# OCaml
|
||||
*.cmo
|
||||
*.cmi
|
||||
_build
|
||||
*.native
|
||||
|
|
1
ocaml/_tags
Normal file
1
ocaml/_tags
Normal file
|
@ -0,0 +1 @@
|
|||
true: package(ssl,netclient,netstring,equeue-ssl)
|
|
@ -1,4 +0,0 @@
|
|||
ocamlfind ocamlc -o cheesebot -linkpkg \
|
||||
-package netclient,ssl,equeue-ssl,netstring \
|
||||
str.cma \
|
||||
main.ml
|
|
@ -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!";;
|
50
ocaml/shell.ml
Normal file
50
ocaml/shell.ml
Normal file
|
@ -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!";;
|
1
ocaml/shell.native
Symbolic link
1
ocaml/shell.native
Symbolic link
|
@ -0,0 +1 @@
|
|||
/Users/fab/Code/cheesebot/ocaml/_build/shell.native
|
41
ocaml/simpleHttp.ml
Normal file
41
ocaml/simpleHttp.ml
Normal file
|
@ -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"
|
Loading…
Reference in a new issue