public/Publish-SocialMediaContent.ps1

function Publish-SocialMediaContent {
    <#
        .EXAMPLE
            Publish-SocialMediaContent -Token $AccessToken -Fixture $Fixtures -Content predictions -StartDate 2025-08-01 -CreateContentOnly -Path C:\sportsmonk
 
        .EXAMPLE
            Publish-SocialMediaContent -Token $AccessToken -Fixture $Fixtures -Content predictions -StartDate 2025-08-03 -Path C:\sportsmonk
 
    #>

    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$true)]
        [ValidateSet('advert','predictions','prediction-results','lineups','rumours','transfers')]
        [string]$Content,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string]$Continent,

        [Parameter(Mandatory=$false)]
        [switch]$CreateContentOnly,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [object]$Fixture,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string]$StartDate,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Token

    )
    process{

        $ErrorActionPreference = 'Stop'

        $InstagramConfig = Get-Content -Path "$Path\git-hub\sportsmonk-api\function-parameters\instagram.json" -ErrorAction Stop | ConvertFrom-Json
        $FacebookConfig = Get-Content -Path "$Path\git-hub\sportsmonk-api\function-parameters\facebook.json" -ErrorAction Stop | ConvertFrom-Json
        
        $InstagramPageId = $($InstagramConfig[0].PageId)
        $BucketName = $($InstagramConfig[0].BucketName)
        $FacebookPageId = ($FacebookConfig[0].PageId)

        if ($Content -eq 'advert') {

            # Set template to use
            $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\youtube_post.txt"

            # Test if it exists
            $TemplatePathExists = Test-Path -Path $TemplatePath

            if ($TemplatePathExists) {

                # Get content of template to post
                $Message = Get-Content -Path $TemplatePath -Raw -ErrorAction Stop

                # Link to include in the feed
                $Link = 'https://www.youtube.com/@AISportPredicts'

                # Post the temple to the feed
                Invoke-FacebookPost -Token $Token -PageId $FacebookPageId -Message $Message -Link $Link

            } # if

        } # if

        if ($Content -eq 'rumours' -or $Content -eq 'transfers' -or $Content -eq 'lineups' -and $($PSBoundParameters.ContainsKey('StartDate'))) {

            # Get content to publish
            $ContentToPublish = Get-ChildItem -Path "$Path\temp\$Content" -Filter '*.txt' -File -ErrorAction Stop
                | Where-Object {$_.FullName -like "*$StartDate*" -and $_.FullName -like "*$Content.txt"}

            foreach ($File in $ContentToPublish) {

                # Print message
                Write-Warning -Message "$($MyInvocation.MyCommand.Name):Processing $($File.FullName)"

                # Get content
                $Message = Get-Content -Path $($File.FullName) -Raw -ErrorAction Stop

                if (!$CreateContentOnly) {

                    # Post content
                    Invoke-FacebookPost -Token $Token -PageId $FacebookPageId -Message $Message

                } # if

            } # foreach

        } # if

        if ($Content -eq 'predictions' -and $($PSBoundParameters.ContainsKey('Fixture')) -and $($PSBoundParameters.ContainsKey('StartDate'))) {

            # Remove current contents
            Remove-Item -Path "$Path\temp\predictions\*" -Recurse -Confirm:$false -Verbose -ErrorAction Stop

            # Create prediction preview content - CREATE CONTENT
            $Fixture | ForEach-Object -Process {

                # Ser variables
                $Competition = $_.Competition
                $Date = $_.Date
                $ConvertedFixtureDate = [DateTime]::Parse($Date)
                $Today = (Get-Date).AddDays(-1)

                if ($ConvertedFixtureDate -ge $Today) {

                    #Write-Host "$Competition on $($Date) is upcoming"

                    # Set variable
                    $FilePath = "$Path\temp\predictions\$Competition\$Date"

                    # Create new directory
                    New-Directory -Path $FilePath

                    # Create goals content
                    New-SocialMediaContent -Competition $Competition -Content match -Date $Date -Prediction -Type ou15 -Path $Path | Out-File "$FilePath\$Competition-$Date-ou15-match-prediction.txt"
                    New-SocialMediaContent -Competition $Competition -Content match -Date $Date -Prediction -Type ou25 -Path $Path | Out-File "$FilePath\$Competition-$Date-ou25-match-prediction.txt"

                    # Correct score
                    New-SocialMediaContent -Competition $Competition -Content match -Date $Date -Prediction -Type csp -Path $Path | Out-File "$FilePath\$Competition-$Date-csp-match-prediction.txt"

                    # Create dbch content
                    New-SocialMediaContent -Competition $Competition -Content match -Date $Date -Prediction -Type dbch -Path $Path | Out-File "$FilePath\$Competition-$Date-dbch-match-prediction.txt"

                    # Create btts content
                    New-SocialMediaContent -Competition $Competition -Content match -Date $Date -Prediction -Type btts -Path $Path | Out-File "$FilePath\$Competition-$Date-btts-match-prediction.txt"

                    # Create fhwn content
                    New-SocialMediaContent -Competition $Competition -Content match -Date $Date -Prediction -Type fhwn -Path $Path | Out-File "$FilePath\$Competition-$Date-fhwn-match-prediction.txt"

                    # Get content to publish
                    $ContentToPublish = Get-ChildItem -Path $FilePath -Filter '*.txt' -File -ErrorAction Stop
                        | Where-Object {$_.FullName -like "*$StartDate*" -and $_.FullName -like '*prediction.txt'}

                    if (!$CreateContentOnly) {

                        foreach ($File in $ContentToPublish) {

                            # Print message
                            Write-Warning -Message "$($MyInvocation.MyCommand.Name):Processing $($File.FullName)"

                            # Get content
                            $Message = Get-Content -Path $($File.FullName) -Raw -ErrorAction Stop

                            # Post content
                            Invoke-FacebookPost -Token $Token -PageId $FacebookPageId -Message $Message

                        } # foreach

                    } # if
                    
                } # if

            } # foreach-object

        } # if

        if ($Content -eq 'prediction-results' -and $($PSBoundParameters.ContainsKey('StartDate'))) {

            while ($Count -ne 1) {

                foreach ($Continent in $Continents) {
            
                    try {

                        Invoke-AutomatedProcessHistoricalFootballFixtureMedia -SMToken $Token -Platform facebook -PageId $FacebookPageId -BucketName $BucketName -Continent $Continent -StartDate $StartDate -Path $Path

                    }
                    catch {
            
                        $_.Exception.Message
            
                    } # try catch

                    try {

                        Invoke-AutomatedProcessHistoricalFootballFixtureMedia -SMToken $Token -Platform instagram -PageId $InstagramPageId -BucketName $BucketName -Continent $Continent -StartDate $StartDate -Path $Path
                        
                    }
                    catch {
            
                        $_.Exception.Message
            
                    } # try catch
            
                } # foreach
            
            } # while

        } # if

    } # process

} # function