rulethu_stock_exchange 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +29 -25
- data/lib/rulethu_stock_exchange/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 997032ba38a153377cf0576dce101b69ec777e48f58c42e5ec06b7b860222d5b
|
4
|
+
data.tar.gz: b1dc52bc0af36edb912a4af09892023c652c47461de0163d3c9c22544ae6fb59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b91fed4ad022fbfb94b08992a4a62b9a80ba492b22fcee7345d87ca977c4f6def7c124c1799c06f0b956c101ed96591cc0b14e13ef7bcbb63315c77fa5c5360
|
7
|
+
data.tar.gz: 8ccb363b6bdec459d6125f6c3c8be366c5ee5ad0a233e5ddad9599b00291fdf615498697382eb3f7b7e1c3e03442f4cb6aac8f7378b3aa4324b0eeb32bfb5572
|
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,43 @@ 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
|
-
|
17
|
+
text = row.find_elements(:tag_name, "td").first.text.strip
|
18
|
+
unless text&.upcase == "EQUITIES"
|
19
|
+
data_rows << row unless text.empty?
|
21
20
|
end
|
22
21
|
end
|
23
|
-
|
24
|
-
header_data =
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
22
|
+
|
23
|
+
header_data = []
|
24
|
+
data_rows.first.find_elements(:tag_name, "td").each do |td|
|
25
|
+
text = td.text.strip.gsub("\n", " ") || "#"
|
26
|
+
text = "#" if text.empty?
|
27
|
+
header_data << text
|
28
|
+
end
|
29
|
+
|
30
|
+
data_rows = data_rows.slice(1, data_rows.length)
|
31
|
+
|
32
|
+
data = []
|
33
|
+
data_rows.each do |row|
|
34
|
+
row_data = {}
|
35
|
+
cells = row.find_elements :tag_name, "td"
|
36
|
+
cells.each_with_index do |cell, index|
|
37
|
+
key = header_data[index] || "#"
|
38
|
+
row_data[key] = cell.text.strip
|
34
39
|
end
|
35
|
-
|
36
|
-
print "."
|
40
|
+
data << row_data
|
37
41
|
end
|
38
|
-
|
42
|
+
data
|
39
43
|
end
|
40
44
|
|
41
45
|
def parse
|
42
46
|
header_data = []
|
43
47
|
begin
|
44
48
|
thead = table.find_element(:tag_name, "thead")
|
45
|
-
cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name,
|
49
|
+
cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name, "th")
|
46
50
|
puts "Writing header data"
|
47
51
|
cells.each do |cell|
|
48
52
|
header_data << cell.text
|
@@ -52,12 +56,12 @@ module RulethuStockExchange
|
|
52
56
|
# Handle missing header gracefully (e.g., log the error)
|
53
57
|
end
|
54
58
|
tbody = table.find_element(:tag_name, "tbody")
|
55
|
-
rows = tbody.find_elements(:tag_name,
|
59
|
+
rows = tbody.find_elements(:tag_name, "tr")
|
56
60
|
body_data = []
|
57
61
|
puts "\nWriting body data"
|
58
62
|
rows.each do |row|
|
59
63
|
data = {}
|
60
|
-
cells = row.find_elements(:tag_name,
|
64
|
+
cells = row.find_elements(:tag_name, "td")
|
61
65
|
if header_data.empty?
|
62
66
|
header_data = cells.map(&:text)
|
63
67
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blessed Sibanda
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: selenium-webdriver
|
@@ -52,7 +52,7 @@ metadata:
|
|
52
52
|
homepage_uri: https://github.com/rulethu/rulethu_stock_exchange.git
|
53
53
|
source_code_uri: https://github.com/rulethu/rulethu_stock_exchange.git
|
54
54
|
changelog_uri: https://github.com/rulethu/rulethu_stock_exchange/blob/main/CHANGELOG.md
|
55
|
-
post_install_message:
|
55
|
+
post_install_message:
|
56
56
|
rdoc_options: []
|
57
57
|
require_paths:
|
58
58
|
- lib
|
@@ -67,8 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
rubygems_version: 3.5.
|
71
|
-
signing_key:
|
70
|
+
rubygems_version: 3.5.20
|
71
|
+
signing_key:
|
72
72
|
specification_version: 4
|
73
73
|
summary: Get Stock Exchange Data
|
74
74
|
test_files: []
|