Skip to content

Instantly share code, notes, and snippets.

@pedropapa
Created August 14, 2017 12:37
Show Gist options
  • Save pedropapa/0185cd38ab1b405a4d13157b30718b30 to your computer and use it in GitHub Desktop.
Save pedropapa/0185cd38ab1b405a4d13157b30718b30 to your computer and use it in GitHub Desktop.
Extrair quantitativos de conversa do whatsapp
<html>
<body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function indexes(source, find) {
var result = [];
for (i = 0; i < source.length; ++i) {
// If you want to search case insensitive use
// if (source.substring(i, i + find.length).toLowerCase() == find) {
if (source.substring(i, i + find.length) == find) {
result.push(i);
}
}
return result;
}
function keysrt(key) {
return function(a, b){
if(a[key] < b[key]){
return 1;
}else if(a[key] > b[key]){
return -1;
}
return 0;
}
}
var conversation = $.get(prompt("Digite o nome do arquivo da conversa, sem a extensão (txt).\n\nPS: O arquivo deve estar no mesmo local onde o script está sendo executado.") + '.txt').then(function(data, err) {
var numbers = data.match(/\d{2}:\d{2}:\d{2}: .*?:/g);
var uniqueNames = [];
$.each(numbers, function(i, el){
var _el = el.match(/\d{2}:\d{2}:\d{2}:( .*?:)/)[1];
if($.inArray(_el, uniqueNames) === -1) uniqueNames.push(_el);
});
var _finally = [];
for(x in uniqueNames) {
var times = indexes(data, uniqueNames[x]).length;
_finally.push({name: uniqueNames[x].match(/ (.*?):/)[1], times: parseInt(times)});
}
console.log(_finally.length);
_finally = _finally.sort(keysrt('times'));
for(x in _finally) {
document.write(_finally[x].name, ": <b>", _finally[x].times, "</b> <br />");
}
}, function(err) {
document.write("Um erro ocorreu ao recuperar o arquivo.");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment