Last active
December 31, 2015 20:09
-
-
Save tfgrahame/8038397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var curl = require('curlrequest'); | |
var curlOptions = { | |
"cert": "../tom-grahame-2013-14.pem", | |
"cacert": "/usr/local/etc/stunnel/ca.pem", | |
encoding: "utf8" | |
}; | |
var xpath = require('xpath'); | |
var dom = require('xmldom').DOMParser; | |
curlOptions.url = 'https://api.test.bbc.co.uk/sportsdata/api/sport/soccer/tournament/premier-league/team/southampton/statistics/2012'; | |
curl.request(curlOptions, function (err, stdout, meta) { | |
var xml = stdout; | |
// console.log(xml); | |
var doc = new dom().parseFromString(xml) | |
var teamname = xpath.select1("//sports-content/statistic/team/team-metadata/name/@full", doc).value | |
var players = xpath.select("//sports-content/statistic/team/player", doc); | |
var squad = [] | |
for (var index in players) { | |
var player = { name: "", assists_total:0 , events_played: 0, events_started: 0, goals_total: 0, shots_total: 0, shots_on_goal_total: 0, shots_on_goal_total: 0, shots_blocked: 0, shutouts: 0, cautions_total: 0, fouls_committed: 0} | |
var position = players[index].getElementsByTagName("player-metadata")[0].getAttributeNode("position-regular").value | |
var full_name = players[index].getElementsByTagName("name")[0].getAttributeNode("full").value | |
player['name'] = full_name | |
player['position'] = position | |
var player_stats = players[index].getElementsByTagName("player-stats")[0] | |
var off_stats = players[index].getElementsByTagName("stats-soccer-offensive")[0] | |
var def_stats = players[index].getElementsByTagName("stats-soccer-defensive")[0] | |
var foul_stats = players[index].getElementsByTagName("stats-soccer-foul")[0] | |
if (typeof(player_stats) === 'object') { | |
var events_played = players[index].getElementsByTagName("player-stats")[0].getAttributeNode("events-played") | |
if (typeof(events_played) === 'object') { | |
player['events_played'] = events_played.value | |
} | |
var events_started = players[index].getElementsByTagName("player-stats")[0].getAttributeNode("events-started") | |
if (typeof(events_started) === 'object') { | |
player['events_started'] = events_started.value | |
} | |
} | |
if (typeof(off_stats) === 'object') { | |
var shots_total = players[index].getElementsByTagName("stats-soccer-offensive")[0].getAttributeNode("shots-total") | |
if (typeof(shots_total) === 'object') { | |
player['shots_total'] = shots_total.value | |
} | |
var shots_on_goal_total = players[index].getElementsByTagName("stats-soccer-offensive")[0].getAttributeNode("shots-on-goal-total") | |
if (typeof(shots_on_goal_total) === 'object') { | |
player['shots_on_goal_total'] = shots_on_goal_total.value | |
} | |
var shots_off_goal_total = players[index].getElementsByTagName("stats-soccer-offensive")[0].getAttributeNode("shots-off-goal-total") | |
if (typeof(shots_off_goal_total) === 'object') { | |
player['shots_off_goal_total'] = shots_off_goal_total.value | |
} | |
var goals_total = players[index].getElementsByTagName("stats-soccer-offensive")[0].getAttributeNode("goals-total") | |
if (typeof(goals_total) === 'object') { | |
player['goals_total'] = goals_total.value | |
} | |
var assists_total = players[index].getElementsByTagName("stats-soccer-offensive")[0].getAttributeNode("assists-total") | |
if (typeof(assists_total) === 'object') { | |
player['assists_total'] = assists_total.value | |
} | |
} | |
if (typeof(def_stats) === 'object') { | |
var shots_blocked = players[index].getElementsByTagName("stats-soccer-defensive")[0].getAttributeNode("shots-blocked") | |
if (typeof(shots_blocked) === 'object') { | |
player['shots_blocked'] = shots_blocked.value | |
} | |
var shutouts = players[index].getElementsByTagName("stats-soccer-defensive")[0].getAttributeNode("shutouts") | |
if (typeof(shutouts) === 'object') { | |
player['shutouts'] = shutouts.value | |
} | |
} | |
if (typeof(foul_stats) === 'object') { | |
var cautions_total = players[index].getElementsByTagName("stats-soccer-foul")[0].getAttributeNode("cautions-total") | |
if (typeof(cautions_total) === 'object') { | |
player['cautions_total'] = cautions_total.value | |
} | |
var fouls_committed = players[index].getElementsByTagName("stats-soccer-foul")[0].getAttributeNode("fouls-committed") | |
if (typeof(fouls_committed) === 'object') { | |
player['fouls_committed'] = fouls_committed.value | |
} | |
} | |
squad.push(player) | |
} | |
console.log(squad) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment