Invoke-BlogArchive.ps1

<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 42d17be2-aed7-4523-a782-459c0f036eeb
 
.AUTHOR PrateekSingh
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Powershell Twitter API Automation
 
.LICENSEURI
 
.PROJECTURI https://geekeefy.wordpress.com/2017/03/27/automating-from-the-blog-archives-tweets-using-powershell/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES PoshTwit
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Script to web data scrape blog articles and tweet them on twitter with Topic, a short URL and appropriate hashtags in a set interval.
 
#>
 


[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=APIKEY" `
                      -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)
}