Created
October 8, 2012 10:58
-
-
Save t-kashima/3851951 to your computer and use it in GitHub Desktop.
自由が丘を走る「Thanks Nature Bus」の時刻表を取得するスクリプト
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use JSON::XS; | |
use DateTime; | |
use utf8; | |
use Data::Dumper; | |
binmode STDIN, ':utf8'; | |
binmode STDOUT, ":utf8"; | |
my $timefile = 'time.json'; | |
sub read_hash_from_json_file { | |
my $file = shift; | |
# my $str = '{"a": "b", "c": "e"}'; | |
my $str = ''; | |
open(my $fh, '<', $timefile) or die $!; | |
while (my $line = <$fh>) { | |
chomp $line; | |
$str .= $line; | |
} | |
close($fh); | |
return decode_json($str); | |
} | |
sub time_from_str { | |
my $str = shift; | |
my $dt = DateTime->now(time_zone => 'Asia/Tokyo'); | |
my $hour = substr($str, 0, 2); | |
my $min = substr($str, 2, 2); | |
my $new_dt = DateTime->new( | |
time_zone => 'Asia/Tokyo', | |
year => $dt->strftime('%Y'), | |
month => $dt->strftime('%m'), | |
day => $dt->strftime('%d'), | |
hour => $hour, | |
minute => $min | |
); | |
return $new_dt; | |
} | |
sub next_time_list_for_stop_time_list { | |
my $stop_time_list = shift; | |
my $dt = DateTime->now(time_zone => 'Asia/Tokyo'); | |
my @next_time = (); | |
for my $time (@$stop_time_list) { | |
my $time_obj = time_from_str($time); | |
if ($time_obj->epoch >= $dt->epoch) { | |
push @next_time, $time_obj; | |
} | |
} | |
return \@next_time; | |
} | |
sub main { | |
my $stop_time_list = read_hash_from_json_file($timefile); | |
for my $key (keys %$stop_time_list) { | |
print $key . "\n"; | |
my $next_time_list = next_time_list_for_stop_time_list($stop_time_list->{$key}); | |
for my $time (@$next_time_list) { | |
print $time->strftime('%H:%M') . "\n"; | |
} | |
} | |
} | |
main; |
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
{"深沢ハウス": ["1200", "1235", "1310", "1345", | |
"1520", "1555", "1630", "1735", | |
"1810", "1845", "1950", "2020", "2050", "2120"], | |
"自由が丘クリニック": ["1206", "1241", "1316", "1351", | |
"1526", "1601", "1636", "1741", | |
"1816", "1851", "1956", "2026", "2056"], | |
"モンサンクレール": ["1207", "1242", "1317", "1352", | |
"1527", "1602", "1637", "1742", | |
"1817", "1852", "1957", "2027", "2057"], | |
"ラヴィータ": ["1208", "1243", "1318", "1353", | |
"1528", "1603", "1638", "1743", | |
"1818", "1853", "1958", "2028", "2058"], | |
"自由が丘駅南口": ["1214", "1249", "1324", "1359", | |
"1534", "1609", "1644", "1749", | |
"1824", "1859", "2004", "2034", "2104"], | |
"高橋建設": ["1220", "1255", "1330", "1405", | |
"1540", "1615", "1650", "1755", | |
"1830", "1905", "2010", "2040", "2110"], | |
"リバティヒルクラブ": ["1222", "1257", "1332", "1407", | |
"1542", "1617", "1652", "1757", | |
"1832", "1907", "2012", "2042", "2112"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment