1 | let writer accum data =
|
---|
2 | Buffer.add_string accum data;
|
---|
3 | String.length data
|
---|
4 |
|
---|
5 | let showContent content =
|
---|
6 | Printf.printf "%s" (Buffer.contents content);
|
---|
7 | flush stdout
|
---|
8 |
|
---|
9 | let showInfo connection =
|
---|
10 | Printf.printf "Time: %f for: %s\n"
|
---|
11 | (Curl.get_totaltime connection)
|
---|
12 | (Curl.get_effectiveurl connection)
|
---|
13 |
|
---|
14 | let getContent connection url =
|
---|
15 | Curl.set_url connection url;
|
---|
16 | Curl.perform connection
|
---|
17 |
|
---|
18 | let curl_pull url =
|
---|
19 | let result = Buffer.create 4069
|
---|
20 | and errorBuffer = ref "" in
|
---|
21 | let connection = Curl.init () in
|
---|
22 | try
|
---|
23 | Curl.set_errorbuffer connection errorBuffer;
|
---|
24 | Curl.set_writefunction connection (writer result);
|
---|
25 | Curl.set_followlocation connection true;
|
---|
26 | Curl.set_url connection url;
|
---|
27 | Curl.perform connection;
|
---|
28 | (* showContent result;*)
|
---|
29 | (* showInfo connection;*)
|
---|
30 | Curl.cleanup connection;
|
---|
31 | Ok result
|
---|
32 | with
|
---|
33 | | Curl.CurlException (_reason, _code, _str) ->
|
---|
34 | Curl.cleanup connection;
|
---|
35 | Error (Printf.sprintf "Error: %s %s" url !errorBuffer)
|
---|
36 | | Failure s ->
|
---|
37 | Curl.cleanup connection;
|
---|
38 | Error (Printf.sprintf "Caught exception: %s" s)
|
---|
39 |
|
---|
40 | let newer time id dir =
|
---|
41 | match Logarion.File_store.to_text @@ Filename.(concat dir (Logarion.Id.short id) ^ ".txt") with
|
---|
42 | | Error x -> prerr_endline x; true
|
---|
43 | | Ok txt -> time > (Logarion.(Header_pack.date (Date.listing txt.date)))
|
---|
44 | | exception (Sys_error _) -> true
|
---|
45 |
|
---|
46 | let print_peers p =
|
---|
47 | let open Logarion.Header_pack in
|
---|
48 | match Msgpck.to_list p.peers with [] -> ()
|
---|
49 | | ps -> print_endline @@
|
---|
50 | List.fold_left (fun a x -> Printf.sprintf "%s %s" a (Msgpck.to_string x)) "peers: " ps
|
---|
51 |
|
---|
52 | type filter_t = { authors: Logarion.Person.Set.t; topics: Logarion.String_set.t }
|
---|
53 |
|
---|
54 | let print_pull_start width total title dir =
|
---|
55 | Printf.printf "%*d/%s %s => %s %!" width 0 total title dir
|
---|
56 |
|
---|
57 | let print_pull width total i =
|
---|
58 | Printf.printf "\r%*d/%s %!" width (i+1) total
|
---|
59 |
|
---|
60 | let printers total title dir =
|
---|
61 | let width = String.length total in
|
---|
62 | print_pull_start width total title dir;
|
---|
63 | print_pull width total
|
---|
64 |
|
---|
65 | let fname dir text = Filename.concat dir (Logarion.Text.short_id text ^ ".txt")
|
---|
66 |
|
---|
67 | let pull_text url dir id =
|
---|
68 | let u = Filename.concat url ((Logarion.Id.short id) ^ ".txt") in
|
---|
69 | match curl_pull u with
|
---|
70 | | Error msg -> Printf.eprintf "Failed getting %s: %s" u msg
|
---|
71 | | Ok txt -> let txt = Buffer.contents txt in
|
---|
72 | match Logarion.Text.of_string txt with
|
---|
73 | | Error s -> prerr_endline s
|
---|
74 | | Ok text ->
|
---|
75 | let file = open_out_gen [Open_creat; Open_trunc; Open_wronly] 0o640 (fname dir text) in
|
---|
76 | output_string file txt; close_out file
|
---|
77 |
|
---|
78 | let per_text url dir filter print i id time title authors topics = match id with
|
---|
79 | | "" -> Printf.eprintf "\nInvalid id for %s\n" title
|
---|
80 | | id -> let open Logarion in
|
---|
81 | print i;
|
---|
82 | if newer time id dir
|
---|
83 | && (String_set.empty = filter.topics
|
---|
84 | || String_set.exists (fun t -> List.mem t topics) filter.topics)
|
---|
85 | && (Person.Set.empty = filter.authors
|
---|
86 | || Person.Set.exists (fun t -> List.mem (Person.to_string t) authors) filter.authors)
|
---|
87 | then pull_text url dir id
|
---|
88 |
|
---|
89 | let pull_index url authors_opt topics_opt =
|
---|
90 | let index_url = url ^ "/index.pck" in
|
---|
91 | match curl_pull index_url with
|
---|
92 | | Error s -> prerr_endline s; false
|
---|
93 | | Ok body ->
|
---|
94 | match Logarion.Header_pack.of_string (Buffer.contents body) with
|
---|
95 | | Error s -> Printf.printf "Error with %s: %s\n" url s; false
|
---|
96 | | Ok pk ->
|
---|
97 | let dir = Filename.concat Logarion.Peers.text_dir pk.info.id in
|
---|
98 | Logarion.File_store.with_dir dir;
|
---|
99 | let file = open_out_gen [Open_creat; Open_trunc; Open_wronly] 0o640
|
---|
100 | (Filename.concat dir "index.pck") in
|
---|
101 | output_string file ( Logarion.Header_pack.string {
|
---|
102 | pk with info = { pk.info with locations = url::pk.info.locations }});
|
---|
103 | close_out file;
|
---|
104 | let filter = let open Logarion in {
|
---|
105 | authors = (match authors_opt with Some s -> Person.Set.of_string s | None -> Person.Set.empty);
|
---|
106 | topics =( match topics_opt with Some s -> String_set.of_string s | None -> String_set.empty);
|
---|
107 | } in
|
---|
108 | let print = printers (string_of_int @@ Logarion.Header_pack.numof_texts pk) pk.info.title dir in
|
---|
109 | try Logarion.Header_pack.iteri (per_text url dir filter print) pk; print_newline (); true
|
---|
110 | with Invalid_argument msg -> Printf.eprintf "\nFailed to parse %s: %s\n%!" url msg; false
|
---|
111 |
|
---|
112 | let pull_list auths topics =
|
---|
113 | Curl.global_init Curl.CURLINIT_GLOBALALL;
|
---|
114 | let pull got_one peer_url = if got_one then got_one else
|
---|
115 | (pull_index peer_url auths topics) in
|
---|
116 | Logarion.Peers.fold pull false;
|
---|
117 | Curl.global_cleanup ()
|
---|
118 |
|
---|
119 | let pull url auths topics = match url with
|
---|
120 | | "" -> pull_list auths topics | x -> ignore (pull_index x auths topics)
|
---|
121 |
|
---|
122 | open Cmdliner
|
---|
123 | let term =
|
---|
124 | let authors = Arg.(value & opt (some string) None & info ["a"; "authors"]
|
---|
125 | ~docv:"comma-separated names" ~doc:"filter by authors") in
|
---|
126 | let topics = Arg.(value & opt (some string) None & info ["t"; "topics"]
|
---|
127 | ~docv:"comma-separated topics" ~doc:"filter by topics") in
|
---|
128 | let url = Arg.(value & pos 0 string "" & info [] ~docv:"URL"
|
---|
129 | ~doc:"Repository location") in
|
---|
130 | Term.(const pull $ url $ authors $ topics),
|
---|
131 | Term.info "pull" ~doc:"pull listed texts" ~man:[ `S "DESCRIPTION";
|
---|
132 | `P "Pull texts from known repositories. To add a new repository use:";
|
---|
133 | `P "txt pull [url]";
|
---|
134 | `P ("This creates a directory in " ^ Logarion.Peers.text_dir
|
---|
135 | ^ " and downloads the text index.pck file in it")]
|
---|
136 |
|
---|
137 | (*module Msg = struct*)
|
---|
138 | (* type t = string * string*)
|
---|
139 | (* let compare (x0,y0) (x1,y1) =*)
|
---|
140 | (* match compare x1 x0 with 0 -> String.compare y0 y1 | c -> c*)
|
---|
141 | (*end*)
|
---|
142 | (*module MsgSet = Set.Make(Msg)*)
|
---|
143 | (*let pull_msgs url _authors _topics =*)
|
---|
144 | (* match http_apply response url with*)
|
---|
145 | (* | Error msg ->*)
|
---|
146 | (* Printf.eprintf "Failed index request for %s %s" url msg*)
|
---|
147 | (* | Ok body ->*)
|
---|
148 | (* let rec fold_msgs s a fn =*)
|
---|
149 | (* let t, msg = Scanf.bscanf s "%s %s@\n" (fun t m -> t, m) in*)
|
---|
150 | (* if t <> "" then fold_msgs s (fn a t msg) fn else a*)
|
---|
151 | (* in*)
|
---|
152 | (* let s = Scanf.Scanning.from_string body in*)
|
---|
153 | (* let msgs = MsgSet.empty in*)
|
---|
154 | (* let date_string t = Ptime.to_date t |>*)
|
---|
155 | (* fun (y, m, d) -> Printf.sprintf "%04d-%02d-%02d" y m d in*)
|
---|
156 | (* let msgs = fold_msgs s msgs*)
|
---|
157 | (* (fun msgs t m -> match Ptime.of_rfc3339 t with*)
|
---|
158 | (* | Ok (v,_,_) -> let open MsgSet in*)
|
---|
159 | (* let msgs = if cardinal msgs > 1 then remove (max_elt msgs) msgs else msgs in*)
|
---|
160 | (* add (v,m) msgs*)
|
---|
161 | (* | _ -> msgs) in*)
|
---|
162 | (* let msg_string = MsgSet.fold*)
|
---|
163 | (* (fun (t,m) a -> a ^ Printf.sprintf " %s 𐄁 %s\n" (date_string t) m)*)
|
---|
164 | (* msgs "" in*)
|
---|
165 | (* Printf.printf "┌───{ %s }───┐\n%s" url msg_string*)
|
---|