/usr/share/doc/libjson-static-camlp4-dev/examples/yahoo.ml is in libjson-static-camlp4-dev 0.9.8-1build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | (*
OCaml script that queries the JSON interface of Yahoo! Image Search,
and displays the results (not very nicely, but you can improve this part).
What you need to compile this program:
- json-wheel
- json-static
- netclient
1) Compile
ocamlfind ocamlopt -o yahoo yahoo.ml -linkpkg \
-package json-static,netclient -syntax camlp4o
2) Run
./yahoo "Nelson Mandela"
For more info on JSON and Yahoo! web services, go to
http://developer.yahoo.com/common/json.html
*)
open Printf
type json search_results =
< result_set "ResultSet":
< total_results_available "totalResultsAvailable": string;
total_results_returned "totalResultsReturned": int;
first_result_position "firstResultPosition": int;
result "Result": item list > >
and item =
< title "Title": string;
summary "Summary": string;
url "Url": string;
click_url "ClickUrl": string;
referer_url "RefererUrl": string;
file_size "FileSize": int;
file_format "FileFormat": string option;
height "Height": string;
width "Width": string;
thumbnail "Thumbnail": thumbnail >
and thumbnail =
< url "Url": string;
height "Height": string;
width "Width": string >
let query_url query =
"http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?\
appid=YahooDemo&query=" ^
Netencoding.Url.encode query ^ "&output=json"
let search query =
let url = query_url query in
printf "From %s\n%!" url;
let j=
(Json_io.json_of_string
(Http_client.Convenience.http_get url))
in
printf "Got the following JSON data:\n%s\n%!"
(Json_io.string_of_json ~compact:false j);
search_results_of_json j
let display obj =
let x = obj#result_set in
let start = x#first_result_position in
printf "Showing results %i-%i of %s\n"
start (start + x#total_results_returned - 1) x#total_results_available;
List.iter
(fun x -> printf " %s\n" x#url)
x#result
let _ =
match Sys.argv with
[| _; q |] -> display (search q)
| _ -> failwith "Usage: yahoo \"your search query\""
|