Advertisement
infovarius

WikidataStructorSetItem

Jul 1st, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. SetItem[Qnum_, item_, clear_Symbol: False, sum_String: "",
  2. nottest_: False, bot_: False, params___] :=
  3. Block[{pref, edittoken, data}, $name = ToString[Qnum];
  4. If[FreeQ[{"P", "Q"}, StringTake[$name, 1]], $name = "Q" <> $name];
  5. pref = If[nottest, "www", "test"];
  6. imp = URLFetch["http://" <> pref <> ".wikidata.org/w/api.php",
  7. "Method" -> "POST",
  8. "Parameters" -> {"action" -> "query", "prop" -> "info|revisions",
  9. "intoken" -> "edit", "titles" -> $name, "format" -> "json"},
  10. "StoreCookies" -> False, "Cookies" -> cookies];
  11. If[imp === $Failed,
  12. Print["Something wrong - token query is broken"]; Return[$Failed]];
  13. res = ImportString[imp, "JSON"];
  14. If[FreeQ[res, "query"],
  15. Print["Something wrong - token query has wrong format"]];
  16. edittoken = ToURLenc@GetParam[res, "edittoken"];
  17. (*data=If[Head[item]===String,item,JSONString[item/.a_String:>"\""<>
  18. a<>"\""]];*)
  19. data = If[Head[item] === String, item, JSONString[item]];
  20. Print["Throttling for ", $TimeThrottle, " seconds..."];
  21. Pause[$TimeThrottle];
  22. imp = URLFetch["http://" <> pref <> ".wikidata.org/w/api.php",
  23. "Method" -> "POST", "Cookies" -> cookies, "StoreCookies" -> False,
  24. "Parameters" ->
  25. Join[{"action" -> "wbeditentity", "id" -> $name,
  26. "format" -> "json", "summary" -> sum, "data" -> data},
  27. If[clear, {"clear" -> "true"}, {}], If[bot, {"bot" -> "1"}, {}],
  28. params, {"token" -> edittoken}], "StoreCookies" -> False,
  29. "Cookies" -> cookies];
  30. If[imp === $Failed,
  31. Print["Something wrong - edit query for [[", $name,
  32. "]] is broken"]; Return[$Failed]];
  33. res = ImportString[imp, "JSON"];
  34. If[FreeQ[res, "success"], GetParam[res, "success"] =!= 1,
  35. Print["Edit query has wrong format"]];
  36. If[! FreeQ[res, "error"],
  37. Print["There was error during edit: ",
  38. err = GetParam[res, "error"]];
  39. If[! StringFreeQ[GetParam[err, "info"],
  40. "try again"], $TimeThrottle *= 2; Pause[$TimeThrottle];
  41. SetItem[Qnum, item, clear, sum, nottest, bot, params]],
  42. PrintLog[Join[res, {"id" -> Qnum, "summary" -> sum}]];
  43. If[$TimeThrottle > 1, $TimeThrottle /= 1.5];
  44. Print["Item [[", $name,
  45. "]] was successfully filled with new data at revision ",
  46. GetParam[res, "lastrevid"]]];]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement