line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::YahooJPN::Sym2Name;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
40883
|
use 5.008;
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
48
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings;
|
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
31
|
|
6
|
1
|
|
|
1
|
|
5
|
use utf8;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.04'; # 2011-06-14 (since 2002-03-26)
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
10
|
|
|
|
|
|
|
our @EXPORT = qw(sym2name);
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
79
|
use Exporter;
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
13
|
1
|
|
|
1
|
|
4
|
use Carp;
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
76
|
|
14
|
1
|
|
|
1
|
|
851
|
use IO::Socket;
|
|
1
|
|
|
|
|
29986
|
|
|
1
|
|
|
|
|
5
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $Server = 'stocks.finance.yahoo.co.jp';
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Finance::YahooJPN::Sym2Name - converts a Japanese stock symbol to the name
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Finance::YahooJPN::Sym2Name;
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# get the name of company of stock symbol code '6758'
|
27
|
|
|
|
|
|
|
my $stockname = sym2name('6758');
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
print $stockname; # it prints 'SONY CORPORATION'
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
This module converts a Japanese stock symbol code to the name of company. Japanese stock markets use 4-digit code number as stock symbol. You can get either English or Japanese name of company.
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 FUNCTIONS
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=over
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item sym2name($symbol [, $lang])
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
This function returns a string of the name of company from C<$symbol> (a stock symbol code of 4-digit number).
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
In case it were a missing code number, the module returns string '(n/a)';
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
C<$lang> attribute specifies language mode of the company's name. A C<$lang> value is 'eng' (in English) or 'jpn' (in Japanese). This attribute is omittable and the default value is 'eng'.
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Note that string data under 'jpn' C<$lang> mode is encoded with UTF-8 character encoding.
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub sym2name ($;$) {
|
52
|
4
|
|
|
4
|
1
|
36
|
my($symbol, $lang) = @_;
|
53
|
|
|
|
|
|
|
|
54
|
4
|
50
|
|
|
|
34
|
unless ($symbol =~ /^\d{4}$/) {
|
55
|
0
|
|
|
|
|
0
|
croak "stock symbol code must be specified with 4-digit code number";
|
56
|
|
|
|
|
|
|
}
|
57
|
|
|
|
|
|
|
|
58
|
4
|
|
|
|
|
17
|
my $url = "http://stocks.finance.yahoo.co.jp/stocks/profile/?code=$symbol";
|
59
|
4
|
|
|
|
|
18
|
my @html = fetch($url);
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# in case it were a missing code number
|
62
|
4
|
|
|
|
|
109
|
foreach my $line (@html) {
|
63
|
2316
|
100
|
|
|
|
5160
|
if ($line =~ m/一致する銘柄は見つかりませんでした/) {
|
64
|
1
|
|
|
|
|
48
|
return '(n/a)';
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
}
|
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
|
|
10
|
my %name;
|
69
|
|
|
|
|
|
|
# find and extract the name of company in Japanese
|
70
|
3
|
|
|
|
|
7
|
foreach my $line (@html) {
|
71
|
2118
|
|
|
|
|
6250
|
$line =~ m/(.+?)【$symbol】.*?<\/title>/;
|
72
|
2118
|
|
|
|
|
13895
|
utf8::decode($name{'jpn'} = $1);
|
73
|
|
|
|
|
|
|
}
|
74
|
3
|
|
|
|
|
30
|
$name{'jpn'} =~ s/\(株\)//;
|
75
|
3
|
|
|
|
|
23
|
$name{'jpn'} =~ s/^\s*//;
|
76
|
3
|
|
|
|
|
22
|
$name{'jpn'} =~ s/\s*$//;
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# find and extract the name of company in English
|
79
|
3
|
|
|
|
|
22
|
for (my $i = 0; $i < @html; $i++) {
|
80
|
1014
|
100
|
|
|
|
3163
|
if ($html[$i] eq ' | 英文社名 | ') {
81
|
3
|
|
|
|
|
175
|
$html[$i + 1] =~ m/ | (.+?)<\/td>/;
|
82
|
3
|
|
|
|
|
15
|
$name{'eng'} = $1;
|
83
|
3
|
|
|
|
|
17
|
$name{'eng'} = full2half($name{'eng'});
|
84
|
3
|
|
|
|
|
9
|
last;
|
85
|
|
|
|
|
|
|
}
|
86
|
|
|
|
|
|
|
}
|
87
|
|
|
|
|
|
|
|
88
|
3
|
100
|
100
|
|
|
30
|
if ($lang and lc($lang) eq 'jpn') {
|
89
|
1
|
|
|
|
|
83
|
return $name{'jpn'};
|
90
|
|
|
|
|
|
|
}
|
91
|
|
|
|
|
|
|
else {
|
92
|
2
|
|
|
|
|
157
|
return $name{'eng'};
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
}
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub fetch ($) {
|
97
|
4
|
|
|
4
|
0
|
11
|
my $abs_path = shift;
|
98
|
|
|
|
|
|
|
|
99
|
4
|
50
|
|
|
|
62
|
my $sock = IO::Socket::INET->new(
|
100
|
|
|
|
|
|
|
PeerAddr => $Server,
|
101
|
|
|
|
|
|
|
PeerPort => 'http(80)',
|
102
|
|
|
|
|
|
|
Proto => 'tcp',
|
103
|
|
|
|
|
|
|
) or die "Couldn't connect to $Server";
|
104
|
|
|
|
|
|
|
|
105
|
4
|
|
|
|
|
1155015
|
print $sock <<"EOF";
|
106
|
|
|
|
|
|
|
GET $abs_path HTTP/1.1
|
107
|
|
|
|
|
|
|
Host: $Server
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
EOF
|
110
|
|
|
|
|
|
|
|
111
|
4
|
|
|
|
|
203365992
|
chomp(my @html = <$sock>);
|
112
|
4
|
|
|
|
|
865
|
close $sock;
|
113
|
|
|
|
|
|
|
|
114
|
4
|
|
|
|
|
28
|
foreach my $line (@html) {
|
115
|
2550
|
|
|
|
|
4931
|
utf8::decode($line);
|
116
|
|
|
|
|
|
|
}
|
117
|
4
|
|
|
|
|
713
|
return @html;
|
118
|
|
|
|
|
|
|
}
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub full2half ($) {
|
121
|
3
|
|
|
3
|
0
|
7
|
my $string = shift;
|
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
1
|
|
1476
|
$string =~ tr/0-9A-Za-z/0-9A-Za-z/;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
129
|
|
124
|
3
|
|
|
|
|
27
|
$string =~ tr[―~ _|.,:;!?][-~ _|.,:;!?];
|
125
|
3
|
|
|
|
|
16
|
$string =~ tr/()[]{}<>《》【】‘’“”/()[]{}<><>[]`\'""/;
|
126
|
3
|
|
|
|
|
17
|
$string =~ tr[-+/*%=×][-+/*%=*];
|
127
|
3
|
|
|
|
|
15
|
$string =~ tr/‐@#$¥&^・/-@#$\\&^-/;
|
128
|
|
|
|
|
|
|
|
129
|
3
|
|
|
|
|
14
|
return $string;
|
130
|
|
|
|
|
|
|
}
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1;
|
133
|
|
|
|
|
|
|
__END__
|