Thursday, October 31, 2013

Wikipedia BOT using Google script

This page will guide you to create a basic edit model Wikipedia BOT. As we know wikipedia is an open source online encyclopedia, it also allows anyone or any bot to update the pages. Basically, Wikipedia and all its sister projects use Mediawiki software and the API of Mediawiki is available for developer. so using Google Apps Script and Mediawiki API one can develop wonderful BOT application for Wikipedia.

There few steps to remember while creating a Wikipedia BOT(for newbie).
Mediawiki API responses are available in most forms. But Json format is most suitable for Google Apps Script. Mediawiki requires two steps to login, first is to get token and second is to confirm the token. And also note that method like Post or get should be appropriate according to the query. Finally you will need to carry the token, session id and user id along with all queries to keep your Authentication for all edits. which is similar like cookies in client machines. Below snippet of code will give you an idea of building cookies using login tokens.

function myFunction() {
var header="Test";
var msg="Hello World";
var lang = "en";
var dom =".wikipedia.org";
var user ="YOURWIKIACCOUNT";
var pass="YOURWIKIPASSWORD";
var page= "User:" + user + "/sandbox";
var options1 ={"method": "POST"};
var lurl = "http://" + lang + dom + "/w/api.php?action=login&format=json&lgname=" + user + "&lgpassword=" + pass;
try {
var login = UrlFetchApp.fetch(lurl,options1);
var data = Utilities.jsonParse(login.getContentText());
if (login.getResponseCode() === 200) {
var options2 = {"method": "POST", "headers": {"cookie":lang+"wikiSession=" + data.login.sessionid + "; path=/; domain=" + dom + "; HttpOnly;" + lang + "wikiUserName=" + user}};
var lconfirm = UrlFetchApp.fetch(lurl + "&lgtoken=" + data.login.token ,options2);
if (lconfirm.getResponseCode() === 200)
{
var data1 = Utilities.jsonParse(lconfirm.getContentText());
if(data1.login.result=="Success"){
var options3 = {"method": "GET", "headers": {"Content-Type":"application/x-www-form-urlencoded", "cookie":lang + "wikiSession=" + data1.login.sessionid + "; path=/; domain=" + dom + "; HttpOnly;" + lang + "wikiUserName=" + data1.login.lgusername + "; " + lang + "wikiUserID=" + data1.login.lguserid + "; " + lang + "wikiToken="+ data1.login.lgtoken}};
var queryurl = "https://" + lang + dom + "/w/api.php?action=query&format=json&prop=info&intoken=edit&titles=" + page;
var qresult = UrlFetchApp.fetch(queryurl,options3);
var datai =qresult.getContentText();
var toke = (datai.split("edittoken")[1]).split("\+")[0];
var token = toke.substring(3,toke.length);
var options4 = {"method": "POST", "headers": {"Content-Type":"application/x-www-form-urlencoded", "cookie":lang + "wikiSession=" + data1.login.sessionid + "; path=/; domain=" + dom + "; HttpOnly;" + lang + "wikiUserName=" + data1.login.lgusername + "; " + lang + "wikiUserID=" + data1.login.lguserid + "; " + lang + "wikiToken="+ data1.login.lgtoken}};
var editurl = "https://" + lang + dom + "/w/api.php?action=edit&format=json&title=" + page + "§ion=new&summary=" + header + "&text=" + msg + "&bot&token=" + token + "%2B%5C";
var edit = UrlFetchApp.fetch(editurl,options4);
Logger.log(edit.getContentText());
}
else{Logger.log("Unable to login");}
}
}
} catch (e) {Logger.log(e.toString());}
}

AppsWiki Editor is the one of the successful Bot applications available in online. (source code) NeechalBOT, kalaiBOT are some of the wiki bot accounts use this Editor.

No comments:

Post a Comment

You Can use HTML code once it converted to NCcode by NCode converter