Download Build (2017) decks and video

Update: Modified the script to handle multiple instances but pay heed to the warning here.

Similar to last year, I have a PowerShell script that will allow you to download the various PowerPoint decks and videos to watch locally rather than stream. This makes some improvements from the earlier scripts (e.g. if a file is already downloaded it will skip downloading it again) and does the following:

  • Creates the relevant folder which includes the Session details (including the Title, and the Presenters)
  • For each session, saves the description in a text file in the created folder.
  • Downloads the relevant presentation (if any)
  • Downloads a jpg which shows the image session – sometimes it is easier just to see the title slide. I thought better to have it and not use it, than the other way.
  • And finally downloads the high-quality video of that session.

In the script, you can change the following (and if you understand Build then this should be easy):

  • Change the path where to download this to (default is d:\build)
  • Choose a lower quality video if you prefer (which of course takes less space and might not be bad depending on which device you are seeing). Of course this also uses less bandwidth.
  • Of course. And if you want only the decks, then you can comment out parts of the script where it doesn’t download the video.

The script will spit out some basic errors and will ‘eat’ some of the exceptions that are expected (e.g. every session doesn’t have a pptx or a video). That won’t break the script, it will just move to the next session.

And finally here is the script:

# First setup the folder where to download using the parameters outlined below.
# Second, loop through and get the decks first
# Third. loop through and get the videos last
# Note: IF you don't want to download the videos, and want only the pptx then comment the section later in the script

# parameters
[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath 
$rss = (new-object net.webclient)

#Filenames might get long, so keep this short!
$downloadlocation = "D:\build"

	if (-not (Test-Path $downloadlocation)) { 
		Write-Host "Folder $fpath dosen't exist. Creating it..."  
		New-Item $downloadlocation -type directory 
	}
set-location $downloadlocation

# Grab the RSS feed - Build 2017
$a = ($rss.downloadstring("http://s.ch9.ms/events/build/2017/rss/mp4high")) 
$b = ($rss.downloadstring("http://s.ch9.ms/events/build/2017/rss/slides")) 

# Video quality default is high; you can select regular (mp4) or lower quality (mp3)
#$a = ($rss.downloadstring("http://s.ch9.ms/events/build/2017/rss/mp4")) 
#$a = ($rss.downloadstring("http://s.ch9.ms/events/build/2017/rss/mp3")) 


# ********** download the decks **********
try { 

    foreach($item in $b.rss.channel.item) {   
	    $code = $item.comments.split("/") | select -last 1	   
	
	    # Get the url for the pptx file
	    $urlpptx = New-Object System.Uri($item.enclosure.url)  
        $urljpg = New-Object System.Uri($item.thumbnail.url)

        # make the filename readable
        $filepptx = $code + "-" + $item.creator + " - " + $item.title.Replace(":", "-").Replace("?", "").Replace("/", "-").Replace("<", "").Replace("|", "").Replace('"',"").Replace("*","").Replace("’","'").Replace("â€","")
	    $filepptx = $filepptx.substring(0, [System.Math]::Min(120, $filepptx.Length))
	    $filepptx = $filepptx.trim()
        $filejpg = $filepptx

	    $filepptx = $filepptx + ".pptx" 
        $filejpg = $filejpg + "_960.jpg"

    
	    if ($code -ne "") {
		     $folder = $code + " - " + $item.title.Replace(":", "-").Replace("?", "").Replace("/", "-").Replace("<", "").Replace("|", "").Replace('"',"").Replace("*","").Replace("’","'").Replace("â€","")
		     $folder = $folder.substring(0, [System.Math]::Min(100, $folder.Length))
		     $folder = $folder.trim()
	    }
	    else {
		    $folder = "NoCodeSessions"
	    }
	
	    if (-not (Test-Path $folder)) { 
		    Write-Host "Folder $folder dosen't exist. Creating it..."  
		    New-Item $folder -type directory 
	    }
	
	    # Make sure the PowerPoint file doesn't already exist
	    if (!(test-path "$downloadlocation\$folder\$filepptx")) { 	
		    # Echo out the file that's being downloaded
		    $filepptx
		    $wc = (New-Object System.Net.WebClient)  

		    # Download the pptx file
		    Invoke-WebRequest $urlpptx -OutFile $downloadlocation\$filepptx

            # download the jpg but don't want to break if this doesn't exist; hence the nested try blocks
            try {
                if (!(test-path "$downloadlocation\$filejpg")) { 	
                    $wc.DownloadFile($urljpg, "$downloadlocation\$filejpg")
                }
            }
            catch {
                Write-Host "Jpeg $filejpg doesn't exist ... eating the exception and moving on ..."
            }
        
		    mv $filepptx $folder 
            mv $filejpg $folder 
	    } #endif
        else {
            Write-Host "PPTX: $filepptx exist; skipping download."  
        }
	} #end-loop foreach

    Write-host "*************** Downloading all the decks complete ***************"
}
catch
{
    Write-host "Oops, could not find any slides."
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-host "\t" $ErrorMessage + "\n" + $FailedItem
}

# ********** download the videos **********
# if you don't want the video but only the slides just comment all the code below in the foreach loop
try { 
    foreach($item in $a.rss.channel.item) {
	    $code = $item.comments.split("/") | select -last 1	   
	
	    # Grab the URL for the MP4 file
	    $url = New-Object System.Uri($item.enclosure.url)  
	
	    # Create the local file name for the MP4 download
	    $file = $code + "-" + $item.creator + "-" + $item.title.Replace(":", "-").Replace("?", "").Replace("/", "-").Replace("<", "").Replace("|", "").Replace('"',"").Replace("*","").Replace("’","'").Replace("â€","")
	    $file = $file.substring(0, [System.Math]::Min(120, $file.Length))
	    $file = $file.trim()
	    $file = $file + ".mp4"  
	
	    if ($code -ne "")
	    {
		     $folder = $code + " - " + $item.title.Replace(":", "-").Replace("?", "").Replace("/", "-").Replace("<", "").Replace("|", "").Replace('"',"").Replace("*","").Replace("’","'").Replace("â€","")
		     $folder = $folder.substring(0, [System.Math]::Min(100, $folder.Length))
		     $folder = $folder.trim()
	    }
	    else
	    {
		    $folder = "NoCodeSessions"
	    }
	
	    if (-not (Test-Path $folder)) { 
		    Write-Host "Folder $folder doesn't exist. Creating it..."  
		    New-Item $folder -type directory 
	    }
		
	
	    # Make sure the MP4 file doesn't already exist
	    if (!(test-path "$folder\$file"))     
	    { 	
		    # Echo out the  file that's being downloaded
		    $file
		    
		    # Download the MP4 file
		    try{
				if (!(test-path "$downloadlocation\$file"))
				{
					Invoke-WebRequest $url -OutFile $downloadlocation\$file
		    
					#move it from the current working folder to the target
					mv $file $folder
				}
				else {
					Write-Host "Video: $file - anoter process possibly working on this; skipping download."
				}
			}
			catch {
				$ErrorMessage = $_.Exception.Message
				$FailedItem = $_.Exception.ItemName
				Write-host "\t" $ErrorMessage + "\n" + $FailedItem
			}
	    }
        else {
            Write-Host "Video: $file exist; skipping download."  
        }

        #Try and get the Sessions text description
        try {
	        $OutFile = New-Item -type file "$($downloadlocation)\$($Folder)\$($Code.trim()).txt" -Force  
            $Content = ""
            $Content = $item.title.ToString().trim() + "`r`n" + $item.creator + "`r`n" + $item.summary.ToString().trim() + "`r`n" + "`r`n" + $item.description.ToString().trim() + "`r`n" + "`r`n" + $item.comments.ToString().trim() 
            add-content $OutFile $Content
        }
        catch {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            Write-host "\t" $ErrorMessage + "\n" + $FailedItem
        }
		
    } #end-loop foreach
}
catch
{
    Write-host "Oops, could not find any videos or some other error happened."
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-host "\t" $ErrorMessage + "\n" + $FailedItem
} # ********** End - download the videos section **********

Write-host "*************** All Done! ***************"

If you read through the script it is pretty self explanatory.

Author: Amit Bahree

This blog is my personal blog and while it does reflect my experiences in my professional life, this is just my thoughts. Most of the entries are technical though sometimes they can vary from the wacky to even political – however that is quite rare.

Quite often, I have been asked what’s up with the “gibberish” and the funny title of the blog? Some people even going the extra step to say that, this is a virus that infected their system (ahem) well. [:D] It actually is quite simple, and if you have still not figured out then check out this link – whats in a name?

Leave a Reply