UNPKG

overwatch-api

Version:

An Unoffical Overwatch API.

167 lines (144 loc) 5.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = _default; var _async = _interopRequireDefault(require("async")); var _cheerio = _interopRequireDefault(require("cheerio")); var _utils = require("./utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var MAX_RETRIES = 3; // Get HTML from playoverwatch career page. function getHTML(platform, region, tag, callback) { var url = "https://overwatch.blizzard.com/en-us/career/".concat(tag, "/"); var ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:112.0) Gecko/20100101 Firefox/112.0'; var options = { uri: encodeURI(url), headers: { 'User-Agent': ua }, encoding: 'utf8' }; return (0, _utils.retryRequest)(options, MAX_RETRIES, callback); } // Begin html parsing. function parseHTML(results, callback) { var $ = _cheerio["default"].load(results.getHTML); // Check if profile exists. var isFound = $('.heading').text() !== 'Page Not Found'; if (!isFound) { return callback(new Error('Profile not found')); } var parsed = { user: $('.Profile-player--name').text(), portrait: $('.Profile-player--portrait').attr('src'), title: $('.Profile-player---title').text(), permission: $('.Profile-private---msg').text(), endorsementImage: $('.Profile-playerSummary--endorsement').attr('src') }; var stats = {}; // Top Heroes. var topHeroCategories = { quickplay: { 'played': '0x0860000000000021', 'games_won': '0x0860000000000039', 'weapon_accuracy': '0x086000000000002F', 'eliminations_per_life': '0x08600000000003D2', 'multikill_best': '0x0860000000000346', 'objective_kills_average': '0x086000000000039C' }, competitive: { 'played': '0x0860000000000021', 'games_won': '0x0860000000000039', 'win_rate': '0x08600000000003D1', 'weapon_accuracy': '0x086000000000002F', 'eliminations_per_life': '0x08600000000003D2', 'multikill_best': '0x0860000000000346', 'objective_kills_average': '0x086000000000039C' } }; // Quickplay. stats['top_heroes'] = { quickplay: {} }; Object.keys(topHeroCategories.quickplay).forEach(function (k) { var topHeroesEls = $(".Profile-heroSummary--view.quickPlay-view [data-category-id=\"".concat(topHeroCategories.quickplay[k], "\"]")).find('.Profile-progressBar'); var topHeroes = []; topHeroesEls.each(function (i, el) { var stat = {}; stat.hero = $(this).find('.Profile-progressBar-title').text(); stat.img = $(this).find('.Profile-progressBar--icon').attr('src'); stat[k] = $(this).find('.Profile-progressBar-description').text(); topHeroes.push(stat); }); stats['top_heroes']['quickplay'][k] = topHeroes; }); // Competitive. stats['top_heroes']['competitive'] = {}; Object.keys(topHeroCategories.competitive).forEach(function (k) { var topHeroesEls = $(".Profile-heroSummary--view.competitive-view [data-category-id=\"".concat(topHeroCategories.competitive[k], "\"]")).find('.Profile-progressBar'); var topHeroes = []; topHeroesEls.each(function (i, el) { var stat = {}; stat.hero = $(this).find('.Profile-progressBar-title').text(); stat.img = $(this).find('.Profile-progressBar--icon').attr('src'); stat[k] = $(this).find('.Profile-progressBar-description').text(); topHeroes.push(stat); }); stats['top_heroes']['competitive'][k] = topHeroes; }); // // Career Stats // var statCategories = ['Combat', 'Match Awards', 'Assists', 'Average', 'Miscellaneous', 'Best', 'Game']; // Quickplay Stats. statCategories.forEach(function (item) { var els = $(".stats.quickPlay-view .option-0 .category .content .header p:contains(\"".concat(item, "\")")).closest('.content').find('.stat-item'); var statsArr = []; els.each(function (i, el) { var stat = {}; stat.title = $(this).find('.name').text(); stat.value = $(this).find('.value').text(); statsArr.push(stat); }); item = item.replace(' ', '_').toLowerCase(); stats[item] = { quickplay: [] }; stats[item]['quickplay'] = statsArr; }); // Competitive Stats. statCategories.forEach(function (item) { var els = $(".stats.competitive-view .option-0 .category .content .header p:contains(\"".concat(item, "\")")).closest('.content').find('.stat-item'); var statsArr = []; els.each(function (i, el) { var stat = {}; stat.title = $(this).find('.name').text(); stat.value = $(this).find('.value').text(); statsArr.push(stat); }); item = item.replace(' ', '_').toLowerCase(); stats[item]['competitive'] = []; stats[item]['competitive'] = statsArr; }); return callback(null, { stats: stats, parsed: parsed }); } // Transform the data into a json object we can serve. function transform(results, callback) { var parseHTML = results.parseHTML; var stats = parseHTML.stats, parsed = parseHTML.parsed; var json = { username: parsed.user, portrait: parsed.portrait, endorsement: parsed.endorsementImage, "private": parsed.permission === 'Private Profile', stats: stats }; return callback(null, json); } function _default(platform, region, tag, callback) { _async["default"].auto({ getHTML: _async["default"].apply(getHTML, platform, region, tag), parseHTML: ['getHTML', _async["default"].apply(parseHTML)], transform: ['getHTML', 'parseHTML', _async["default"].apply(transform)] }, function (err, results) { if (err) { return callback(err); } return callback(null, results.transform); }); }