Skip to content

Commit bb506e2

Browse files
committed
initial commit, fighter api
0 parents  commit bb506e2

File tree

5 files changed

+275
-0
lines changed

5 files changed

+275
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*build/
2+
*.lock-wscript
3+
*.node
4+
*node_modules/
5+
.DS_Store
6+
npm-debug.log
7+
*~

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Andrew Valish
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
# UFC Fighter API
3+
4+
Crawls and parses fighter data from UFC.com fighter profiles
5+
6+
## Install
7+
From source:
8+
9+
```
10+
git clone https://github.com/valish/ufc-api
11+
cd ufc-api
12+
npm install
13+
```
14+
From npm:
15+
16+
`npm install ufc`
17+
18+
## Use
19+
```
20+
> var ufc = require('ufc');
21+
> var url = "http://www.ufc.com/fighter/Jon-Jones"
22+
> ufc.fighter(url, function(data) {
23+
console.log(data);
24+
});
25+
> {
26+
27+
}
28+
```

lib/ufc/index.js

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "ufc",
3+
"version": "0.1.0",
4+
"description": "NodeJS API for UFC.com",
5+
"main": "./lib/ufc",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/Valish/ufc-api.git"
12+
},
13+
"keywords": [
14+
"UFC",
15+
"MMA",
16+
"API",
17+
"Fighter",
18+
"Metrics"
19+
],
20+
"author": "= <=>",
21+
"license": "ISC",
22+
"bugs": {
23+
"url": "https://github.com/Valish/ufc-api/issues"
24+
},
25+
"homepage": "https://github.com/Valish/ufc-api",
26+
"dependencies": {
27+
"cheerio": "^0.18.0",
28+
"request": "^2.53.0"
29+
}
30+
}

0 commit comments

Comments
 (0)