rulethu_stock_exchange 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -6
- data/lib/rulethu_stock_exchange/io.rb +8 -8
- data/lib/rulethu_stock_exchange/parser.rb +26 -26
- data/lib/rulethu_stock_exchange/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e2e09b5387087ac9abfb3fea2dda21cb983328fcefe63233543ba22797ff244
|
4
|
+
data.tar.gz: 0a51aca05580cac8ff1f555b0b31de12ad3c8f1526b147a6f66d01530a8fe36b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc72e015dd2e3ca7450be928e323d12e48ded2807767fdbf4ed63ac6036834e6998e5bef1dbc1228d048cb5b3c1175a803101adb29b65bbc7b03e20ed5cae3a6
|
7
|
+
data.tar.gz: b8ea28ad59be15ccd1f864e0d6f49b25ec57650a37a5eee2ea08babf110b47d573e2226785b69a16d6c5d30023706c2b374d1d9eb7d2ca95023dee699e80a212
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# RulethuStockExchange
|
2
2
|
|
3
3
|
This gem allows you to scrape stock exchange data from the following exchanges:
|
4
|
-
* Zimbabwe Stock Exchange (ZSE)
|
5
|
-
* Johannesburg Stock Exchange (JSE)
|
6
|
-
* London Stock Exchange (LSE)
|
7
4
|
|
5
|
+
- Zimbabwe Stock Exchange (ZSE)
|
6
|
+
- Johannesburg Stock Exchange (JSE)
|
7
|
+
- London Stock Exchange (LSE)
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -18,8 +18,12 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
** Make sure you have firefox browser installed **
|
22
|
+
|
23
|
+
### Example 1: Scraping Data From London Stock Exchange
|
24
|
+
|
22
25
|
Require the gem
|
26
|
+
|
23
27
|
```ruby
|
24
28
|
require 'rulethu_stock_exchange'
|
25
29
|
```
|
@@ -29,32 +33,39 @@ Get the stock exchange. (Currently available stock exchanges are ZSE, JSE and LS
|
|
29
33
|
```ruby
|
30
34
|
exchange = RulethuStockExchange::Exchanges::LSE
|
31
35
|
```
|
36
|
+
|
32
37
|
Create a Scraper passing in the url for the exchangew
|
38
|
+
|
33
39
|
```ruby
|
34
40
|
scraper = RulethuStockExchange::Scraper.new exchange[:url]
|
35
41
|
```
|
36
42
|
|
37
43
|
Scrape the data passing in a selector
|
44
|
+
|
38
45
|
```ruby
|
39
46
|
html = scraper.scrape exchange[:selector]
|
40
47
|
```
|
41
48
|
|
42
49
|
Create a parser to parse the data
|
50
|
+
|
43
51
|
```ruby
|
44
|
-
parser = RulethuStockExchange::Parser.new html
|
52
|
+
parser = RulethuStockExchange::Parser.new html
|
45
53
|
```
|
46
54
|
|
47
55
|
Parse the data
|
56
|
+
|
48
57
|
```ruby
|
49
|
-
data = parser.parse()
|
58
|
+
data = parser.parse()
|
50
59
|
```
|
51
60
|
|
52
61
|
Write the data to JSON file
|
62
|
+
|
53
63
|
```ruby
|
54
64
|
filename = RulethuStockExchange::IO.write_to_json_file data, 'lse'
|
55
65
|
```
|
56
66
|
|
57
67
|
Convert the JSON file to CSV
|
68
|
+
|
58
69
|
```ruby
|
59
70
|
RulethuStockExchange::IO.json_to_csv filename
|
60
71
|
```
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
2
|
+
require "json"
|
3
|
+
require "csv"
|
4
|
+
require "time"
|
5
5
|
|
6
6
|
module RulethuStockExchange
|
7
7
|
class IO
|
@@ -10,8 +10,8 @@ module RulethuStockExchange
|
|
10
10
|
date = Time.now.strftime("%d-%m-%Y")
|
11
11
|
data_dir = Pathname.new("data") / Pathname.new(prefix)
|
12
12
|
data_dir.mkpath
|
13
|
-
filename = "#{data_dir}/#{date}-data.json"
|
14
|
-
File.open(filename,
|
13
|
+
filename = "#{data_dir}/#{prefix}-#{date}-data.json"
|
14
|
+
File.open(filename, "w") do |f|
|
15
15
|
f.write(JSON.dump(data))
|
16
16
|
end
|
17
17
|
filename
|
@@ -20,13 +20,13 @@ module RulethuStockExchange
|
|
20
20
|
def self.json_to_csv(json_file)
|
21
21
|
puts "\nConverting #{json_file} to CSV"
|
22
22
|
data = Object.new
|
23
|
-
File.open(json_file,
|
23
|
+
File.open(json_file, "r") do |f|
|
24
24
|
data = JSON.parse(f.read)
|
25
25
|
end
|
26
26
|
|
27
|
-
filename = json_file.sub(/\.json$/,
|
27
|
+
filename = json_file.sub(/\.json$/, ".csv")
|
28
28
|
|
29
|
-
CSV.open(filename,
|
29
|
+
CSV.open(filename, "w") do |csv|
|
30
30
|
csv << data.first.keys
|
31
31
|
data.each do |row|
|
32
32
|
csv << row.values
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require
|
2
|
+
require "selenium-webdriver"
|
3
3
|
|
4
4
|
module RulethuStockExchange
|
5
5
|
class Parser
|
@@ -10,39 +10,39 @@ module RulethuStockExchange
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def parse_zse
|
13
|
-
tbody = table.find_element(:tag_name,
|
14
|
-
rows = tbody.find_elements(:tag_name,
|
15
|
-
|
16
|
-
puts "\nWriting body data"
|
13
|
+
tbody = table.find_element(:tag_name, "tbody")
|
14
|
+
rows = tbody.find_elements(:tag_name, "tr")
|
15
|
+
data_rows = []
|
17
16
|
rows.each do |row|
|
18
|
-
|
19
|
-
|
20
|
-
body_data << cell.text.strip
|
21
|
-
end
|
17
|
+
text = row.find_elements(:tag_name, "td").first.text.strip
|
18
|
+
data_rows << row unless text.empty?
|
22
19
|
end
|
23
|
-
|
24
|
-
header_data =
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
20
|
+
|
21
|
+
header_data = []
|
22
|
+
rows[0].find_elements(:tag_name, "td").each do |td|
|
23
|
+
text = td.text.strip.gsub("\n", " ") || "#"
|
24
|
+
text = "#" if text.empty?
|
25
|
+
header_data << text
|
26
|
+
end
|
27
|
+
|
28
|
+
data = []
|
29
|
+
data_rows.each do |row|
|
30
|
+
row_data = {}
|
31
|
+
cells = row.find_elements :tag_name, "td"
|
32
|
+
cells.each_with_index do |cell, index|
|
33
|
+
key = header_data[index] || "#"
|
34
|
+
row_data[key] = cell.text.strip
|
34
35
|
end
|
35
|
-
|
36
|
-
print "."
|
36
|
+
data << row_data
|
37
37
|
end
|
38
|
-
|
38
|
+
data
|
39
39
|
end
|
40
40
|
|
41
41
|
def parse
|
42
42
|
header_data = []
|
43
43
|
begin
|
44
44
|
thead = table.find_element(:tag_name, "thead")
|
45
|
-
cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name,
|
45
|
+
cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name, "th")
|
46
46
|
puts "Writing header data"
|
47
47
|
cells.each do |cell|
|
48
48
|
header_data << cell.text
|
@@ -52,12 +52,12 @@ module RulethuStockExchange
|
|
52
52
|
# Handle missing header gracefully (e.g., log the error)
|
53
53
|
end
|
54
54
|
tbody = table.find_element(:tag_name, "tbody")
|
55
|
-
rows = tbody.find_elements(:tag_name,
|
55
|
+
rows = tbody.find_elements(:tag_name, "tr")
|
56
56
|
body_data = []
|
57
57
|
puts "\nWriting body data"
|
58
58
|
rows.each do |row|
|
59
59
|
data = {}
|
60
|
-
cells = row.find_elements(:tag_name,
|
60
|
+
cells = row.find_elements(:tag_name, "td")
|
61
61
|
if header_data.empty?
|
62
62
|
header_data = cells.map(&:text)
|
63
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rulethu_stock_exchange
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blessed Sibanda
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: selenium-webdriver
|