Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active October 24, 2020 07:48
Show Gist options
  • Save PrateekKumarSingh/757b7582d7268c1309db92db4c139843 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/757b7582d7268c1309db92db4c139843 to your computer and use it in GitHub Desktop.
[cmdletbinding()]
param(
[Int] $IntervalInMins = 15,
[Int] $Count = 5
)
if(-not (Get-module poshtwit)) {
Install-Module PoshTwit -Force -Verbose -Scope CurrentUser
}
Function Get-ShortURL
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline = $true)][string] $URL
)
Write-Verbose "Creating Short URL for $URL"
Invoke-RestMethod -Uri "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDne5JQP_6F0aBsnz9RcItd3zeO1v6J734" `
-Body $(''| Select-object @{n='longUrl';e={$URL}}|convertto-json) `
-ContentType 'application/json' `
-Method Post | ForEach-Object ID
}
Function Get-BlogArticle
{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)] [uri] $URL
)
$Data = @() ; $PageNumber = 1
Write-Verbose "Web requesting $URL for Articles - Topic & URL."
While($true) {
if($PageNumber -eq 1) {
$SubString = ""
}
else {
$SubString = "page/$PageNumber/"
}
Try {
$WebRequest = Invoke-WebRequest -Uri "https://$URL/$SubString"
$Data += ($WebRequest).parsedhtml.all | `
Where-Object {$_.nodename -eq 'a' -and $_.rel -eq 'bookmark' -and $_.innertext -like "*powershell*"} | `
Select-Object @{n='Topic';e={$_.InnerText}}, @{n='URL';e={$_.href}}
}
Catch
{
Break
}
$WebRequest = $null
$PageNumber++
}
$Data
}
$Articles = Get-BlogArticle -URL 'Geekeefy.wordpress.com'
Write-Verbose "$($Articles.count) Articles found & extracted."
Write-Verbose "Selecting $Count random articles."
$HashtagKeywords = "Powershell","Automation","Troubleshooting","DataExtraction","DataWrangling","Exchange","Google","Microsoft","PSTip","Tip","WebScraping","WhatILearnedToday"
$Articles| Get-Random -Count $Count | `
ForEach-Object {
$Topic = $_
$words= Foreach($Word in $Topic.Topic.Split(" ")) {
if($Word -in $HashtagKeywords) {
"#$word"
}
else {
$word
}
}
$TweetContent = $($words -join ' ')+"`n$($Topic.URL| Get-ShortURL -Verbose)"
If($TweetContent.length -le 140)
{
$Tweet = @{
ConsumerKey = 'ConsumerKey';
ConsumerSecret = 'ConsumerSecret';
AccessToken = 'AccessToken';
AccessSecret = 'AccessSecret';
Tweet = $TweetContent;
}
Write-Verbose "Tweeting the text > $TweetContent ."
Publish-Tweet @Tweet
Write-Verbose "Done."
}
Write-Verbose "Waiting for $IntervalInMins mins until next Tweet."
Start-Sleep -Seconds (60*$IntervalInMins)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment