Last active
October 20, 2018 08:38
-
-
Save gardejo/5291c219a541ea5a6befb0761bbec70e to your computer and use it in GitHub Desktop.
An crafty command-line interface to open the page viewer of the Asahi Shimbun.
This file contains 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 | |
`asahi.ps1` : Open the latest issue, morning edition. | |
`asahi.ps1 ev` : Open the latest issue, evening edition. | |
`asahi.ps1 -issue 20180101 -edition ev` : Open the specified issue/edition. | |
`asahi.ps1 -duration -5 -edition ev` : Open the specified issue/edition. | |
.DESCRIPTION | |
An crafty command-line interface to open the page viewer of the Asahi Shimbun. | |
To run this script via a windows command shell script | |
to avoid the execution policy: | |
`powershell -ExecutionPolicy RemoteSigned -File %~dpn0.ps1` | |
.PARAMETER edition | |
An edition of the newspaper. | |
"" ( = morning ) or "ev" ( = evening ) edition. | |
.PARAMETER issue | |
An issue of the newspaper. | |
"" ( = latest ) or "yyyyMMdd" ( = specified ) issue. | |
.PARAMETER duration | |
An issue of the newspaper (N days ago). | |
It is allowed -14 to 0. | |
.PARAMETER help | |
Indicates this help. | |
.LINK | |
https://www.asahi.com/ | |
# .EXAMPLE | |
# asahi.ps1 | |
# .EXAMPLE | |
# asahi.ps1 ev | |
# .EXAMPLE | |
# asahi.ps1 -issue 20180101 | |
# .EXAMPLE | |
# asahi.ps1 -duration -14 | |
# .EXAMPLE | |
# asahi.ps1 -edition ev -issue 20180101 | |
# .EXAMPLE | |
# asahi.ps1 -help | |
#> | |
# Initializing... | |
param( | |
[AllowNull()] [AllowEmptyString()] [ValidateSet("", "ev")] [string] $edition = "", | |
[string] $issue, | |
[ValidateRange(-14, 0)] [int] $duration = 0, | |
[switch] $help | |
) | |
if ($help) { | |
Get-Help -Detailed $MyInvocation.MyCommand.Path | |
return | |
} | |
[DateTime] $date = [DateTime]::Now # The basis date. | |
if ($issue) { | |
[DateTime] $date = [DateTime]::ParseExact($issue, "yyyyMMdd", $null) | |
} | |
elseif (! $duration) { | |
# Detecting latest issue... | |
if ( | |
# A morning edition is published in 05:00 today. | |
( $edition -ne "ev" -and ( [int] $date.ToString("%H") -lt 5 ) ) -or | |
# An evening edition is published in 17:00 today. | |
( $edition -eq "ev" -and ( [int] $date.ToString("%H") -lt 17 ) ) | |
) { | |
# The latest issue is published in yesterday. | |
$duration -- | |
} | |
} | |
# If the specified publish date is Sunday... (It is not comply with holidays!) | |
if ( | |
$edition -eq "ev" -and | |
$date.AddDays($duration).ToString( | |
"ddd", [CultureInfo]::CreateSpecificCulture("en-US") | |
) -eq "Sun" | |
) { | |
# The latest issue is published in the preceding business day. | |
$duration -- | |
} | |
# Buld the issue... | |
$issue = $date.AddDays($duration).ToString("yyyyMMdd") | |
# Open URL of the issue/edition... | |
# "invoke-item" is not available for the purpose | |
start "http://www.asahi.com/shimen/redirect/$issue$edition.htm" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment