Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SetItem[Qnum_, item_, clear_Symbol: False, sum_String: "",
- nottest_: False, bot_: False, params___] :=
- Block[{pref, edittoken, data}, $name = ToString[Qnum];
- If[FreeQ[{"P", "Q"}, StringTake[$name, 1]], $name = "Q" <> $name];
- pref = If[nottest, "www", "test"];
- imp = URLFetch["http://" <> pref <> ".wikidata.org/w/api.php",
- "Method" -> "POST",
- "Parameters" -> {"action" -> "query", "prop" -> "info|revisions",
- "intoken" -> "edit", "titles" -> $name, "format" -> "json"},
- "StoreCookies" -> False, "Cookies" -> cookies];
- If[imp === $Failed,
- Print["Something wrong - token query is broken"]; Return[$Failed]];
- res = ImportString[imp, "JSON"];
- If[FreeQ[res, "query"],
- Print["Something wrong - token query has wrong format"]];
- edittoken = ToURLenc@GetParam[res, "edittoken"];
- (*data=If[Head[item]===String,item,JSONString[item/.a_String:>"\""<>
- a<>"\""]];*)
- data = If[Head[item] === String, item, JSONString[item]];
- Print["Throttling for ", $TimeThrottle, " seconds..."];
- Pause[$TimeThrottle];
- imp = URLFetch["http://" <> pref <> ".wikidata.org/w/api.php",
- "Method" -> "POST", "Cookies" -> cookies, "StoreCookies" -> False,
- "Parameters" ->
- Join[{"action" -> "wbeditentity", "id" -> $name,
- "format" -> "json", "summary" -> sum, "data" -> data},
- If[clear, {"clear" -> "true"}, {}], If[bot, {"bot" -> "1"}, {}],
- params, {"token" -> edittoken}], "StoreCookies" -> False,
- "Cookies" -> cookies];
- If[imp === $Failed,
- Print["Something wrong - edit query for [[", $name,
- "]] is broken"]; Return[$Failed]];
- res = ImportString[imp, "JSON"];
- If[FreeQ[res, "success"], GetParam[res, "success"] =!= 1,
- Print["Edit query has wrong format"]];
- If[! FreeQ[res, "error"],
- Print["There was error during edit: ",
- err = GetParam[res, "error"]];
- If[! StringFreeQ[GetParam[err, "info"],
- "try again"], $TimeThrottle *= 2; Pause[$TimeThrottle];
- SetItem[Qnum, item, clear, sum, nottest, bot, params]],
- PrintLog[Join[res, {"id" -> Qnum, "summary" -> sum}]];
- If[$TimeThrottle > 1, $TimeThrottle /= 1.5];
- Print["Item [[", $name,
- "]] was successfully filled with new data at revision ",
- GetParam[res, "lastrevid"]]];]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement