thaistock 0.0.0 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/thaistock.rb +69 -61
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb4488b0a4e82b04eee939eeb8b5364b466216638f559841494180ea7b4e68e8
|
4
|
+
data.tar.gz: 9cbb7f8ecd81f0127a58e82f169277df67ca2d709591f50b0aa6144b817655d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38daabec7318b6e42eefba3fe29f630ac4389617dcb45d48be80ab0ab1b8f618b254aa8454a6f14672b97cd982d1e13dc29b7c14a86edbb74488447dce6b1e84
|
7
|
+
data.tar.gz: 83d5b817b1754486a8274684e5c7c8729c87ca1533b26a13135192811555ac87c557e64970984eebbc1fdb3b5753beabda6c6f28c62663c4fc16528f497419e2
|
data/lib/thaistock.rb
CHANGED
@@ -5,82 +5,90 @@
|
|
5
5
|
require 'open-uri'
|
6
6
|
require 'nokogiri'
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# @
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
8
|
+
class ThaiStock
|
9
|
+
|
10
|
+
# generate openable URL for Nokogiri
|
11
|
+
#
|
12
|
+
# @param [String] URL's postfix
|
13
|
+
# @return [String] openable URL
|
14
|
+
def self.get_link(url)
|
15
|
+
prefix = "https://www.set.or.th"
|
16
|
+
return prefix + url.values[0]
|
17
|
+
end
|
17
18
|
|
18
19
|
|
19
|
-
# open url with Nokogiri
|
20
|
-
#
|
21
|
-
# @param [String] URL
|
22
|
-
# @return [Nokogiri] object
|
23
|
-
def open_link(url)
|
24
|
-
|
25
|
-
end
|
20
|
+
# open url with Nokogiri
|
21
|
+
#
|
22
|
+
# @param [String] URL
|
23
|
+
# @return [Nokogiri] object
|
24
|
+
def self.open_link(url)
|
25
|
+
return Nokogiri::HTML(URI.open("#{url}"))
|
26
|
+
end
|
26
27
|
|
27
28
|
|
28
|
-
# check index of asset column
|
29
|
-
#
|
30
|
-
# @param [Nokogiri] object
|
31
|
-
# @return [Int] column of asset
|
32
|
-
def check_asset_index(stock)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
29
|
+
# check index of asset column
|
30
|
+
#
|
31
|
+
# @param [Nokogiri] object
|
32
|
+
# @return [Int] column of asset
|
33
|
+
def self.check_asset_index(stock)
|
34
|
+
index = 1
|
35
|
+
begin
|
36
|
+
while true
|
37
|
+
stock_detail = stock.css("th[#{index}]").first.text
|
38
|
+
if stock_detail.to_s.include? "ไตรมาส"
|
39
|
+
break
|
40
|
+
else
|
41
|
+
index += 1
|
42
|
+
end
|
41
43
|
end
|
44
|
+
rescue NoMethodError
|
45
|
+
index = 10
|
42
46
|
end
|
43
|
-
|
44
|
-
index = 10
|
47
|
+
return index
|
45
48
|
end
|
46
|
-
return index
|
47
|
-
end
|
48
49
|
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
# Excucute the program
|
52
|
+
def self.thai_stock
|
52
53
|
|
53
|
-
|
54
|
-
|
54
|
+
url = "https://www.set.or.th/set/commonslookup.do"
|
55
|
+
source = self.open_link(url)
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
prefix_menu.search("td a").map{ |stock|
|
57
|
+
source.search("a").map{ |prefix_href|
|
58
|
+
if prefix_href.to_s.include? "prefix="
|
59
59
|
|
60
|
-
|
61
|
-
|
60
|
+
# open each catagories
|
61
|
+
prefix_menu = self.open_link(get_link(prefix_href))
|
62
|
+
prefix_menu.search("td a").map{ |stock|
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
print company_name
|
66
|
-
print " : "
|
64
|
+
# open each stock in the following catagories
|
65
|
+
stock_info = self.open_link(get_link(stock))
|
67
66
|
|
68
|
-
|
69
|
-
|
67
|
+
# print company's name
|
68
|
+
company_name = stock_info.css("h3").text
|
69
|
+
print company_name
|
70
|
+
print " : "
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
index = check_asset_index(current_stock_asset)
|
72
|
+
stock_info.search("a").map{ |stock_asset|
|
73
|
+
if stock_asset.to_s.include? "companyhighlight"
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
75
|
+
# open each stock detail
|
76
|
+
current_stock_asset = self.open_link(get_link(stock_asset))
|
77
|
+
index = self.check_asset_index(current_stock_asset)
|
78
|
+
|
79
|
+
begin
|
80
|
+
# print company asset
|
81
|
+
company_asset = current_stock_asset.css("td[#{index}]").first.text
|
82
|
+
puts company_asset
|
83
|
+
rescue NoMethodError
|
84
|
+
puts "NO DATA FOUND"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
}
|
88
|
+
}
|
89
|
+
end
|
84
90
|
}
|
85
91
|
end
|
86
|
-
|
92
|
+
end
|
93
|
+
|
94
|
+
|