|
| 1 | + |
| 2 | +/* |
| 3 | +
|
| 4 | + UFC Fighter API in NodeJS |
| 5 | + ------------------------- |
| 6 | + Crawler and Parser for UFC.com Fighter Profiles |
| 7 | +
|
| 8 | + Copyright: (c) 2015 Andrew Valish |
| 9 | + License: BSD, see LICENSE for more details |
| 10 | +
|
| 11 | +*/ |
| 12 | + |
| 13 | +var request = require("request"); |
| 14 | +var cheerio = require("cheerio"); |
| 15 | + |
| 16 | +//-------------------------------------------------------+ |
| 17 | +// Get Fighter Profile Data |
| 18 | +// ufc.fighter(url, callback(data)); |
| 19 | +//-------------------------------------------------------+ |
| 20 | + |
| 21 | +module.exports.fighter = function(url, callback) { |
| 22 | + request(url, function(error, response, html) { |
| 23 | + if (!error && response.statusCode == 200) { |
| 24 | + var $ = cheerio.load(html); |
| 25 | + |
| 26 | + //----------------------------------+ |
| 27 | + // JSON object for Fighter |
| 28 | + //----------------------------------+ |
| 29 | + var fighter = { |
| 30 | + name: "", |
| 31 | + nickname: "", |
| 32 | + fullname: "", |
| 33 | + hometown: "", |
| 34 | + location: "", |
| 35 | + age: "", |
| 36 | + height: "", |
| 37 | + height_cm: "", |
| 38 | + weight: "", |
| 39 | + weight_kg: "", |
| 40 | + record: "", |
| 41 | + college: "", |
| 42 | + degree: "", |
| 43 | + summary: [], |
| 44 | + metrics: { |
| 45 | + strikes: { |
| 46 | + attempted: 0, |
| 47 | + successful: 0, |
| 48 | + standing: 0, |
| 49 | + clinch: 0, |
| 50 | + ground: 0 |
| 51 | + }, |
| 52 | + takedowns: { |
| 53 | + attempted: 0, |
| 54 | + successful: 0, |
| 55 | + submissions: 0, |
| 56 | + passes: 0, |
| 57 | + sweeps: 0 |
| 58 | + } |
| 59 | + }, |
| 60 | + fights: [] |
| 61 | + }; |
| 62 | + |
| 63 | + // Name |
| 64 | + $('#fighter-details h1').filter(function() { |
| 65 | + var el = $(this); |
| 66 | + name = el.text(); |
| 67 | + fighter.name = name; |
| 68 | + }); |
| 69 | + |
| 70 | + // Nickname |
| 71 | + $('td#fighter-nickname').filter(function() { |
| 72 | + var el = $(this); |
| 73 | + nickname = el.text(); |
| 74 | + fighter.nickname = nickname; |
| 75 | + }); |
| 76 | + |
| 77 | + // Fullname |
| 78 | + $('head title').filter(function() { |
| 79 | + var el = $(this); |
| 80 | + fullname = el.text().split(' -')[0]; |
| 81 | + fighter.fullname = fullname; |
| 82 | + }); |
| 83 | + |
| 84 | + // Hometown |
| 85 | + $('td#fighter-from').filter(function() { |
| 86 | + var el = $(this); |
| 87 | + hometown = el.text().replace(/[\n\t]/g,""); |
| 88 | + fighter.hometown = hometown; |
| 89 | + }); |
| 90 | + |
| 91 | + // Location |
| 92 | + $('td#fighter-lives-in').filter(function() { |
| 93 | + var el = $(this); |
| 94 | + location = el.text().replace(/[\n\t]/g,""); |
| 95 | + fighter.location = location; |
| 96 | + }); |
| 97 | + |
| 98 | + // Age |
| 99 | + $('td#fighter-age').filter(function() { |
| 100 | + var el = $(this); |
| 101 | + age = el.text(); |
| 102 | + fighter.age = age; |
| 103 | + }); |
| 104 | + |
| 105 | + // Height |
| 106 | + $('td#fighter-height').filter(function() { |
| 107 | + var el = $(this); |
| 108 | + height = el.text().split(' (')[0]; |
| 109 | + height_cm = el.text().split('( ')[1].split(' cm )')[0]; |
| 110 | + fighter.height = height; |
| 111 | + fighter.height_cm = height_cm; |
| 112 | + }); |
| 113 | + |
| 114 | + // Weight |
| 115 | + $('td#fighter-weight').filter(function() { |
| 116 | + var el = $(this); |
| 117 | + weight = el.text().split(' lb (')[0]; |
| 118 | + weight_kg = el.text().split('( ')[1].split(' kg )')[0]; |
| 119 | + fighter.weight = weight; |
| 120 | + fighter.weight_kg = weight_kg; |
| 121 | + }); |
| 122 | + |
| 123 | + // Record |
| 124 | + $('td#fighter-skill-record').filter(function() { |
| 125 | + var el = $(this); |
| 126 | + record = el.text(); |
| 127 | + fighter.record = record; |
| 128 | + }); |
| 129 | + |
| 130 | + // College |
| 131 | + $('td#fighter-college').filter(function() { |
| 132 | + var el = $(this); |
| 133 | + college = el.text(); |
| 134 | + fighter.college = college; |
| 135 | + }); |
| 136 | + |
| 137 | + // Degree |
| 138 | + $('td#fighter-degree').filter(function() { |
| 139 | + var el = $(this); |
| 140 | + degree = el.text(); |
| 141 | + fighter.degree = degree; |
| 142 | + }); |
| 143 | + |
| 144 | + // Summary |
| 145 | + $('td#fighter-skill-summary').filter(function() { |
| 146 | + var el = $(this); |
| 147 | + summary = el.text().split(", "); |
| 148 | + fighter.summary = summary; |
| 149 | + }); |
| 150 | + |
| 151 | + // Striking Metrics |
| 152 | + $('#fight-history .overall-stats').first().filter(function() { |
| 153 | + var el = $(this); |
| 154 | + var stAttempted = el.find('.graph').first(); |
| 155 | + var stSuccessful = el.find('.graph#types-of-successful-strikes-graph'); |
| 156 | + strikes_attempted = parseInt(stAttempted.find('.max-number').text()); |
| 157 | + strikes_successful = parseInt(stAttempted.find('#total-takedowns-number').text()); |
| 158 | + strikes_standing = parseInt(stSuccessful.find('.text-bar').first().text()); |
| 159 | + strikes_clinch = parseInt(stSuccessful.find('.text-bar').first().next().text()); |
| 160 | + strikes_ground = parseInt(stSuccessful.find('.text-bar').first().next().next().text()); |
| 161 | + fighter.metrics.strikes.attempted = strikes_attempted; |
| 162 | + fighter.metrics.strikes.successful = strikes_successful; |
| 163 | + fighter.metrics.strikes.standing = strikes_standing; |
| 164 | + fighter.metrics.strikes.clinch = strikes_clinch; |
| 165 | + fighter.metrics.strikes.ground = strikes_ground; |
| 166 | + }); |
| 167 | + |
| 168 | + // Grappling Metrics |
| 169 | + $('#fight-history .overall-stats').first().next().filter(function() { |
| 170 | + var el = $(this); |
| 171 | + var tdAttempted = el.find('.graph').first(); |
| 172 | + var tdSuccessful = el.find('.graph#grappling-totals-by-type-graph'); |
| 173 | + takedowns_attempted = parseInt(tdAttempted.find('.max-number').text()); |
| 174 | + takedowns_successful = parseInt(tdAttempted.find('#total-takedowns-number').text()); |
| 175 | + takedowns_submissions = parseInt(tdSuccessful.find('.text-bar').first().text()); |
| 176 | + takedowns_passes = parseInt(tdSuccessful.find('.text-bar').first().next().text()); |
| 177 | + takedowns_sweeps = parseInt(tdSuccessful.find('.text-bar').first().next().next().text()); |
| 178 | + fighter.metrics.takedowns.attempted = takedowns_attempted; |
| 179 | + fighter.metrics.takedowns.successful = takedowns_successful; |
| 180 | + fighter.metrics.takedowns.submissions = takedowns_submissions; |
| 181 | + fighter.metrics.takedowns.passes = takedowns_passes; |
| 182 | + fighter.metrics.takedowns.sweeps = takedowns_sweeps; |
| 183 | + }); |
| 184 | + |
| 185 | + callback(fighter); |
| 186 | + } |
| 187 | + }); |
| 188 | +} |
| 189 | + |
0 commit comments