Commits

UENISHI Kota  committed dc4c8b9

seems working till getting image url

  • Participants
  • Parent commits 1bce5eb

Comments (0)

Files changed (1)

+open Http_client.Convenience (* to use this, link 'netclient' from OCAMLPACKS *)
+open Xml
+
+exception Bad_tumblr_xml;;
+
+let get_xml url =
+  let s = http_get url in
+  print_string s;
+  flush stdout;;
+
+let get_root_url username = 
+  Printf.sprintf "http://%s.tumblr.com" username;;
+
+let api_url_base username = 
+  Printf.sprintf "http://%s.tumblr.com/api/read" username;;
+
+let get_tagged_child node name =
+  let rec pup list = 
+    match list with
+      |[]-> raise Bad_tumblr_xml;
+      |child::remain->
+	 if (Xml.tag child)=name
+	 then child
+	 else pup remain
+  in
+    pup (Xml.children node);;
+
+let get_tagged_and_attr_child node tagname attrname attrvalue=
+  let rec pup list = 
+    match list with
+      |[] -> raise Bad_tumblr_xml;
+      |child::remain->
+	if (Xml.tag child)=tagname & (Xml.attrib child attrname)=attrvalue
+	then 
+(*	  print_endline (Xml.to_string_fmt child); *)
+	  Xml.pcdata (List.nth (Xml.children child) 0)
+	else pup remain 
+  in
+    pup (Xml.children node) 
+
+let get_imgurl list post = (* <post>...<post>.xml -> (id, url) *)
+  (Xml.attrib post "id", 
+   (get_tagged_and_attr_child post "photo-url" "max-width" "1280") )::list ;;
+
+let query username offset = 
+  let url_base = api_url_base username in
+  let get_req = Printf.sprintf "?start=%d&num=50&type=photo" offset in
+  let url = url_base ^ get_req in
+  let posts_xml = List.nth (Xml.children (Xml.parse_string (http_get url))) 1 in
+    List.fold_left get_imgurl [] (Xml.children posts_xml);;
+
+let rec endless username start =
+  (query username start)::(endless username (start+50));;
+
+let rec print_id_urls list = 
+  match list with 
+    | [] -> print_endline "";
+    | (id, url)::remain -> 
+	print_endline (id ^ " " ^ url);
+	print_id_urls remain;;
+
+print_id_urls (query "kuenishi" 0);;
+
+
+(*
+let rec print_urls list = 
+  match list with 
+    | [] -> print_endline "";
+    | url::remain -> 
+	print_endline url;
+	print_urls remain;;
+
+
+  print_endline (api_url_base "kuenishi");;
+  let posts = List.map (Xml.children posts_xml) in
+  print_endline (Xml.tag posts) *)