Skip to content

Instantly share code, notes, and snippets.

@pedropapa
Last active September 28, 2017 23:19
Show Gist options
  • Save pedropapa/500abc368a1e2543ae52b64151b0cb46 to your computer and use it in GitHub Desktop.
Save pedropapa/500abc368a1e2543ae52b64151b0cb46 to your computer and use it in GitHub Desktop.
SA-MP forums contributors ranking extractor
/**
* How to run:
*
* 1. Open Google Chrome.
* 2. Open SA-MP forums url (http://forum.sa-mp.com/forumdisplay.php)
* 3. Open browser's console (Windows: ctrl + shift + i, MacOS: command + shift + i).
* 4. Copy & Paste this script in the console.
* 5. Hit enter to run the code.
* 6. Follow instructions shown.
*
* Last extracted rank: https://gist.githubusercontent.com/pedropapa/51d0245965e5cfc3c800118f4f8e9ca9/raw/7568f22cd1fb325b787681c0c6deab8749e30f4b/ranking.txt
*/
var rgxp = /\n[\s]+(\n.*){2}\n*?\[*?>(.*?)<.*?\].*\n.*?showthread.*?>(.*?)<.*[\s\S]*?smallfont.>.*\n.*\n.*\n\s+(.*?span.*?>(.*?)<\/span|(.*?)\s+)[\s\S]*?replies.{2}([\d+,\,]+).+views.{2}([\d+,\,]+)/mig;
var rkRgxp = new RegExp(rgxp.source, 'mi');
var forums = [
"/forumdisplay.php?f=70&order=desc&page=%page%",
"/forumdisplay.php?f=71&order=desc&page=%page%",
"/forumdisplay.php?f=17&order=desc&page=%page%",
"/forumdisplay.php?f=83&order=desc&page=%page%",
"/forumdisplay.php?f=82&order=desc&page=%page%",
"/forumdisplay.php?f=64&order=desc&page=%page%",
"/forumdisplay.php?f=18&order=desc&page=%page%"
];
var Ranking = [];
var i = 0;
var pagesRan = 0;
var totalPages = 0;
var duplicateChecker = [];
alert("Wait until the alert, this process can take up to 20 minutes. You can watch the execution status in your browser's console.");
for(var forumIndex in forums) {
forumCrawl(forums[forumIndex]);
}
function forumCrawl(forumUrl) {
var pageLink = forumUrl;
forumUrl = forumUrl.replace('%page%', 1);
document.body.innerHTML = ajaxRequest('get', false, forumUrl);
var _totalPages = parseInt(document.body.innerHTML.match(/page.(\d+).+last.page/i)[1]);
totalPages += _totalPages;
var i = document.createElement('iframe');
i.style.display = 'none';
document.body.appendChild(i);
window.console = i.contentWindow.console;
var i = 0;
while(++i <= _totalPages) {
callRankPageDelayed(pageLink, i);
}
}
function callRankPageDelayed(pageLink, page) {
setTimeout(function() {rankPage(pageLink, page)}, Math.round(Math.random() * 5000));
}
function rankPage(pageLink, page) {
var i = document.createElement('iframe');
i.style.display = 'none';
document.body.appendChild(i);
window.console = i.contentWindow.console;
console.log('>>> ', pagesRan, totalPages);
var pageUrl = pageLink.replace('%page%', page);
var requestBody = ajaxRequest('get', false, pageUrl);
document.body.innerHTML = requestBody;
var pageRanking = document.body.innerHTML.match(rgxp);
for(var current in pageRanking) {
var record = rkRgxp.exec(pageRanking[current]);
[_, _, topicType, topicTitle, _, member1, member2, replies, views] = record;
if(!duplicateChecker[topicTitle+replies+views]) {
duplicateChecker[topicTitle+replies+views] = true;
(typeof member1 == 'undefined') ? (member = member2) : (member = member1);
if (!Ranking[member]) {
Ranking[member] = {
contributor: member,
contributions: 0,
replies: 0,
views: 0,
types: {
Tutorial: [],
FilterScript: [],
"Tool/Web/Other": [],
Include: [],
Plugin: [],
Map: [],
GameMode: [],
Other: []
}
};
}
if (topicType == "") {
topicType = "Other";
}
Ranking[member].replies += parseInt(replies.replace(/\,/g, ''));
Ranking[member].views += parseInt(views.replace(/\,/g, ''));
Ranking[member].types[topicType].push({
topicTitle: topicTitle,
views: parseInt(views.replace(/\,/g, '')),
replies: parseInt(replies.replace(/\,/g, ''))
});
Ranking[member].types[topicType].sort(function (b, c) {
return c.replies - b.replies
});
++Ranking[member].contributions;
}
}
++pagesRan;
if(pagesRan == totalPages) {
alert('See the result in your browser\'s console');
// O fórum do sa-mp executa um código que sobreescreve as funções de console, como o console.log. O snippet abaixo restaura o comportamento padrão desse objeto.
var i = document.createElement('iframe');
i.style.display = 'none';
document.body.appendChild(i);
window.console = i.contentWindow.console;
var sorted = startFromZero(Ranking).sort(function(b,c){return c.replies - b.replies})
for(var x in sorted) {
console.log(parseInt(x) + 1 + "º: " + sorted[x].contributor + " | Contributions: "+ sorted[x].contributions +" | Views: " + sorted[x].views + " | Answers: " + sorted[x].replies);
console.log(" Contributions:");
for(var y in sorted[x].types) {
if(sorted[x].types[y].length) {
console.log(" " + y + ":");
for (var z in sorted[x].types[y]) {
console.log(
" " + sorted[x].types[y][z].topicTitle + " | Answers: " + sorted[x].types[y][z].replies + " | Views: " + sorted[x].types[y][z].views);
}
}
}
}
}
}
function ajaxRequest(method, option, url, sendData, headers) {
$ = new XMLHttpRequest;
t = null;
with($) {
open(method, url, option);
if(method == "POST")
if(headers.length > 0)
for(y in headers)
setRequestHeader(headers[y][0], headers[y][1]);
send(sendData);
t = responseText;
}
return t;
}
function startFromZero(arr) {
var newArr = [];
var count = 0;
for (var i in arr) {
newArr[count++] = arr[i];
}
return newArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment