thaistock 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/thaistock.rb +86 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 024470e4192d3b2cdf379fbb423ba8498515da8c236d87062c0bba69f0631b97
4
+ data.tar.gz: 758cb2bb2c253c990cb8ed5c26c3c3fb8228cd9cac7869de7db4f68eaba3ce68
5
+ SHA512:
6
+ metadata.gz: '062695d95eb60435f216b5d0ec92badc1451fb45d55bc91aadd60a036e1779652a80866bd1ac3ae6dab61ff2d3fbea26c7f7c445c231f9cdc59095b4278c0262'
7
+ data.tar.gz: df0af877159645a5ceb8e86a835c57616cf1a633a46ac1e2dac845c3f20fe0ff67b652534de86a50e2c848de17147d3d6e76811726c04e0f7351f69837051db2
data/lib/thaistock.rb ADDED
@@ -0,0 +1,86 @@
1
+ # Homework 2: Web Scaping pratice
2
+ # Name: Saruj Sattayanurak
3
+ # ID: 6210545700
4
+
5
+ require 'open-uri'
6
+ require 'nokogiri'
7
+
8
+
9
+ # generate openable URL for Nokogiri
10
+ #
11
+ # @param [String] URL's postfix
12
+ # @return [String] openable URL
13
+ def get_link(url)
14
+ prefix = "https://www.set.or.th"
15
+ return prefix + url.values[0]
16
+ end
17
+
18
+
19
+ # open url with Nokogiri
20
+ #
21
+ # @param [String] URL
22
+ # @return [Nokogiri] object
23
+ def open_link(url)
24
+ return Nokogiri::HTML(URI.open("#{url}"))
25
+ end
26
+
27
+
28
+ # check index of asset column
29
+ #
30
+ # @param [Nokogiri] object
31
+ # @return [Int] column of asset
32
+ def check_asset_index(stock)
33
+ index = 1
34
+ begin
35
+ while true
36
+ stock_detail = stock.css("th[#{index}]").first.text
37
+ if stock_detail.to_s.include? "ไตรมาส"
38
+ break
39
+ else
40
+ index += 1
41
+ end
42
+ end
43
+ rescue NoMethodError
44
+ index = 10
45
+ end
46
+ return index
47
+ end
48
+
49
+
50
+ url = "https://www.set.or.th/set/commonslookup.do"
51
+ source = open_link(url)
52
+
53
+ source.search("a").map{ |prefix_href|
54
+ if prefix_href.to_s.include? "prefix="
55
+
56
+ # open each catagories
57
+ prefix_menu = open_link(get_link(prefix_href))
58
+ prefix_menu.search("td a").map{ |stock|
59
+
60
+ # open each stock in the following catagories
61
+ stock_info = open_link(get_link(stock))
62
+
63
+ # print company's name
64
+ company_name = stock_info.css("h3").text
65
+ print company_name
66
+ print " : "
67
+
68
+ stock_info.search("a").map{ |stock_asset|
69
+ if stock_asset.to_s.include? "companyhighlight"
70
+
71
+ # open each stock detail
72
+ current_stock_asset = open_link(get_link(stock_asset))
73
+ index = check_asset_index(current_stock_asset)
74
+
75
+ begin
76
+ # print company asset
77
+ company_asset = current_stock_asset.css("td[#{index}]").first.text
78
+ puts company_asset
79
+ rescue NoMethodError
80
+ puts "NO DATA FOUND"
81
+ end
82
+ end
83
+ }
84
+ }
85
+ end
86
+ }
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thaistock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Saruj Sattayanurak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This is a simple gem that read and extract current asset of stocks in
14
+ Thailand from www.set.or.th. This gem is a part of Software Specification and Design
15
+ course 01219243 at Kasetsart University
16
+ email: jomsaruj@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/thaistock.rb
22
+ homepage: https://rubygems.org/gems/thaistock
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: read and extract current asset of stocks in Thailand
44
+ test_files: []