Skip to content

Instantly share code, notes, and snippets.

@micmaher
Last active January 4, 2017 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micmaher/b7efd1259a8ac3d4b41d to your computer and use it in GitHub Desktop.
Save micmaher/b7efd1259a8ac3d4b41d to your computer and use it in GitHub Desktop.
Get Euro 2016 Team and Player Data from UEFA
Function Get-AllTeams{
<#
.SYNOPSIS
List all qualifying teams
.NOTES
Author: Michael Maher
Date: 14/2/16
#>
[CmdletBinding()]
$teams = Invoke-WebRequest -Uri 'http://www.uefa.com/uefaeuro/season=2016/teams/index.html'
$teamName = ($teams.ParsedHtml.getElementsByTagName('span') | Where{$_.className -eq 'team-name_name'}).innerText
$teamName
}
Function New-WindowsForm(){
<#
.SYNOPSIS
Used to draw form for matching an entrants name to a drawn team
#>
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Sample Form"
$Form.AutoSizeMode = "GrowAndShrink" # or GrowOnly
$Form.BackColor = 'white'
$Form.AutoSize = $False
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.StartPosition = "CenterScreen"
$Form.Height = 500
$Form.Width = 470
$Form.AutoScroll = $True
$Form.AcceptButton = $Button
$Form.KeyPreview = $True
# Scope of variable needs to be global to be available outside function
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$global:personname=$objTextBox1.Text;$Form.Close()}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form.Close()}})
# Adds a button labelled OK
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
# Scope of variable needs to be global to be available outside function
$OKButton.Add_Click({$global:personname=$objTextBox1.Text;$Form.Close()})
$Form.Controls.Add($OKButton)
# Adds a button labelled Cancel
# Each time you add a new control you’ll typically create a new instance of a .NET Framework class
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
$Form.Controls.Add($CancelButton)
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,20)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "Please enter your name"
$Form.Controls.Add($objLabel1)
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(10,40)
$objTextBox1.Size = New-Object System.Drawing.Size(200,20)
$Form.Controls.Add($objTextBox1)
# Bring window to the foreground
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
# Now show the form on-screen
[void] $Form.ShowDialog()
}
Function Get-SweepTeam{
<#
.SYNOPSIS
Random selection of team for an office sweep
#>
Begin{
$list = Get-AllTeams
# Using .NET style array for remove functionality
$pool = New-Object System.Collections.ArrayList
$pool.AddRange($list)
}
Process{
While($pool.Count -gt 0){
New-WindowsForm
If ($personname){
$selected = $pool| Get-Random
Write-Output "$personname - $selected"
# Take chosen teams from remaining pool
$pool.Remove($selected)
}
Else{Continue}
}
}
End{
}
}
Function Get-TeamID{
<#
.SYNOPSIS
Used by other functions to lookup the team ID
#>
[CmdletBinding()]
Param(
[Parameter(mandatory=$true,
HelpMessage="Pick a team", Position=0)]
[ValidateSet('Albania', 'Austria', 'Belgium', 'Croatia', 'Czech Republic', 'England',
'France', 'Germany', 'Hungary', 'Iceland', 'Italy', 'Northern Ireland', 'Poland',
'Portugal', 'Republic of Ireland', 'Romania', 'Russia', 'Slovakia', 'Spain',
'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'Wales')]
[String]$team)
$teams = Invoke-WebRequest -Uri 'http://www.uefa.com/uefaeuro/season=2016/teams/index.html'
$teamName = ($teams.ParsedHtml.getElementsByTagName('span') | Where{ $_.className -eq 'team-name_name'}).innerText
$teamURL = ($teams.ParsedHtml.getElementsByTagName('a') | Where{ $_.className -eq 'team-hub_link'}).pathname -replace '//', 'http://www.uefa.com/uefaeuro/'
$teamID0 = $teamURL -replace '//uefaeuro/season=2016/teams/team=', ''
$teamID = ($teamID0 -replace '/index.html', '') -replace 'http://www.uefa.com/uefaeuro/uefaeuro/season=2016/teams/team='
$table = @{}
for ($i = 0; $i -lt $teamName.Count; $i++){
$table[$teamName[$i]] = $teamID[$i]}
$table.Get_Item($team)
}
Function Get-TeamPage{
<#
.SYNOPSIS
Sends Euro 2016 Team Page and launches it in Internet Explorer
.PARAMETER Team
Select a qualifiying team
.EXAMPLE
Open the team page for the Albanian team in Internet Explorer
Get-TeamPage -Team Albania
#>
[CmdletBinding(HelpUri = 'http://www.uefa.com/uefaeuro/season=2016/teams/team=2/index.html')]
Param(
[Parameter(mandatory=$true,
HelpMessage="Pick a team", Position=0)]
[ValidateSet('Albania', 'Austria', 'Belgium', 'Croatia', 'Czech Republic', 'England',
'France', 'Germany', 'Hungary', 'Iceland', 'Italy', 'Northern Ireland', 'Poland',
'Portugal', 'Republic of Ireland', 'Romania', 'Russia', 'Slovakia', 'Spain',
'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'Wales')]
[String]$Team)
$teamID = (Get-TeamID -team $team)
$teamURL = "http://www.uefa.com/uefaeuro/season=2016/teams/team=$teamID/index.html"
$IE=new-object -com internetexplorer.application
$IE.navigate2($teamURL)
$IE.visible=$true
}
Function Get-AllTeamIDs{
<#
.SYNOPSIS
Get the team name to ID mapping for all qualifiers
#>
$teams = Invoke-WebRequest -Uri 'http://www.uefa.com/uefaeuro/season=2016/teams/index.html'
$teamName = ($teams.ParsedHtml.getElementsByTagName('span') | Where{ $_.className -eq 'team-name_name'}).innerText
$teamURL = ($teams.ParsedHtml.getElementsByTagName('a') | Where{ $_.className -eq 'team-hub_link'}).pathname -replace '//', 'http://www.uefa.com/uefaeuro/'
$teamID0 = $teamURL -replace '//uefaeuro/season=2016/teams/team=', ''
$teamID = ($teamID0 -replace '/index.html', '') -replace 'http://www.uefa.com/uefaeuro/uefaeuro/season=2016/teams/team='
$table = @{}
for ($i = 0; $i -lt $teamName.Count; $i++){
$table[$teamName[$i]] = $teamID[$i]
}
$table
}
Function Get-Squad{
<#
.SYNOPSIS
Sends Euro 2016 Squad
.DESCRIPTION
Connects to UEFA web site to scrape the squad data
.PARAMETER Team
Select a qualifiying team
.PARAMETER Output
Send the output to Gridview or a Table or CSV
.EXAMPLE
Get the Belgian squad and send it to the default output which is Gridview
Get-Squad Belgium
.EXAMPLE
Get the Belgian squad and send it to a table
Get-Squad -Team Belgium -Output Table
Name Birthdate Age Club Apps Goals
---- --------- --- ---- ---- -----
Thibaut Courtois 11/05/1992 23 Chelsea 8 3
Jean-François Gillet 31/05/1979 36 Mechelen - -
Simon Mignolet 06/08/1988 27 Liverpool 2 2
Matz Sels 26/02/1992 24 Gent - -
Toby Alderweireld 02/03/1989 27 Tottenham 10 -
Dedryck Boyata 28/11/1990 25 Celtic - -
Luis Cavanda 02/01/1991 25 Trabzonspor 1 -
Laurent Ciman 05/08/1985 30 Montreal - -
#>
[CmdletBinding()]
Param(
[Parameter(mandatory=$true, HelpMessage="Pick a team", Position=0)]
[ValidateSet('Albania', 'Austria', 'Belgium', 'Croatia', 'Czech Republic', 'England',
'France', 'Germany', 'Hungary', 'Iceland', 'Italy', 'Northern Ireland', 'Poland',
'Portugal', 'Republic of Ireland', 'Romania', 'Russia', 'Slovakia', 'Spain',
'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'Wales')]
[String]$Team,
[ValidateSet('Gridview', 'Table', 'CSV')]
[String]$Output ='Gridview')
$teamID = Get-TeamID -team $team
$squads = Invoke-WebRequest -Uri "http://www.uefa.com/uefaeuro/season=2016/teams/team=$teamID/squad/index.html"
$data = ($squads.ParsedHtml.getElementsByTagName('td')| Where{$_}).Innertext
$results = for ($i = 0; $i -lt $data.count; $i += 6){
[PSCustomObject]@{
Name = $data[$i]
Birthdate = $data[$i+1]
Age = $data[$i+2]
Club = $data[$i+3]
Apps = $data[$i+4]
Goals = $data[$i+5]}
}
If ($Output -eq 'Table'){
Write-Verbose 'Outputting to Table'
$results | Format-Table -AutoSize
}
If ($Output -eq 'Gridview'){
Write-Verbose 'Outputting to Grid'
$results | Out-GridView
}
If ($Output -eq 'CSV'){
Write-Verbose 'Outputting to CSV'
$results | Export-Csv -Path "$env:TEMP\$team.csv" -NoTypeInformation
}
}
@micmaher
Copy link
Author

Get-AllTeams - Gets all 24 qualifiers for Euro 2016
Get-Squad - Gets latest squad information from UEFA.com
Get-SweepTeam - Random draw of 24 teams
Get-TeamPage - Opens team page on UEFA.com

@micmaher
Copy link
Author

To Do

  • Dynamic Parameters for team names
  • Display teams still yet to be drawn during sweep
  • Cursor starts in name field and not on Ok button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment