vilterti / TaringaExtraInfo

// ==UserScript==
// @name TaringaExtraInfo
// @namespace Violentmonkey Scriptsd
// @match *://www.taringa.net/*
// @grant none
// @require https://code.jquery.com/jquery-3.2.1.slim.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js
// @grant       GM_xmlhttpRequest
// @connect     api.taringa.net
// @license MIT
// @autor Kroll_AK47(taringa)
// ==/UserScript==
// 
// 
////////////////////////////////////////
let ocultarPostDeCreadores = false // Si no queres ocultar los posts pero si queres ver si tienen creadores, cambien true por false o viseversa

let cache = JSON.parse(window.localStorage.getItem('cache')) || []

console.log('CACHE:', cache)

let usuarios = getAllNicks()
let posts = getAllPosts()

async function main() {

  procesarPost(cache)

  usuariosNoCacheados = _.difference(usuarios, cache.map(e => e.nick))

  //Le mete la info de la api a los usuarios
  usuariosNoCacheados = await Promise.all(usuarios.map(getUserInfo))

  usuariosNoCacheados = usuariosNoCacheados.map(e => {
    return {
      nick: e.nick,
      country: e.country,
      rewards_type: e.rewards_type
    }
  })

  usuarios = _.uniq([...usuariosNoCacheados, ...cache])

  window.localStorage.setItem('cache', JSON.stringify(usuarios))

  procesarPost(usuariosNoCacheados)
}

function procesarPost(usuarios) {
  let creadores = usuarios.filter(e => e.rewards_type == 'revshare')

  for (post of posts) {
    if (creadores.map(e => e.nick).includes(post.owner)) {
      if (ocultarPostDeCreadores) {
        $(post).parents('li').css('display', 'none')
      }
      agregarMedalla(post.main)
    }
    agregarBandera(post, usuarios)
  }
}

main()

function agregarMedalla(post) {
  $(post).find('h3')
    .prepend('<p> <span class="icon-recompensado" style="position:absolute; color:red;left: 20px;font-size: 10px;"></span></p>')
}

function agregarBandera(post, usuarios) {
  let ownerCountry = _.find(usuarios, {
    nick: post.owner
  })
  if (!ownerCountry) return
  ownerCountry = ownerCountry.country.toLowerCase()

  let margin = 11
  if ($(post.main).find('img').hasClass('thumb')) {
    margin = 4
  }
  $(post.main)
    .find('h3')
    .prepend(`<img src="https://o1.t26.net/images/flags/${ownerCountry}.png" class="hastipsy country-name" style="position:absolute; width:10px; height:10px;top:${margin}px;left:${margin}px">`)
}

//Devuelve la informarcion de un usuario desde la api de taringa
function getUserInfo(usuario) {
  return new Promise((resolve, reject) => {
    GM_xmlhttpRequest({
      method: 'GET',
      url: 'http://api.taringa.net/user/nick/view/' + usuario,
      onload: ({
        response
      }) => {
        try {
          let data = JSON.parse(response)
          resolve(data)
        }
        catch (err) {
          resolve('buegueado: ' + usuario)
        }

      }, // JSON error?
      onerror: reject
    })
  })
}

function getAllNicks() {
  let nicks = []

  //Crea una lista de todos los usuarios que aparecen en la home
  $('.usuario').each((i, e) => {
    nicks.push($(e).attr('title'))
  })
  $('.icon-usuarios').each((i, e) => {
    nicks.push($(e).html())
  })
  return _.uniq(nicks)
    .filter(e => {
      return !e.includes('\n') && e.length > 1
    })
}

function getAllPosts() {
  return $('.icon-usuarios')
    .parents('li')
    .map((i, e) => {
      return {
        main: e,
        owner: $(e).find('.usuario').attr('title') || $(e).find('p').html()
      }
    })
}

function getPostOwner(post) {

}