FFDataDL.psm1


# FF Data scrapping
# (v)Kristan, Feb 2022
# History
# 21/02/2022 First Version
#
#

#Test 1 Get-FFPlayerData -Gameweek 26 -Season 21-22
#Test 2 Get-FFUserData -Gameweek 26 -Season 21-22 -BlockOfData 11


Function Get-FFUserData{
    param(
        #Names the File
        [Parameter(
        Mandatory, 
        Position=1, 
        HelpMessage="What gameweek is it?")]
        [ValidateRange(1, 38)]
        [int]$Gameweek,

        #Names the folder and the file that the data will be put in
        [Parameter(
        Mandatory, 
        Position=2, 
        HelpMessage="What season is it?")]
        [string]$Season,

        #Assines a value to download a given block of user data
        [Parameter(
        Mandatory, 
        Position=3, 
        HelpMessage= "Which block would you like to Download?")]
        [ValidateRange(1, 11)]
        [int]$BlockOfData
    )
      

    Process{
        #Downloads the data broken down into blocks of one million users
        switch($BlockOfData){
            1{
                $StartID=1 
                $EndID=1000000
            }
            2{
                $StartID=1000001 
                $EndID=2000000
            }            
            3{
                $StartID=2000001 
                $EndID=3000000
            }            
            4{
                $StartID=3000001
                $EndID=4000000
            }
            5{
                $StartID=4000001 
                $EndID=5000000
            }            
            6{
                $StartID=5000001
                $EndID=6000000
            }
            7{
                $StartID=6000001 
                $EndID=7000000
            }
            8{
                $StartID=7000001
                $EndID=8000000
            }
            9{
                $StartID=8000001 
                $EndID=9000000
            }
            10{
                $StartID=9000001
                $EndID=10000000
            }
            #Test case to download 500 files
            11{
                $StartID=500000 
                $EndID=500500
            }

        }

        $FolderPath = "C:\Data\fantasy\"+$Season+"_UserData_"+$StartID+"-"+$EndID+"\"
        $RootURL="https://fantasy.premierleague.com/api"
        if(Test-Path $FolderPath){
            Write-Host "Folder Exists"
        }
        else{  
            #PowerShell Create directory if not exists
            New-Item $FolderPath -ItemType Directory
            Write-Host "Folder Created successfully"
        }
        #Loops throught the API loacation downloading one User's data at a time
        for($id=$StartID; $id -le $EndID; $id++){
            $File = "UserID_"+ $id +"_GW"+ $Gameweek +"_"+ $Season +".json"
            $FilePath = $FolderPath + $File
            $URI= $RootURL + "/entry/" + $id + "/history/"
            $URI
            Invoke-WebRequest -Uri $URI  | select-Object -ExpandProperty content | Out-File  -FilePath $FilePath
        }
    }
}
    
#Retrieves Actual Player stats ("elements"), Gameweek Data ("events"), and Team data ("teams")
Function Get-FFPlayerData {
    param(
    [Parameter(
    Mandatory, 
    Position=1, 
    HelpMessage="What gameweek is it?")]
    [ValidateRange(1, 38)]
    [int]$Gameweek,

    [Parameter(
    Mandatory, 
    Position=2, 
    HelpMessage="What season is it?")]
    [string]$Season
    )       
    
    Process{
        $date = Get-Date -Format FileDate
        $File = "GW"+$Gameweek+"_"+$date+".json"
        $FolderPath = "C:\Data\fantasy\"+$Season+"_PlayerData\"
        $FilePath = $FolderPath + $File
        $RootURL="https://fantasy.premierleague.com/api"
        $URI= $RootURL + "/bootstrap-static/"
        if(Test-Path $FolderPath){
            Write-Host "Folder Exists"
        }
        else{  
            #PowerShell Create directory if not exists
            New-Item $FolderPath -ItemType Directory
            Write-Host "Folder Created successfully"
        }
        $URI
    
        Invoke-WebRequest -Uri $URI  | select-Object -ExpandProperty content | Out-File  -FilePath $FilePath

    }
}

export-modulemember -function Get-FFUserData
export-modulemember -function Get-FFPlayerData