public/Invoke-AutomatedInstagramSingleMediaPost.ps1
function Invoke-AutomatedInstagramSingleMediaPost { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BucketName, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Competition, [Parameter(Mandatory=$true)] [ValidateSet('corners','goals','match','outcomes')] [string]$Content, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Date, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$PageId, [Parameter(Mandatory=$false)] [switch]$Post, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Path, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Token ) process{ $ErrorActionPreference = 'Stop' try { # Count Instagram posts that have been made Get-InstagramPostCount -Date $Date -Path $Path # Import the fixtures from the results file $ResultsPath = "$Path\sportsmonk-results\$Competition\$Date\$Competition-$Date.csv" $ResultsPathExists = Test-Path -Path $ResultsPath if ($ResultsPathExists) { if ($Content -eq 'outcomes') { $FixtureArtifactPath = "$Path\fixture-artifact\working-set\$Competition\$Date" $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\outcomes_post.txt" } else { $FixtureArtifactPath = "$Path\fixture-artifact\working-set\$Competition\$Date\$Content" } # if # Import the results $FixtureResults = Import-Csv -Path $ResultsPath -ErrorAction Stop # Check the artifact path exists $FixtureArtifactPathExists = Test-Path -Path $FixtureArtifactPath $Hash1 =@{ Competition = $Competition FunctionName = $($MyInvocation.MyCommand.Name) Content = $Content FixtureArtifactPathExists = $FixtureArtifactPathExists FixtureResultCount = $($FixtureResults.Count) Post = 'instagram-image' } $Hash1 | Format-Table } # if if ($FixtureArtifactPathExists -and $($FixtureResults.Count) -ge 1) { if ($Content -eq 'outcomes') { $FixtureArtifacts = Get-InstagramContent -BucketName $BucketName -Content $Content -Type post | Where-Object {$_.Name -like "$Date-$Competition*"} } else { $FixtureArtifacts = Get-InstagramContent -BucketName $BucketName -Content $Content -Type post } # if if ($($FixtureArtifacts.Count) -ge 1) { # Convert string to DateTime object $DateObject = [DateTime]::ParseExact($Date, 'yyyy-MM-dd', $null) # Format the DateTime object to the desired format 2025-04-06 becomes 06-04-2025 $FormattedDate = $DateObject.ToString('dd-MM-yyyy') # Get Competition name $CompetitionName = Get-Competition -Name $Competition if ($Content -eq 'outcomes') { $FileCount = 1 $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\outcomes_post.txt" $TmplatePathExists = Test-Path -Path $TemplatePath if ($TmplatePathExists) { foreach ($FixtureArtifact in $FixtureArtifacts) { $S3Key = "instagram/$Content/$($FixtureArtifact.Name)" $PSObject = [PSCustomObject]@{ Competition = $Competition Date = $Date PhotoPath = $($FixtureArtifact.Name) S3Key = $S3Key FileCount = $FileCount Content = $Content Post = 'instagram-image' } $PSObject | Format-List $Template = Get-Content -Path $TemplatePath -Raw $Template = $Template.Replace('<CompetitionName>',$CompetitionName) $Template = $Template.Replace('<FormattedDate>',$FormattedDate) $Template $FilesProcessed = $false $OutcomeFilesToProcess = Confirm-ProcessedFootballFixtureMedia -Competition $Competition -Date $Date -Content $Content -Endpoint instagram -Path $Path if ($($FixtureArtifacts.Count) -eq $($OutcomeFilesToProcess.Count)) { $FilesProcessed = $true } # if $Hash2 =@{ CompetitionName = $CompetitionName Date = $Date Content = $Content FixtureArtifacts = $($FixtureArtifacts.Count) FilesToProcesss = $($OutcomeFilesToProcess.Count) FilesProcessed = $FilesProcessed } $Hash2 | Format-Table if ($Post -and !$FilesProcessed) { if ($Post) { [int]$DateYear = $Date.Split('-')[0] [int]$DateMonth = $Date.Split('-')[1] $MLogPath = Get-ProcessedFootballFixtureFolderPath -Competition $Competition -DateYear $DateYear -DateMonth $DateMonth -Type media -Path $Path Invoke-InstagramSingleMediaPost -PageId $PageId -Token $Token -Caption $Template -BucketName $BucketName -File $S3Key $PSObject | ConvertTo-Json | Set-Content -Path "$MlogPath\$Competition-$Date-$FileCount-$Content-instagram.json" $FileCount ++ } # if } # if } # foreach } # if } # if foreach ($FixtureResult in $FixtureResults) { $FixtureNameString = $($FixtureResult.FixtureName).Replace(' ','-') $FilePrefix = "$($FixtureResult.fixture_id)-$FixtureNameString" if ($Content -eq 'match') { $FixtureArtifact = $FixtureArtifacts | Where-Object {$_.Name -like "$FilePrefix-twitter-review*"} } else { $FixtureArtifact = $FixtureArtifacts | Where-Object {$_.Name -like "$FilePrefix-twitter-$Content*"} } # if if ($($FixtureArtifact.Count) -eq 1) { $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\social_media.txt" $TmplatePathExists = Test-Path -Path $TemplatePath if ($TmplatePathExists) { $S3Key = "instagram/$Content/$($FixtureArtifact.Name)" $PSObject = [PSCustomObject]@{ Competition = $Competition Date = $Date FixtureId = $($FixtureResult.fixture_id) FixtureName = $($FixtureResult.FixtureName) S3Key = $S3Key Content = $Content Post = 'instagram-image' } $PSObject | Format-List $Template = Get-Content -Path $TemplatePath -Raw $Template = $Template.Replace('<Content>',$Content) $Template = $Template.Replace('<CompetitionName>',$CompetitionName) $Template = $Template.Replace('<FormattedDate>',$FormattedDate) $Template = $Template.Replace('<FixtureName>',$($FixtureResult.FixtureName)) $Template $FixtureProcessed = $false $FixturesToProcess = Confirm-ProcessedFootballFixtureMedia -Competition $Competition -Date $Date -Content $Content -Endpoint instagram -Path $Path if ($($FixturesToProcess.fixture_Id) -notcontains $($FixtureResult.fixture_id)) { $FixtureProcessed = $true } # if $Hash3 =@{ FixtureId = $($FixtureResult.fixture_id) FixtureName = $($FixtureResult.FixtureName) Content = $Content FixtureProcessed = $FixtureProcessed } $Hash3 | Format-Table if ($Post -and !$FixtureProcessed) { [int]$DateYear = $Date.Split('-')[0] [int]$DateMonth = $Date.Split('-')[1] $MLogPath = Get-ProcessedFootballFixtureFolderPath -Competition $Competition -DateYear $DateYear -DateMonth $DateMonth -Type media -Path $Path Invoke-InstagramSingleMediaPost -PageId $PageId -Token $Token -Caption $Template -BucketName $BucketName -File $S3Key $PSObject | ConvertTo-Json | Set-Content -Path "$MlogPath\$Competition-$Date-$($FixtureResult.fixture_id)-$Content-instagram.json" } # if } # if } # if } # foreach } # if } # if } catch { throw "$($MyInvocation.MyCommand.Name): $_.Exception.Message" } # try catch } # process } # function |