PoshCode Logo PowerShell Code Repository

Twitbrain cheat by Stefan Stranger 5 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/910"></script>download | new post

#Twitbrain Cheat PowerShell script #Description: PowerShell script to beat everyone at the Twitter twitbrain game

  1. For more info follow @twitbrain at www.twitter.com
#Change the Twitter Username and Password in the script. #Author: Stefan Stranger #Website: http://tinyurl.com/sstranger #Date: 03/07/2009 #Version: 0.1 #Function Publish-Tweet from James O’Neills blog (http://blogs.technet.com/jamesone/archive/2009/02/16/how-to-drive-twitter-or-other-web-tools-with-powershell.aspx)

  1. #Twitbrain Cheat PowerShell script
  2. #Description: PowerShell script to beat everyone at the Twitter twitbrain game
  3. #             For more info follow @twitbrain at www.twitter.com
  4. #Change the Twitter Username and Password in the script.
  5. #Author: Stefan Stranger
  6. #Website: http://tinyurl.com/sstranger
  7. #Date: 03/07/2009
  8. #Version: 0.1
  9. #Function Publish-Tweet from James O'Neills blog (http://blogs.technet.com/jamesone/archive/2009/02/16/how-to-drive-twitter-or-other-web-tools-with-powershell.aspx)
  10.  
  11.  
  12. [System.Reflection.Assembly]::LoadWithPartialName(System.Web") | Out-Null
  13.  
  14. Function Publish-Tweet([string] $TweetText)
  15. {
  16.         [System.Net.ServicePointManager]::Expect100Continue = $false
  17.         $request = [System.Net.WebRequest]::Create("http://twitter.com/statuses/update.xml")
  18.         $Username = "username"
  19.         $Password = "password"
  20.         $request.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
  21.         $request.Method = "POST"
  22.         $request.ContentType = "application/x-www-form-urlencoded"
  23.         write-progress "Tweeting" "Posting status update" -cu $tweetText
  24.        
  25.         $formdata = [System.Text.Encoding]::UTF8.GetBytes( "status="  + $tweetText  )
  26.         $requestStream = $request.GetRequestStream()
  27.                 $requestStream.Write($formdata, 0, $formdata.Length)
  28.         $requestStream.Close()
  29.         $response = $request.GetResponse()
  30.        
  31.         write-host $response.statuscode
  32.         $reader = new-object System.IO.StreamReader($response.GetResponseStream())
  33.                 $reader.ReadToEnd()
  34.         $reader.Close()
  35. }
  36.  
  37. Function Waiting()
  38. {
  39.         #Change $a if you want to wait longer or shorter
  40.         for ($a=15; $a -gt 1; $a--)
  41.         {
  42.         Write-Progress -Activity "Waiting for next poll" `
  43.         -SecondsRemaining $a -Status "Please wait."
  44.         Start-Sleep 1
  45.         }
  46. }
  47.  
  48. Write-Host "You are going to cheat;-)"
  49. $strResponse = Read-Host "Are you sure you want to continue? (Y/N)"
  50.  
  51. if ($strResponse -eq 'N')
  52.         {
  53.         Write-host "Maybe a good choice. It has to be a fair competition ;-)"
  54.         break
  55.         }
  56.  
  57. #infinite loop
  58. #Quit script by using Ctrl-C
  59. for (;;)
  60. {
  61.    #Retrieve sum from Twitbrain website
  62.    Write-host "Get calculation from Twitbrain website"
  63.         $ws = New-Object net.WebClient
  64.        
  65.         #Download Twitbrain website
  66.         $html = $ws.DownloadString("http://ajaxorized.com/twitbrain")
  67.        
  68.         #Save website content to temporarily file.
  69.         $currentdir = [Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath
  70.         $html | Set-Content "$currentdir\Twitbrain.html"
  71.  
  72.         $twitbrainpage = Get-Content "$currentdir\Twitbrain.html" | out-string
  73.        
  74.         #Search for calculation string in web page
  75.         $calc = [regex]::match($twitbrainpage,'(?<=\<div class="challenge"\>).+(?=\</div>
  76.                                         <p class="challenge-answer">)',"singleline").value.trim()
  77.        
  78.         #search/replace                                
  79.         $calc = $calc -replace "\*times\*","*"
  80.         $calc = $calc -replace "\+plus\+","+"
  81.         $calc = $calc -replace "\-minus\-","-"
  82.        
  83.         #Do the math on the sum
  84.         $result = invoke-expression $calc
  85.        
  86.         #Create tweet to post to twitter
  87.         $tweet = "@twitbrain " + $result
  88.  
  89.         #Post to Twitter
  90.         #Check if result has not been posted earlier.
  91.         $oldresult = Get-Content "$currentdir\oldresult.txt"
  92.         if ($result -eq $oldresult)
  93.         {
  94.                 write-host "No new Twitbrain question is published yet"
  95.         }
  96.         else
  97.         {
  98.                 Write-host "What is the result of the next question?"
  99.                 Write-host $calc
  100.                 Publish-Tweet $tweet
  101.                 write-host "Tweet publised"
  102.                 #Write old result to text file
  103.                 $oldresult = $result
  104.                 Write-host "Save oldresult to text file"
  105.                 $oldresult > "$currentdir\oldresult.txt"
  106.         }
  107.  
  108.         #Wait 15 seconds
  109.         #Call Waiting Function
  110.         Waiting
  111. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me