Last active
February 3, 2023 14:39
-
-
Save advanceboy/e1f899108bd077e9ff4a9e3f7eaf3e48 to your computer and use it in GitHub Desktop.
大量の nanaco ギフトを、 InternetExplorer を操作して自動で登録する PowerShell スクリプト
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 | |
Automatic nanaco gift card registration tool PowerShell edition. | |
.DESCRIPTION | |
Set either 'CardNumber' or 'Password' parameter. | |
<CommonParameters> is not supported. | |
.EXAMPLE | |
PS > $NanacoNumber = '0000000000000000'; | |
PS > $Password = Read-Host pass; | |
PS > &.\Register-NanacoGiftAutomatic.ps1 .\mail-text.txt -NanacoNumber $NanacoNumber -Password $Password; | |
.LINK | |
References: https://aquasoftware.net/blog/?p=1358 | |
#> | |
#Requires -Version 5 | |
param ( | |
# Text file whitch contains gift id url(s) | |
[Parameter(Mandatory=$true, Position=0, ParameterSetName = "NanacoMobile")] | |
[Parameter(Mandatory=$true, Position=0, ParameterSetName = "NanacoCard")] | |
[string] | |
$GiftTextFile, | |
# Nanaco number | |
[Parameter(Mandatory=$true, Position=1, ParameterSetName = "NanacoMobile")] | |
[Parameter(Mandatory=$true, Position=1, ParameterSetName = "NanacoCard")] | |
[ValidatePattern('^[0-9]{16}$', Options='IgnoreCase')] | |
[string] | |
$NanacoNumber, | |
# Card number for login as nanaco card member | |
[Parameter(Mandatory=$true, ParameterSetName = "NanacoCard")] | |
[ValidateNotNullOrEmpty()] | |
[ValidatePattern('^[0-9]{7}$', Options='IgnoreCase')] | |
[string] | |
$CardNumber, | |
# Password for login as nanaco mobile member | |
[Parameter(Mandatory=$true, ParameterSetName = "NanacoMobile")] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$Password, | |
# The browser to be controlled by Selenium | |
# Need to download the WebDriver program to match the browser. | |
[ValidateSet(IgnoreCase=$false, 'Chrome', 'Firefox', 'Edge', 'Safari', 'Opera')] | |
$Browser = 'Chrome', | |
# To treats the text file specified on the GiftTextFile parameter as a list of Gift-IDs. | |
[switch] | |
$AsGiftIdList, | |
# Specifies the file encoding. The default is default installed code page. | |
$Encoding = $null | |
); | |
trap { break; } | |
# Validate Params | |
if ([string]::IsNullOrEmpty($CardNumber) -and [string]::IsNullOrEmpty($Password)) { | |
throw "Both parameter 'CardNumber' and parameter 'Password' can not be null."; | |
} | |
$GiftTextFile = (Resolve-Path -LiteralPath $GiftTextFile).Path; | |
# Resolve the "Encoding" Argument | |
if ($null -eq $Encoding) { | |
$bytes = New-Object byte[](3); | |
$fs = [System.IO.File]::OpenRead($GiftTextFile); | |
try { | |
[void]$fs.Read($bytes, 0, 3); | |
} finally { | |
$fs.Dispose(); | |
} | |
if ([System.BitConverter]::ToInt32($bytes + @(0), 0) -eq 0x00BFBBEF) { | |
$Encoding = 'UTF8'; | |
} elseif ([System.BitConverter]::ToInt32($bytes[0..1] + @(0, 0), 0) -eq 0x0000FEFF) { | |
$Encoding = 'Unicode'; | |
} elseif ([System.BitConverter]::ToInt32($bytes[0..1] + @(0, 0), 0) -eq 0x0000FFFE) { | |
$Encoding = 'BigEndianUnicode'; | |
} elseif ($PSVersionTable.PSVersion -lt [version]'6.0') { | |
# Uses the System.Text.Encoding.Default | |
$Encoding = [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]::String; | |
} else { | |
# Uses the default encoding for MS-DOS and console programs by System.Management.Automation.EncodingConversion | |
$Encoding = 'OEM'; | |
} | |
} | |
# load Selenium | |
$myDir = $PSScriptRoot; | |
Add-Type -Path "$myDir\WebDriver.dll"; | |
# 1. Start Browser | |
$driver = New-Object "OpenQA.Selenium.$Browser.${Browser}Driver"; | |
# Register | |
function Register-NanacoGift { | |
param( | |
[Parameter(ValueFromPipeline=$True)] | |
[string] | |
$registerUrl | |
) | |
process { | |
if ([string]::IsNullOrEmpty($registerUrl)) { | |
throw 'No valid URL'; | |
} | |
# Dump progresses. | |
$registerUrl -match '(?<=\?)gid=.{4}' > $null; | |
$maskedGid = $Matches[0] + '************'; | |
Write-Host "Process: $maskedGid"; | |
# 2. login | |
$driver.Navigate().GoToUrl($registerUrl); | |
if (![string]::IsNullOrEmpty($CardNumber)) { | |
# For nanaco card member | |
# put nanaco number | |
$driver.FindElementById("nanacoNumber02").SendKeys($NanacoNumber); | |
Start-Sleep -m 100; | |
# put card number | |
$driver.FindElementById("cardNumber").SendKeys($CardNumber); | |
Start-Sleep -m 100; | |
# click login | |
$driver.FindElementById('loginPass02').Click(); | |
} else { | |
# For nanaco mobile member | |
# put nanaco number | |
$driver.FindElementById("nanacoNumber01").SendKeys($NanacoNumber); | |
Start-Sleep -m 100; | |
# put password | |
$driver.FindElementById("pass").SendKeys($Password); | |
Start-Sleep -m 100; | |
# click login | |
$driver.FindElementById('loginPass01').Click(); | |
} | |
Start-Sleep -m 100; | |
# 3. click "nanacoギフト登録" of member menu | |
$anchor = $driver.FindElementsByTagName('a') | Where-Object { | |
$null -ne $_.Text -and $_.Text.Contains('nanacoギフト登録') | |
} | Select-Object -First 1; | |
if ($null -eq $anchor) { | |
throw 'no valid button (Probably the card number or the password is wrong)'; | |
} | |
$anchor.Click(); | |
Start-Sleep 1; | |
# 4. click "ご利用約款に同意の上、登録" | |
$originalHwnd = $driver.CurrentWindowHandle; | |
$currentHwndList = [string[]]$driver.WindowHandles; | |
$anchorss = $driver.FindElementsByCssSelector('input[src*="/member/image/gift100/btn_400.gif"]') | Select-Object -First 1; | |
if ($null -eq $anchorss) { | |
throw 'no valid button'; | |
} | |
$anchorss.Click(); | |
Start-Sleep 1; | |
# 5. click "確認画面へ" button on gift registration form. | |
$driver.WindowHandles | ForEach-Object { | |
# switch to new window | |
if ($currentHwndList -notcontains $_) { | |
[void]$driver.SwitchTo().Window($_); | |
} | |
}; | |
try { | |
Start-Sleep -m 100; | |
$anchorss = $driver.FindElementById('submit-button'); | |
$anchorss.Click(); | |
Start-Sleep 1; | |
# push "登録" button and close IE window | |
$anchorss = $driver.FindElementsByCssSelector('input[alt="登録する"]') | Select-Object -First 1; | |
if ($null -eq $anchorss) { | |
Write-Error 'no valid button (Probably the gift is already registered)'; | |
} else { | |
$anchorss.Click(); | |
} | |
Start-Sleep 1; | |
} finally { | |
$driver.Close() | |
[void]$driver.SwitchTo().Window($originalHwnd); | |
} | |
} | |
} | |
# run | |
try { | |
if ($AsGiftIdList) { | |
# build URLs from nanaco gift-ids | |
$urlList = Get-Content $GiftTextFile -Encoding $Encoding | Where-Object { $_ -match '^[a-zA-Z0-9]{16}$'; } | ForEach-Object { "https://www.nanaco-net.jp/pc/emServlet?gid=$_" }; | |
} else { | |
# extracting URLs from the email text | |
$urlList = Get-Content $GiftTextFile -Encoding $Encoding | Where-Object { $_ -match 'https://www\.nanaco-net\.jp/pc/emServlet\?gid=[a-zA-Z0-9]{16}'; } | ForEach-Object { $Matches[0] }; | |
} | |
$urlList | Register-NanacoGift; | |
} finally { | |
$driver.Quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment