Advertisement
Guest User

keyToPandora

a guest
Aug 29th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ErrorActionPreference = "Continue"
  2.  
  3. function wait-ie {
  4.     do {
  5.         Start-Sleep -Milliseconds 5000
  6.         }
  7.     while($ie.Busy)
  8.     }
  9.  
  10. function downloadFile($url, $targetFile) {
  11.     $uri = New-Object "System.Uri" "$url"
  12.     $request = [System.Net.HttpWebRequest]::Create($uri)
  13.     $request.set_Timeout(15000) #15 second timeout
  14.     $response = $request.GetResponse()
  15.     $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
  16.     $responseStream = $response.GetResponseStream()
  17.     $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
  18.     $buffer = new-object byte[] 10KB
  19.     $count = $responseStream.Read($buffer,0,$buffer.length)
  20.     $downloadedBytes = $count
  21.     while ($count -gt 0) {
  22.         $targetStream.Write($buffer, 0, $count)
  23.         $count = $responseStream.Read($buffer,0,$buffer.length)
  24.         $downloadedBytes = $downloadedBytes + $count
  25.         Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength)  * 100)
  26.         }
  27.     Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
  28.     $targetStream.Flush()
  29.     $targetStream.Close()
  30.     $targetStream.Dispose()
  31.     $responseStream.Dispose()
  32.     }
  33.  
  34. function next-song {
  35.     do {
  36.         Start-Sleep -Milliseconds 5000
  37.         If($ie.Document.getElementById("still_listening_ignore") -ne $null) {
  38.             $ie.Document.getElementById("still_listening_ignore").click()
  39.             }
  40.         }
  41.     while($songName -eq ($ie.Document.body.getElementsByClassName("songTitle") | Select-Object -ExpandProperty innerText))
  42.     If($ie.Document.getElementById("still_listening_ignore") -ne $null) {
  43.         $ie.Document.getElementById("still_listening_ignore").click()
  44.         }
  45.     $songName = $ie.Document.body.getElementsByClassName("songTitle") | Select-Object -ExpandProperty innerText
  46.     $songArtist = $ie.Document.body.getElementsByClassName("artistSummary") | Select-Object -ExpandProperty innerText
  47.     # element ID is jp_audio_1 for Pandora One
  48.     $songDownload = $ie.Document.getElementById("jp_audio_0") | Select-Object -ExpandProperty src
  49.     $filePath = $directory + $songArtist + ' - ' + $songName + '.m4a'
  50.     If(!(Test-Path $filePath)) {
  51.         Write-Host "Downloading $songName by $songArtist" -ForegroundColor Green
  52.         downloadFile -url $songDownload -targetFile $filePath
  53.         }
  54.     Else {
  55.         Write-Host "Already downloaded $songName by $songArtist" -ForegroundColor Yellow
  56.         }
  57.     next-song
  58.     }
  59.  
  60. $directory = "<directory>\" # must have a slash at the end
  61. $height = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Height
  62. $width = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width/2
  63. $ie = new-object -com "InternetExplorer.Application"
  64. $ie.navigate("http://www.pandora.com/")
  65. wait-ie
  66. $ie.visible = $true
  67. $ie.Top = 0
  68. $ie.Left = $width.ToString()
  69. $ie.Width = $width.ToString()
  70. $ie.Height = $height.ToString()
  71. next-song
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement