Compartilhar via


RSS2Twitter PowerShell script

As you might have noticed I’ve been using Twitter quite often lately. On my previous weblog I installed a Drupal Twitter module for updating Twitter with my latest blog posts. But now I migrated to Technet Blogs I didn’t had this possibility anymore, so I looked for an easy way to have my latest blog posts updated to Twitter.

I searched for “rss2twitter” on Windows Live Search and found a perl script but that didn’t work for me. It just failed with the next errors:

“C:\Temp\rss2twitter>rss2twitter.exe
Malformed UTF-8 character (1 byte, need 4, after start byte 0xf1) at C:\Temp\rss
2twitter\rss2twitter.exe line 128.”

So I though why not use PowerShell? With the help of Out-Twitter from Doug Finke it was quite easy.

So here is the PowerShell RSS2Twitter script:

# Rss2Twitter PowerShell Script
# Stefan Stranger 2008
# Retrieve last post from RSS feed and post to Twitter Account
# Out-Twitter function from Doug Finke (https://dougfinke.com/blog/?p=236)

function Send-TwitterStatus($status)
{

    # Enter Twitter Username and Password
    $userName = "twitteraccount"
    $password = "password"

    $url = "https://twitter.com/statuses/update.xml"
    $data = "status={0}" -f $status

    $request = [Net.WebRequest]::Create($url)
    $request.Credentials = New-Object System.Net.NetworkCredential($userName, $password);
    $request.ContentType = "application/x-www-form-urlencoded";
    $request.Method = "POST";

    $bytes = [System.Text.Encoding]::UTF8.GetBytes($data)

    $request.ContentLength = $bytes.Length

    $requestStream = [System.IO.Stream]$request.GetRequestStream()
    $requestStream.write($bytes, 0, $bytes.Length)

    $response = $request.GetResponse()
    $reader = [System.IO.StreamReader]$response.GetResponseStream()

    ([xml]$reader.ReadToEnd()).status
}

#Enter rss feed from where you want update Twitter
$uri=https://enterhere.your.rssfeed
$wc = New-Object Net.WebClient
$rss = [xml]$wc.DownloadString($uri)
$posts = $rss.rss.channel.item | Select-Object title, link
#Retrieve last post on RSS feed page.
$lastpost = $posts[0].title + ' ' + $posts[0].link

#Post to Twitter
Send-TwitterStatus $lastpost

rss2twitter.txt

Comments

  • Anonymous
    January 01, 2003
    I just got a Tweet from jpavleck (www.twitter.com/jpavleck) and he pointed me to Twitterfeed.com. Why didn't I find this earlier ;-) Maybe gonna use this for the MPNotifier Twitter account...