Недавно кто-то в чате спрашивал, пробовал ли я разбирать страницы светлой.
В общем, есть небольшой пример без проверок, безопасности и прочего.
Хз, нужно ли оно кому. Просто оставлю здесь, может кому-то пригодится
(function () {
// Request
function Request() {
this.initialize.apply(this, arguments);
}
Request.prototype.initialize = function (url, params) {
this.url = url;
this.params = params;
var request = this;
this.http = new XMLHttpRequest();
this.http.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200) {
if (request.onFinish) {
request.onFinish(this.responseText);
}
}
};
};
Request.prototype.get = function(onFinish) {
this._send('GET', onFinish);
};
Request.prototype.post = function (onFinish) {
this._send('POST', onFinish);
};
Request.prototype._send = function(method, onFinish) {
this.onFinish = onFinish;
this.http.open(method, this.url, true);
this.http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
if (this.params) {
var paramsStr = this._stringFromRequestParams(this.params);
this.http.send(paramsStr);
} else {
this.http.send(null);
}
};
Request.prototype._stringFromRequestParams = function (params) {
var result = "";
for (var key in params) {
var val = params[key]
if (result.length > 0) {
result += `&${key}=${val}`;
} else {
result += `${key}=${val}`;
}
}
return result;
};
// Chat Client
function LightChat() {
this.initialize.apply(this, arguments);
}
LightChat.prototype.initialize = function () {
this.baseUrl = 'http://rpgmaker.ru';
this.username = 'Anonymous';
};
LightChat.prototype._findFormTags = function(url, onFinish) {
var request = new Request(url);
request.get(function (html) {
var returnVal = html.match(/name="return"\svalue="([^"]*?)"/)[1];
var formTag = html.match(/name="([^"]*?)"\svalue="1"/)[1];
onFinish(returnVal, formTag);
});
};
LightChat.prototype._findUsername = function(html) {
return html.match(/\<a\shref="\/social\/mypage"\>([\w\d]+)\<\/a\>/)[1];
};
LightChat.prototype.login = function (username, password, onFinish) {
var me = this;
this._findFormTags(this.baseUrl, function (returnVal, formTag) {
var url = `${me.baseUrl}/component/users/?task=user.login&Itemid=`;
var params = {
'username' : username,
'passwd' : password,
'options' : 'com_users',
'task' : 'user.login',
'return' : returnVal,
formTag : '1'
};
var req = new Request(url, params);
req.post(function (html) {
me.username = me._findUsername(html);
onFinish(me.username);
});
});
};
LightChat.prototype.sendMessage = function(message) {
var url = "http://rpgmaker.ru/?mode=addshout";
var params = {'n': this.username, 'c': message, 'u': 'http://', 'h': ''}
var req = new Request(url, params);
req.post(function (html) {
console.log('Message sent!');
});
};
LightChat.prototype.loadMessages = function (onFinish) {
var url = `${this.baseUrl}/?mode=getshouts&jal_lastID=0`;
var req = new Request(url);
req.get(function (html) {
var rawHistory = html.match(/<div\sid="contenttop">([^\b]+)/)[1];
var historyRawRegex = /(\d+?)\|\|(\d+?)###([^#]+?)###([^#]+?)###([^#]*?)###/ig;
var messages = new Array();
while (res = historyRawRegex.exec(rawHistory)) {
messages.push({
'messageId' : res[1],
'senderId' : res[2],
'senderName' : res[3],
'avatarUrl' : res[4],
'text' : res[5]})
}
onFinish(messages);
});
};
// Usage example
var client = new LightChat();
client.login('user', 'password', function (username) {
console.log(`User logged in as ${username}!`);
client.sendMessage('Всем дня!');
client.loadMessages(function (messages) {
for (msg in messages) {
console.log(`${msg.senderName}: ${msg.text}`)
}
});
});
})();
А еще есть подобное на python 3