Skip to content

Instantly share code, notes, and snippets.

@thedavecarroll
Last active July 9, 2021 14:17
Show Gist options
  • Save thedavecarroll/8794b49437af7c19b79c9f8e7b8478bc to your computer and use it in GitHub Desktop.
Save thedavecarroll/8794b49437af7c19b79c9f8e7b8478bc to your computer and use it in GitHub Desktop.
PSFollowFriday
function Split-Array {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[String[]] $InputObject
,
[ValidateRange(1, [int]::MaxValue)]
[int] $Size = 10
)
begin { $items = New-Object System.Collections.Generic.List[object] }
process { $items.AddRange($InputObject) }
end {
$chunkCount = [Math]::Floor($items.Count / $Size)
foreach ($chunkNdx in 0..($chunkCount-1)) {
, $items.GetRange($chunkNdx * $Size, $Size).ToArray()
}
if ($chunkCount * $Size -lt $items.Count) {
, $items.GetRange($chunkCount * $Size, $items.Count - $chunkCount * $Size).ToArray()
}
}
}
function Get-PSFollowFridayTweetText {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$UserName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ListSlug,
[int]$ActiveWithin = 7,
[ValidateSet('Retweets','Replies')]
[string[]]$Exclude = 'Retweets'
)
$TweetTemplate = @'
Today's #PowerShell #PSFollowFriday list comes from @{0}'s Twitter List, {1}.
{2}
{3}
'@
Set-BluebirdPSConfiguration -OutputType CustomClasses
try {
'Getting Twitter List for {0} with Slug of {1}' -f $UserName, $ListSlug | Write-Verbose
$PSFollowFridayListOwners = Get-TwitterList -Id 1400680326754615296
$PSFollowFridayUsers = Get-TwitterList -Id 1403723956373360641
$List = Get-TwitterList -Slug $ListSlug -OwnerUserName $UserName
if ($null -eq $List) {
Write-Warning -Message "Did not find Twitter List for $UserName with Slug of $ListSlug"
return
}
Write-Verbose -Message "Retrieving the list membership"
$ListMembers = $List | Get-TwitterListMember
'Found {0} members' -f $ListMembers.Count | Write-Verbose
if ($ListMembers.Count -gt 100) {
$ListMemberUsers = $ListMembers | Split-Array -Size 100 | ForEach-Object { Get-TwitterUser -User $_ }
} else {
$ListMemberUsers = $ListMembers | Get-TwitterUser
}
if ($ListMemberUsers.Protected -eq $true) {
$SkippingUsers = $ListMemberUsers | Where-Object Protected
'Skipping {0} protected users.' -f $SkippingUsers.Count | Write-Warning
$ListMemberUsers = $ListMemberUsers | Where-Object -Not Protected
}
$TimeLineParams = @{
StartTime = (Get-Date).AddDays(-$ActiveWithin)
Exclude = $Exclude
}
'Checking for user activitiy within the last {0} days' -f $ActiveWithin | Write-Verbose
$UserCount = 1
$Activity = $ListMemberUsers |
ForEach-Object {
Write-Progress -Activity 'Getting user timelines...' -Status $_.UserName -PercentComplete ($UserCount / $ListMemberUsers.Count * 100)
if ((Get-TwitterTimeline -User $_ @TimeLineParams).Count -gt 0) {
$_.UserName
}
$UserCount++
}
$FollowList = $Activity | Get-Random -Count 10
'Selected users: {0}' -f ($FollowList -join ', ') | Write-Verbose
'Adding selected users to {0} (Id: {1})' -f $PSFollowFridayUsers.Name,$PSFollowFridayUsers.Id | Write-Verbose
$PSFollowFridayUsers | Add-TwitterListMember -UserName $FollowList | Out-Null
'Adding list owner, {0}, to {1} (Id: {2})' -f $UserName,$PSFollowFridayListOwners.Name,$PSFollowFridayListOwners.Id | Write-Verbose
$PSFollowFridayListOwners | Add-TwitterListMember -UserName $UserName | Out-Null
$TweetFollowList = $FollowList | ForEach-Object { '@{0}' -f $_ }
$TweetTemplate -f $List.UserName,$List.Name,$List.Uri,($TweetFollowList -join ' ')
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
@thedavecarroll
Copy link
Author

2021-05-28

$PSFollowFridayTweet = Get-PSFollowFridayTweetText -UserName adouwes -ListSlug PowerShell
Publish-Tweet -TweetText $PSFollowFridayTweet

@thedavecarroll
Copy link
Author

Updated to work with v0.5.0 release.

The Get-TwitterListMember returns a string array of usernames. I need a [BluebirdPS.APIV2.UserInfo.User] for Get-TwitterTimeline so I needed to use Get-TwitterUser which has a limit of 100 usernames per call.

To easily chunk the returned members list into 100 (or less), I included Split-Array from a StackOverflow answer from @mklement0. Thanks! That's some awesome code.

@thedavecarroll
Copy link
Author

Updated. Only use Split-Array if the membership count is greater than 100.

@thedavecarroll
Copy link
Author

Updated. Added Out-Null to Add-TwitterListMember statements.

@thedavecarroll
Copy link
Author

Update. Added logic to skip protected users and to enforce CustomClasses output.

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