Bước tới nội dung

Thành viên:KiDavidbot

Từ điển mở Wiktionary
KiDavidbot
(thảo luận · đóng góp)
Người chủMuDavid
Cờ?
Ngôn ngữ lập trìnhMathematica
Ngày bắt đầuChưa kiểm tra
Tổng số sửa đổiChưa kiểm tra
Tuân thủ quy trình ngắt khẩn cấp?

Tôi là con rôbô của thành viên MuDavid. Tiền tố mu- là tiền tố tiếng Burǔndi chỉ con người, còn tiền tố ki- chỉ đồ vật nói chung. Mục đích của rôbô là cleanup mục từ tiếng Hà Lan và tạo trang dạng biến, chia của mục từ tiếng Hà Lan.

Con bot này được lập trình bằng Mathematica:

  • defs:
myagent = "foo";
sitelang = "vi";
sitename = "wiktionary";
username = "bar";
password = "baz";
site = "https://" <> sitelang <> "." <> sitename <> ".org/";
apiname = site <> "w/api.php";
  • đăng nhập, đòi edit token, đăng xuất:
first[response_] := StringPosition[response, "logintoken"][[1, 2]] + 4
last[response_] := 
 FirstCase[StringPosition[response, "\""], 
    x_ /; x[[1]] > first[response]][[1]] - 2
extractlogintoken[response_] := 
 StringTake[response, {first[response], last[response]}]
extractedittoken[response_] := 
 Module[{first, last}, 
  first = StringPosition[response, "csrftoken"][[1, 2]] + 4; 
  last = FirstCase[StringPosition[response, "\""], 
      x_ /; x[[1]] > first][[1]] - 2; 
  StringTake[response, {first, last}]]
  return = URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST",
   "Parameters" -> {"action" -> "query", "meta" -> "tokens", 
    "type" -> "login", "format" -> "json"}]
logintoken = extractlogintoken[return];
URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
 "Parameters" -> {"action" -> "clientlogin", 
   "loginreturnurl" -> "http://example.com/", 
   "logintoken" -> logintoken, "username" -> username, 
   "password" -> password, "rememberMe" -> "1", "format" -> "json"}]
return = URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST",
   "Parameters" -> {"action" -> "query", "meta" -> "tokens", 
    "format" -> "json"}]
token = extractedittoken[return];
logout := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "logout", "format" -> "json"}]
  • tải mã wiki của một trang:
loadwiki[word_] := 
 URLFetch[site <> "w/index.php?action=raw&title=" <> word, 
  "UserAgent" -> myagent]
  • sửa trang:
edit[page_, newtext_, summary_] := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "edit", "title" -> page, 
    "text" -> newtext, "summary" -> summary, "assert" -> "user", 
    "token" -> token, "format" -> "json"}]
editnocreate[page_, newtext_, summary_] := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "edit", "title" -> page, 
    "text" -> newtext, "summary" -> summary, "nocreate" -> "1", 
    "assert" -> "user", "token" -> token, "format" -> "json"}]
editcreate[page_, newtext_, summary_] := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "edit", "title" -> page, 
    "text" -> newtext, "summary" -> summary, "createonly" -> "1", 
    "assert" -> "user", "token" -> token, "format" -> "json"}]
editbot[page_, newtext_, summary_] := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "edit", "title" -> page, 
    "text" -> newtext, "summary" -> summary, "bot" -> "1", 
    "assert" -> "bot", "token" -> token, "format" -> "json"}]
editnocreatebot[page_, newtext_, summary_] := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "edit", "title" -> page, 
    "text" -> newtext, "summary" -> summary, "nocreate" -> "1", 
    "bot" -> "1", "assert" -> "bot", "token" -> token, 
    "format" -> "json"}]
editcreatebot[page_, newtext_, summary_] := 
 URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
  "Parameters" -> {"action" -> "edit", "title" -> page, 
    "text" -> newtext, "summary" -> summary, "createonly" -> "1", 
    "bot" -> "1", "assert" -> "bot", "token" -> token, 
    "format" -> "json"}]
hasmessage := (! 
   StringFreeQ[
    URLFetch[apiname, "UserAgent" -> myagent, "Method" -> "POST", 
     "Parameters" -> {"action" -> "query", "meta" -> "userinfo", 
       "uiprop" -> "hasmsg", "format" -> "json"}], "messages"])