Last active
August 18, 2020 23:15
-
-
Save ghotz/b24c1d6f74155bec0653ec5d69d251ac to your computer and use it in GitHub Desktop.
Save a Relive video given the URL for an activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Save a Relive video given the URL for an activity | |
| .DESCRIPTION | |
| Quick hack to save Relive videos from the URL sent via e-mail after creating it. | |
| Along with the video, a file with .url extension is created with the original URL for future reference. | |
| Filenames are standardized replacing invalid characters with _ and using the following name template: | |
| YYYYmmdd - title of the video.{mp4|url} | |
| Please note that old videos need to be re-created by Relive, requesting the page triggers the re-creation | |
| and the script has a retry logic but in some cases it will still fail after several retries because the | |
| page still dont have the URL in og:video even after the video has been re-created | |
| .PARAMETER ReliveURI | |
| URL to the page of a Relive activity, this is usually sent via e-mail after creating it. | |
| .PARAMETER DownloadDir | |
| Path where the video and .url are to be saved | |
| .PARAMETER RetrySeconds | |
| How many seconds to wait between retries between 29 and 60, default is 20. | |
| .PARAMETER MaxRetries | |
| Maximum number of retry attempts between 1 and 10, default is 6. | |
| .EXAMPLE | |
| save-relive-video.ps1 -DownloadDir "F:\Videos\Temp\Relive\2018" -ReliveURI "https://www.relive.cc/view/vYvrpP27L6P" | |
| #> | |
| param | |
| ( | |
| [string]$ReliveURI, | |
| [string]$DownloadDir, | |
| [ValidateRange(20,60)] | |
| [int]$RetrySeconds = 20, | |
| [ValidateRange(1,10)] | |
| [int]$MaxRetries = 6 | |
| ); | |
| Write-Information "Processing $ReliveURI, saving in $DownloadDir" | |
| $TryNum = 1 | |
| while ($TryNum -le $MaxRetries) { | |
| $Response = Invoke-WebRequest -Uri $ReliveURI -UseBasicParsing -DisableKeepAlive -Headers @{"Cache-Control"="no-cache"} | |
| if ($Response.StatusCode -ne 200) { | |
| Write-Error "Couldn't open $ReliveURI." | |
| exit | |
| } | |
| if ($Response.RawContent -match '<meta\ property="og:video"\ content="(?<uri>.*?)"/>') { | |
| $Uri = $Matches['uri'] | |
| if ($Uri -like "*undefined*") { | |
| # video needs to be re-freshed, meanwhile fetch new URI (continue with old if not found) | |
| if ($Response.RawContent -match '<meta\ property="og:url"\ content="(?<uri>.*?)"/>') { | |
| $ReliveURI = $Matches['uri'] | |
| } | |
| Write-Warning "Video is too old and needs to be refreshed by Relive! Attempt $TryNum of $MaxRetries at waiting $RetrySeconds seconds before retrying with new URL $ReliveURI." | |
| Start-Sleep -Seconds $RetrySeconds | |
| $TryNum++ | |
| } | |
| else { | |
| break | |
| } | |
| } | |
| else { | |
| Write-Error "Couldn't find og:video property, page needs to be analyzed and script changed." | |
| exit | |
| } | |
| } | |
| if ($TryNum -gt $MaxRetries) { | |
| Write-Error "Video still not ready after retrying, please try again later." | |
| exit | |
| } | |
| if ($Response.RawContent -match '<title>(?<title>.*?)</title>') { | |
| $title = [System.Web.HttpUtility]::HtmlDecode($Matches['title']) | |
| $title = $title -replace "'", "" -replace "Relive ", "" | |
| } | |
| else { | |
| Write-Error "Couldn't parse video title, page needs to be analyzed and script changed." | |
| exit | |
| } | |
| if ($Response.RawContent -match '"taken_at":"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}).*?"') { | |
| $videodate = $Matches['year'] + $Matches['month'] + $Matches['day'] | |
| } | |
| else { | |
| if ($Response.RawContent -match '"moment_at":"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}).*?"') { | |
| $videodate = $Matches['year'] + $Matches['month'] + $Matches['day'] | |
| } | |
| else { | |
| if ($Response.RawContent -match '<span\ class="activity-by">(?<athlete>.*?)</span><span>.*?(?<literaldate>\d{2}.*?\d{4}).*?</span>') { | |
| $videodate = ([datetime]$Matches['literaldate']).ToString('yyyyMMdd') | |
| } | |
| else { | |
| Write-Error "Couldn't parse video date, page needs to be analyzed and script changed." | |
| exit | |
| } | |
| } | |
| } | |
| $filename = $videodate + ' - ' + ($title.Split([IO.Path]::GetInvalidFileNameChars()) -join "_") | |
| Write-Verbose "Saving $Uri to $DownloadDir" | |
| $Response = Invoke-WebRequest -Uri $Uri -UseBasicParsing -OutFile (Join-Path $DownloadDir ($filename + ".mp4")) | |
| Write-Verbose "Writing .url file" | |
| Set-Content -Path (Join-Path $DownloadDir ($filename + ".url")) -Value "[InternetShortcut]`r`nURL=$ReliveURI" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment