Jeshii’s PowerShell Gateway (for Twitter)
I’m writing a script as a PowerShell <-> Twitter gateway. Basically, I want to make it so some of the automatic English tweets sent from sites like nike+ or yelp can have Japanese equivalents that would get automatically posted in both languages. It’s a project. It will be documented here.
Over the course of 9/23 to 9/24/2010, I wrote the following (it is altered to hide my directories, consumer key, and relevant twitter accounts). While this is supposed to do both languages, I can’t get the Japanese working. It works fine with regular characters but doesn’t with Japanese. Oh well. See how complicated a little script to grab tweets, replace text, and post tweets is now?!?! BTW, the OAuth library I’m using is here.
Add-Type -Path PS:\Libraries\DevDefined.OAuth.dll
start-transcript -path PS:\logs\twitter-logs.txt -append
[System.Net.ServicePointManager]::Expect100Continue = $false
# create context (provide correct keys)
$cons = New-Object devdefined.oauth.consumer.oauthconsumercontext
$cons.ConsumerKey = '[get your own]'
$cons.ConsumerSecret = '[get your own]'
$cons.SignatureMethod = [devdefined.oauth.framework.signaturemethod]::HmacSha1
# create session; I pass $null and not full urls. It looks weird, maybe
# there is a more elegant way how to create the session
$session = new-object DevDefined.OAuth.Consumer.OAuthSession $cons, $null, $null, $null
# create access token for first account and fill its data
$accessToken = new-object DevDefined.OAuth.Framework.TokenBase
$at = import-cliXml PS:\Tokens\TwitterAccessToken.clixml #first accounts's key
$accessToken.ConsumerKey, $accessToken.Realm, $accessToken.Token, $accessToken.TokenSecret = $at.ConsumerKey, $at.Realm, $at.Token, $at.TokenSecret
# create access token for second account and fill-in its data
$accessTokenJ1 = new-object DevDefined.OAuth.Framework.TokenBase
$atJ1 = import-cliXml PS:\Tokens\TwitterAccessToken-second.clixml #second accounts's key
$accessTokenJ1.ConsumerKey, $accessTokenJ1.Realm, $accessTokenJ1.Token, $accessTokenJ1.TokenSecret = $atJ1.ConsumerKey, $atJ1.Realm, $atJ1.Token, $atJ1.TokenSecret
while(1 -eq 1) {
# finally, create request and read response
$req = $session.Request($accessToken)
$req.Context.RequestMethod = 'GET'
$req.Context.RawUri = [Uri]'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=firstacccount&count=1'
$res = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($req)
$text=$res.statuses.status.text
Write-Output (Get-Date -format s) "Checking First Account:"
if (!$url) {
#check second account for recent nike tweets
$reqEN = $session.Request($accessTokenJ1)
$reqEN.Context.RequestMethod = 'GET'
$reqEN.Context.RawUri = [Uri]"http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=secondaccount"
$resEN = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($reqEN)
foreach ($TwitString in $resEN.Statuses.status) {
if ((!$url) -and $TwitString.text.Contains("http://go.nike.com")) {
$url = $TwitString.text.substring($TwitString.text.Length-27)
}
}
}
if ($text.Contains("http://go.nike.com") -and !($text.Contains($url))) {
#replace text
$newtextEN = $text.Replace("ran","I rode my bicycle")
#grab URL
$url = $text.substring($text.Length-27)
#Debug output
Write-Output $newtextEN
#two different URI encoding schemes. Which one to use???
#$newtextENURL=[system.Web.Httputility]::UrlEncode($newtextEN)
$newtextENURL=[System.Uri]::EscapeDataString($newtextEN)
# finally, create request and read response
$reqEN = $session.Request($accessTokenJ1)
$reqEN.Context.RequestMethod = 'POST'
$reqEN.Context.RawUri = [Uri]"http://api.twitter.com/1/statuses/update.xml?status=$newtextENURL"
$resEN = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($reqEN)
Write-Output $resEN
<#Japanese version
#$newtextJPURL="テスト"
# finally, create request and read response
$reqJP = $session.Request($accessTokenJ2)
$reqJP.Context.RequestMethod = 'POST'
$reqJP.Context.RawUri = [Uri]"http://api.twitter.com/1/statuses/update.xml?status=テスト"
$resJP = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($reqJP)
Write-Output $resJP #>
}
Start-Sleep -s (60*15)
}
1 Comment »

By dmace on Jan 25, 2011 | Reply
Thanks! This script fixed my post-issues with OAuth.