Skip to content

Instantly share code, notes, and snippets.

@vacas
Created April 4, 2017 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vacas/c32ef6df958e89bff34262c073becc7c to your computer and use it in GitHub Desktop.
Save vacas/c32ef6df958e89bff34262c073becc7c to your computer and use it in GitHub Desktop.
Soccer API created for a Codetrotters student using api.football-data.org
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Soccer Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="soccerBody container">
<div class="row">
<div class="col-xs-12 col-sm-6">
<input id="homeTeamName" type="text" name="team">
<button id="homeSubmit" type="submit" name="button">Submit</button>
<div class="homeTeam">
</div>
</div>
<div class="col-xs-12 col-sm-6">
<input id="awayTeamName" type="text" name="team">
<button id="awaySubmit" type="submit" name="button">Submit</button>
<div class="awayTeam">
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="soccer_api.js"></script>
</body>
</html>
$(document).ready(function() {
$('#homeSubmit').click(function() {
getId($("#homeTeamName").val(), "homeTeam", "homeList");
});
$('#awaySubmit').click(function() {
getId($("#awayTeamName").val(), "awayTeam", "awayList");
});
});
function getPlayers(team, list) {
$.ajax({
headers: {
'X-Auth-Token': 'YOUR_API_KEY'
// Obtained at http://api.football-data.org/index
},
url: 'http://api.football-data.org/v1/teams/' + team + '/players',
dataType: 'json',
type: 'GET',
}).done(function(response) {
console.log(response);
for(var i = 0; i < 11; i++) {
return getStats(response.players[i].name, response.players[i].position);
}
});
}
function getTeam(id, body, list) {
$.ajax({
headers: {
'X-Auth-Token': 'YOUR_API_KEY'
// Obtained at http://api.football-data.org/index
},
url: 'http://api.football-data.org/v1/teams/' + id,
dataType: 'json',
type: 'GET',
}).done(function(response) {
$("." + body).html("<img class='img-responsive' src=" + response.crestUrl + "> <br><br> <h2>" + response.name + "</h2><ol class='" + list + "'></ol>");
return getPlayers(id, list);
});
}
function getId(query, body, list) {
$.ajax({
headers: {
'X-Auth-Token': 'YOUR_API_KEY'
// Obtained at http://api.football-data.org/index
},
url: 'http://api.football-data.org/v1/teams?name=' + query,
dataType: 'json',
type: 'GET',
}).done(function(response) {
return getTeam(response.teams[0].id, body, list);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment