public/Invoke-AutomatedInstagramSingleMediaStoryPost.ps1
function Invoke-AutomatedInstagramSingleMediaStoryPost { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BucketName, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Competition, [Parameter(Mandatory=$true)] [ValidateSet('corners','goals','match')] [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 { # Import the fixtures from the results file $ResultsPath = "$Path\sportsmonk-results\$Competition\$Date\$Competition-$Date.csv" $ResultsPathExists = Test-Path -Path $ResultsPath if ($ResultsPathExists) { $FixtureResults = Import-Csv -Path $ResultsPath -ErrorAction Stop # Check the artifact path exists $FixtureArtifactPath = "$Path\fixture-artifact\instagram\$Content" $FixtureArtifactPathExists = Test-Path -Path $FixtureArtifactPath $Hash1 =@{ Competition = $Competition FunctionName = $($MyInvocation.MyCommand.Name) Content = $Content FixtureArtifactPathExists = $FixtureArtifactPathExists FixtureResultCount = $($FixtureResults.Count) } $Hash1 | Format-Table } # if if ($FixtureArtifactPathExists -and $($FixtureResults.Count) -ge 1) { $Url = "https://$BucketName.s3.eu-west-1.amazonaws.com" $Response = Invoke-WebRequest -Uri $url [xml]$xml = $Response.Content # Get the fixture artifacts $RemoteArtifacts = $xml.ListBucketResult.Contents ` | Where-Object {$_.Key -like "instagram/$Content*"} ` | Select-Object -ExpandProperty Key $PSObjects =@() $RemoteArtifacts | ForEach-Object -Process { $FileName = $_.Split('/')[-1] $PSObject = [PSCustomObject]@{ Name = $FileName } $PSObjects += $PSObject } # foreach-object # Exclude files with special characters $FixtureArtifacts = $PSObjects | Where-Object {$_.Name -match "^[\x00-\x7F]+$"} 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 foreach ($FixtureResult in $FixtureResults) { $FixtureNameString = $($FixtureResult.FixtureName).Replace(' ','-') $FilePrefix = "$($FixtureResult.fixture_id)-$FixtureNameString" $FixtureArtifact = $FixtureArtifacts | Where-Object {$_.Name -like "$FilePrefix*"} 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 Endpoint = 'instagram-story' } $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 [int]$DateYear = $Date.Split('-')[0] [int]$DateMonth = $Date.Split('-')[1] $MFileDateTime = Get-Date -Format FileDateTime $MLogPath = Get-ProcessedFootballFixtureFolderPath -Competition $Competition -DateYear $DateYear -DateMonth $DateMonth -Type media -Path $Path $FilesToProcess = Get-ChildItem -Path $MLogPath | Where-Object {$_.FullName -like "*$Content-instagram-story*"} $FixtureProcessed = $false foreach ($File in $($FilesToProcess.FullName)) { Write-CustomWarningMessage -ParentPath $Path -FilePath $File $FileContent = Get-Content $File | ConvertFrom-Json if ($($FileContent.FixtureId) -eq $($FixtureResult.fixture_id)) { $FixtureProcessed = $true } # if } # foreach if ($Post -and !$FixtureProcessed) { Invoke-InstagramSingleMediaPost -PageId $PageId -Token $Token -BucketName $BucketName -File $S3Key -Story $PSObject| ConvertTo-Json | Set-Content -Path "$MlogPath\$MFileDateTime-$Competition-football-fixture-$Content-instagram-story.json" } # if } # if } # if } # foreach } # if } # if } catch { throw "$($MyInvocation.MyCommand.Name): $_.Exception.Message" } # try catch } # process } # function |