Created
March 2, 2020 06:58
-
-
Save nimzo6689/a1b113a1455d118a397b0e330b62130f to your computer and use it in GitHub Desktop.
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
# If you have not yet installed HtmlAgilityPack(HAP), try to run these commands. | |
# | |
# Register-PackageSource -Name Nuget -Location "http://www.nuget.org/api/v2" –ProviderName Nuget -Trusted | |
# Install-Package HtmlAgilityPack -RequiredVersion 1.11.20 -Scope CurrentUser -SkipDependencies | |
Add-Type -AssemblyName "$env:LOCALAPPDATA\PackageManagement\NuGet\Packages\HtmlAgilityPack.1.11.20\lib\netstandard2.0\HtmlAgilityPack.dll" | |
# If your XPath does not work, and it contains tbody or something like that, remove these tags, because web brawzer automatically added. | |
# 時刻表ページから平日・土日祝で運航されるタイムラインIDを取得する。 | |
# 取得できたタイムラインIDごとに | |
# 全時間帯(5時~24時)のノードを取得 | |
# -- 平日 ------------------ | |
# line table | |
# line_id | |
# company_id | |
# bound_for(inbound, outbound) | |
# day_type(weekdays, holidays) | |
# timeline table | |
# line_id, timeline_id, station_id, arrival_time, departure_time | |
class YahooTransitCrawler { | |
static [HtmlAgilityPack.HTMLDocument] createDom([string]$path) { | |
$content = Invoke-RestMethod "https://transit.yahoo.co.jp/$path" | |
$htmlDoc = New-Object HtmlAgilityPack.HTMLDocument | |
$htmlDoc.LoadHtml($content); | |
return $htmlDoc; | |
} | |
} | |
# 都道府県別の路線一覧ページから路線駅一覧情報を取得する | |
# 路線駅一覧ページから駅情報を取得する | |
# 駅の路線一覧ページから時刻表IDを取得する | |
function Get-RailId { | |
[CmdletBinding()] | |
param ( | |
[int]$StationId | |
) | |
# 「Yahoo!路線情報」から時刻表を取得 | |
$htmlDoc = [YahooTransitCrawler]::createDom("station/rail/$StationId/?kind=1"); | |
$timeLines = @() | |
foreach ($rail in $htmlDoc.DocumentNode.SelectNodes('//*[@id="mdSearchLine"]/ul/li/dl')) { | |
$railName = $rail.SelectSingleNode('dt').InnerText | |
$boundFor = $rail.SelectSingleNode('dd/ul/li/a').InnerText | |
$railPath = $rail.SelectSingleNode('dd/ul/li/a').Attributes.Value | |
# /station/time/22790/?gid=7171&... | |
$railPath -match '/station/time/(\d+)/?gid=(\d+)&' | Out-Null | |
$timeLines += [PSCustomObject]@{ | |
StationId = $Matches[1] | |
BoundFor = $Matches[2] | |
} | |
} | |
return $timeLines | |
} | |
# 時刻表から停車時間IDを取得する | |
function Get-TimeLineId { | |
[CmdletBinding()] | |
param ( | |
[int]$StationId, [int]$BoundFor, [int]$DayType = 1 | |
) | |
# 「Yahoo!路線情報」から時刻表を取得 | |
$htmlDoc = [YahooTransitCrawler]::createDom("station/time/$StationId/?tab=sta&gid=$BoundFor&kind=$DayType"); | |
$timeLines = @() | |
foreach ($timelinePath in $htmlDoc.DocumentNode.SelectNodes('//*[@class="tblDiaDetail"]/tr/td[2]/ul/li/a').Attributes.Value) { | |
# /station/time/train/4454/?... | |
$timelinePath -match '/station/time/train/(\d+)/' | Out-Null | |
$timeLines += $Matches[1] | |
} | |
return $timeLines | |
} | |
# ある時刻に発車する電車の運行タイムラインを取得 | |
function Get-TrainTimeLine { | |
[CmdletBinding()] | |
param ( | |
[int]$TimeLineId, | |
# StationId は存在している駅のものであればどれでも問題ないため、固定値を設定。 | |
[int]$StationId = 22790 | |
) | |
# 「Yahoo!路線情報」からダイヤ情報を取得 | |
$htmlDoc = [YahooTransitCrawler]::createDom("station/time/train/$TimeLineId/?st=$StationId"); | |
$TimeLine = @() | |
$IsStartingSta = $true | |
# 各停車駅ごとの駅名、到着・出発時刻を取得 | |
foreach ($dia in $htmlDoc.DocumentNode.SelectNodes('//*[@id="mdDiaStopSta"]/ul/li/div')) { | |
# 停車駅名 | |
$station = $dia.SelectSingleNode('p').InnerText | |
# 到着時刻 | |
$arrivalTime = $dia.SelectSingleNode('ul[1]/li[1]').InnerText | |
# 出発時刻 | |
$departureTime = $dia.SelectSingleNode('ul[1]/li[2]').InnerText | |
if ($IsStartingSta) { | |
# 始発駅の場合、 出発時刻が $arrivalTime に格納されるため、スワップする。 | |
$departureTime, $arrivalTime = $arrivalTime, $departureTime | |
$IsStartingSta = $false | |
} | |
$TimeLine += [PSCustomObject]@{ | |
Station = $station | |
ArrivalTime = $arrivalTime ? [timespan]($arrivalTime -replace '着') : $null | |
DepartureTime = $departureTime ? [timespan]($departureTime -replace '発') : $null | |
} | |
} | |
return $TimeLine | |
} | |
Get-TimeLineId -StationId 22790 -BoundFor 2880 | |
Get-TrainTimeLine -TimeLineId 171323 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment