armory xanax usage

description

  1. // ==UserScript==
  2. // @name armory xanax usage
  3. // @namespace namespace
  4. // @version 0.1
  5. // @description description
  6. // @author tos
  7. // @match *.torn.com/*
  8. // @grant GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. const APIKEY = 'API_KEY_HERE'
  12.  
  13. const torn_api = async (args) => {
  14. const a = args.split('.')
  15. if (a.length!==3) throw(`Bad argument in torn_api(args, key): ${args}`)
  16. return new Promise((resolve, reject) => {
  17. GM_xmlhttpRequest ( {
  18. method: "POST",
  19. url: `https://api.torn.com/${a[0]}/${a[1]}?selections=${a[2]}&key=${APIKEY}`,
  20. headers: {
  21. "Content-Type": "application/json"
  22. },
  23. onload: (response) => {
  24. try {
  25. const resjson = JSON.parse(response.responseText)
  26. resolve(resjson)
  27. } catch(err) {
  28. reject(err)
  29. }
  30. },
  31. onerror: (err) => {
  32. reject(err)
  33. }
  34. })
  35. })
  36. }
  37.  
  38. torn_api('faction..armorynewsfull').then((res) => {
  39. let total_xanax_used = 0
  40. let members_used_xanax = {}
  41. let other_news = []
  42. let oldest_event = null
  43. for (eid in res.armorynews) {
  44. const news = res.armorynews[eid].news
  45. const timestamp = res.armorynews[eid].timestamp
  46. if (news.includes('Xanax')) {
  47. if (!oldest_event || timestamp < oldest_event) {
  48. oldest_event = timestamp
  49. }
  50. if (news.includes('used one of')) {
  51. player_name = news.split('>')[1].split('<')[0]
  52. if (!(player_name in members_used_xanax)) {
  53. members_used_xanax[player_name] = 1
  54. }
  55. else {
  56. members_used_xanax[player_name] += 1
  57. }
  58. total_xanax_used += 1
  59. }
  60. else other_news.push(news)
  61. }
  62. }
  63. console.log('Oldest Event:', (new Date(oldest_event * 1000)).toUTCString())
  64. console.log('Total Xanax Used:', total_xanax_used)
  65. console.log('Members Xanax Used:', members_used_xanax)
  66. console.log('Other Xanax News:', other_news)
  67. })