Invoke-BlogArchive.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<#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)
}