Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active February 7, 2019 19:35
Show Gist options
  • Save PrateekKumarSingh/2357781fb763657efa112ac12da6f2fd to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/2357781fb763657efa112ac12da6f2fd to your computer and use it in GitHub Desktop.
function Get-CustomDate
{
param(
[Parameter(Mandatory = $true)]
[string]$DateString,
[string]$DateFormat = 'ddd MMM d HH:mm:ss yyyy',
[cultureinfo]$Culture = $(Get-UICulture)
)
# replace double space by a single one
$DateString = $DateString -replace '\s+', ' '
[Datetime]::ParseExact($DateString, $DateFormat, $Culture)
}
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=YOUR API KEY" `
-Body $(''| Select-object @{n='longUrl';e={$URL}}|convertto-json) `
-ContentType 'application/json' `
-Method Post | ForEach-Object ID
}
Function Get-RSSFeed
{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)] [uri] $URL
)
Write-Host "Fetching RSS Feeds: $URL" -ForegroundColor Green
$article = @()
$page_num=1
$flag=$true
While($true -and $flag){
try{
$feed_url = ([xml](Invoke-WebRequest "$URL/feed")).rss.channel.link.where({$_.rel -eq 'self'}).href
if(([uri]$feed_url).localpath -like "*feed.xml"){
$xml = [xml] ((Invoke-WebRequest "$feed_url").content)
$flag= $false
}
else{
Write-Host " [+] $feed_url/?paged=$page_num" -ForegroundColor Green
$xml = [xml] ((Invoke-WebRequest "$feed_url/?paged=$page_num").content)
if($xml){
Foreach ($item in $xml.rss.channel.item)
{
$date = Get-CustomDate -DateString $($item.pubDate -replace " \+0000", "").trim() -DateFormat 'ddd, d MMM yyyy HH:mm:ss'
try{
$article += [PSCustomObject] @{
date = $date
author = $item.creator.'#cdata-section'
title = $item.title
num_comments = ($item.comments)[1]
link = (Get-ShortURL $item.link)
category = $item.category.'#cdata-section'#.where({$_ -ne 'Uncategorized'})
description = $item.description.'#cdata-section'
commentsURL = ($item.comments)[0]
}
}
catch{
$_.exception.message
}
}
}
}
}catch{
break
}
$page_num=$page_num+1
}
return $article
}
import-module PoshTwit
Get-process pwsh | Where-Object MainWindowTitle -eq 'wp-to-twitter' | Stop-Process -Force
$Host.UI.RawUI.WindowTitle = 'wp-to-twitter'
$Articles = Get-RSSFeed -URL 'RidiCurious.com'
$HashtagKeywords = "Powershell","Python","AWS","Azure"
ForEach($blogpost in ($Articles|Get-Random))
{
$Title = $blogpost.Title
$words= Foreach($Word in $Title.Split(" ")) {
if($Word -in $HashtagKeywords) {
"#$word"
}
else {
$word
}
}
$TweetContent = $($words -join ' ')+"`n$($Blogpost.link)"
If($TweetContent.length -le 140)
{
$Tweet = @{
ConsumerKey = 'XXX';
ConsumerSecret = 'XXX';
AccessToken = 'X-XX';
AccessSecret = 'XXXX';
Tweet = $TweetContent;
}
Publish-Tweet @Tweet
Write-Verbose "Done."
}
$hours = (10..12 | Get-Random)
Write-host "Sleeping for $hours hours until next Tweet." -ForegroundColor Magenta
Start-Sleep -Seconds (60*60*$hours) -Verbose
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment