PoshCode Archive  Artifact [e76c010a5e]

Artifact e76c010a5ea9b46f398387511f05c78a2fb7f64399d4132bad04336b37d1bfa3:

  • File Get-TV-Show-Airdates.ps1 — part of check-in [6901c990fe] at 2018-06-10 13:53:50 on branch trunk — These functions retrieve information about tv show airdates. They are used as a part of my “home automation with powershell”-project. (user: DollarUnderscore size: 7704)

# encoding: ascii
# api: powershell
# title: Get TV Show Airdates
# description: These functions retrieve information about tv show airdates. They are used as a part of my “home automation with powershell”-project.
# version: 0.1
# type: function
# author: DollarUnderscore
# license: CC0
# function: Get-TVShowNextAirDate
# x-poshcode-id: 5351
# x-archived: 2017-04-01T02:48:56
# x-published: 2017-08-04T08:23:00
#
# It’s mostly just for fun but it’s currently used to order snacks (See “Buy groceries with PowerShell”-post) when a TV Show I follow starts/ends soon.
# A short post about these functions are available at:
# http://dollarunderscore.azurewebsites.net/?p=2911
#
function Get-TVShowNextAirDate
{
    <#
    .Synopsis
       Retrieves information about tv shows next air date
    .DESCRIPTION
       This cmdlet parses pogdesign's tv calendar for tv show air dates
    .EXAMPLE
       Get-TVShowNextAirDate
    .EXAMPLE
       Get-TVShowNextAirDate | Format-Table
    .EXAMPLE
       Get-TVShowNextAirDate | Where-Object { $_.TVShow -eq "Family Guy" }
    #>

    [CmdletBinding()]
    param()

    Begin {
        # Get the site
        $URL = "http://www.pogdesign.co.uk/cat/next-airing"
        try {
            $CatTV = Invoke-WebRequest -Uri $URL -UseBasicParsing -ErrorAction Stop
        }
        catch {
            Write-Error "Failed to fetch TV Show data."
            return
        }

        $TVShows = $CatTV.Content -split "<div class=`"showlist" | Select-Object -Skip 1
    }
    Process {
        foreach ($TVShow in $TVShows) {

                $TVShowName = ((($TVShow -split "<a href")[1] -split "</a>")[0] -split ">")[1] -replace "&amp;","&"
                $EpisodeName = ((($TVShow -split "<a href")[2] -split "</a>")[0] -split ">")[1] -replace "&amp;","&"
                $Season = (((((($TVShow -split "<a href")[2] -split "</a>")[0] -split ">")[0] -split "season-")[1]) -split "/Episode-")[0]
                $Episode = (((((($TVShow -split "<a href")[2] -split "</a>")[0] -split ">")[0] -split "season-")[1]) -split "/Episode-")[1] -replace '"$'
                $AirDateString = (($TVShow -split "epdate`">")[1] -split "</span>")[0]
                $Year = "20" + (($AirDateString -split " ")[2] -replace "^'")
                $MonthString = ($AirDateString -split " ")[1]
                $Day = ($AirDateString -split " ")[0] -replace "[^\d]"

                $Month = switch ($MonthString)
                                {
                                    "Jan" {  "1" }
                                    "Feb" {  "2" }
                                    "Mar" {  "3" }
                                    "Apr" {  "4" }
                                    "May" {  "5" }
                                    "Jun" {  "6" }
                                    "Jul" {  "7" }
                                    "Aug" {  "8" }
                                    "Sep" {  "9" }
                                    "Oct" { "10" }
                                    "Nov" { "11" }
                                    "Dec" { "12" }

                                }
                $AirTime = ((($TVShow -split "eptime`">")[1] -split "</span>")[0] -split " ")[0]
                $AirTimeAndDate = Get-Date "$Year-$Month-$Day $AirTime"

                $returnObject = New-Object System.Object
                $returnObject | Add-Member -Type NoteProperty -Name TVShow -Value $TVShowName
                $returnObject | Add-Member -Type NoteProperty -Name EpisodeName -Value $EpisodeName
                $returnObject | Add-Member -Type NoteProperty -Name Season -Value $Season
                $returnObject | Add-Member -Type NoteProperty -Name Episode -Value $Episode
                $returnObject | Add-Member -Type NoteProperty -Name AirDate -Value $AirTimeAndDate

                Write-Output $returnObject

                # Cleanup
                Remove-Variable TVShowName, EpisodeName, Season, Episode, AirTimeAndDate, AirDateString, Year, MonthString, Month, Day, AirTime, returnObject
        }
    }

    End { }
}

function Get-TVShowAirDate
{
    <#
    .Synopsis
       Retrieves information about a specific tv show's air dates
    .DESCRIPTION
       This cmdlet parses pogdesign's tv calendar for a specific tv show's air dates
    .EXAMPLE
       Get-TVShowAirDate
    .EXAMPLE
       Get-TVShowAirDate | Format-Table
    .EXAMPLE
       Get-TVShowNextAirDate | Get-TVShowAirDate | ft
    #>

    [CmdletBinding()]

    param([Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
          [string] $TVShow)

    Begin { }

    Process {

        $URL = "http://www.pogdesign.co.uk/cat/$($TVShow -replace ' ','-' -replace "&","and" -replace '[^a-zA-Z\d-]')-summary"

        try {
            $TVShowInfo = Invoke-WebRequest -Uri $URL -UseBasicParsing
        }
        catch {
            Write-Error "Failed to fetch TV Show data."
            return
        }

        $TVShowEpisodeSpans = (($TVShowInfo.Content -split "Next Airing Episode Dates")[1] -split "Back to the TV Calendar")[0] -split "<div class=`"" | select -Skip 1

        foreach ($TVShowEpisodeSpan in $TVShowEpisodeSpans) {
            $EpisodeName = ((($TVShowEpisodeSpan -split "<a href=`"")[1] -split "`">")[1] -split "</A>")[0] -replace "&amp;","&"
            $Season = (((((($TVShowEpisodeSpan -split "<a href")[1] -split "</a>")[0] -split ">")[0] -split "season-")[1]) -split "/Episode-")[0]
            $Episode = (((((($TVShowEpisodeSpan -split "<a href")[1] -split "</a>")[0] -split ">")[0] -split "season-")[1]) -split "/Episode-")[1] -replace '"$'
            $AirDateString = (($TVShowEpisodeSpan -split "epdate`">")[1] -split "</span>")[0]
            $Year = "20" + (($AirDateString -split " ")[2] -replace "^'")
            $MonthString = ($AirDateString -split " ")[1]
            $Day = ($AirDateString -split " ")[0] -replace "[^\d]"

            $Month = switch ($MonthString)
                            {
                                "Jan" {  "1" }
                                "Feb" {  "2" }
                                "Mar" {  "3" }
                                "Apr" {  "4" }
                                "May" {  "5" }
                                "Jun" {  "6" }
                                "Jul" {  "7" }
                                "Aug" {  "8" }
                                "Sep" {  "9" }
                                "Oct" { "10" }
                                "Nov" { "11" }
                                "Dec" { "12" }

                            }
            $AirTime = ((($TVShowEpisodeSpan -split "eptime`">")[1] -split "</span>")[0] -split " ")[0]
            $AirTimeAndDate = Get-Date "$Year-$Month-$Day $AirTime"

            $returnObject = New-Object System.Object
            $returnObject | Add-Member -Type NoteProperty -Name TVShow -Value $TVShow
            $returnObject | Add-Member -Type NoteProperty -Name EpisodeName -Value $EpisodeName
            $returnObject | Add-Member -Type NoteProperty -Name Season -Value $Season
            $returnObject | Add-Member -Type NoteProperty -Name Episode -Value $Episode
            $returnObject | Add-Member -Type NoteProperty -Name AirDate -Value $AirTimeAndDate

            Write-Output $returnObject

            # Cleanup
            Remove-Variable EpisodeName, Season, Episode, AirTimeAndDate, AirDateString, Year, MonthString, Month, Day, AirTime, returnObject
        }
    }

    End {  }
}