basic_yahoo_finance 0.5.1 → 0.6.0
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/CHANGELOG.md +19 -0
- data/README.md +19 -42
- data/lib/basic_yahoo_finance/version.rb +1 -1
- data/lib/basic_yahoo_finance.rb +63 -16
- metadata +4 -21
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e7e80847cd866e0bbf3de8dc8be42079bd72b8c771d03e2ef581b0d6929c2bc
|
|
4
|
+
data.tar.gz: 8fe353c9b4a494e55f157bfc3807432eaabe345a54963028b78330bda8f0a17e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 55f5dbfb66c2afa4175671e770310dbe41ef2c48fdd5e53761019b6367bf92aad4b62281c811776a9e6314adafa17fec5838e4771c186c0b5ecf4833d57acca2
|
|
7
|
+
data.tar.gz: c3fa55322e6155b1c0caaf39844170c3547482f8eda9fade825f16ccd2e54d064672eecff1551e0b1899226f813a3592da1165214dd522e897112294b4b48002
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,25 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.6.0] - 2026-02-21
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Historical data query method (`history`) with support for single/multiple stocks and custom intervals by [@lucienLopez](https://github.com/lucienLopez)
|
|
13
|
+
- Include minitest and minitest-mock gems required by newer Ruby 3.4.x versions
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
- Update gem dependencies and Ruby version
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
|
|
21
|
+
- Broken API query generating HTTP 404 Not Found error
|
|
22
|
+
|
|
23
|
+
### Removed
|
|
24
|
+
|
|
25
|
+
- Possibility to use modules in query
|
|
26
|
+
|
|
8
27
|
## [0.5.1] - 2023-07-14
|
|
9
28
|
|
|
10
29
|
### Changed
|
data/README.md
CHANGED
|
@@ -27,13 +27,12 @@ Or install it yourself as:
|
|
|
27
27
|
|
|
28
28
|
Instantiate the `Query` class and use the quotes method on it with either a single stock as a `String` and a single module as a `String` or by passing an `Array` of stocks such as show in the two examples below.
|
|
29
29
|
|
|
30
|
+
### Quotes
|
|
31
|
+
|
|
30
32
|
```ruby
|
|
31
33
|
# Query a stock, with the price module
|
|
32
34
|
|
|
33
35
|
query = BasicYahooFinance::Query.new
|
|
34
|
-
data = query.quotes('AVEM', 'price')
|
|
35
|
-
|
|
36
|
-
# OR the module argument can be left empty as the price module is the default
|
|
37
36
|
data = query.quotes('AVEM')
|
|
38
37
|
|
|
39
38
|
# Query multiple stocks
|
|
@@ -56,43 +55,21 @@ data["AVEM"]["regularMarketPrice"]["raw"]
|
|
|
56
55
|
# 52.72
|
|
57
56
|
```
|
|
58
57
|
|
|
59
|
-
|
|
58
|
+
### Historical Data
|
|
60
59
|
|
|
61
|
-
|
|
60
|
+
Use the `history` method to retrieve historical price data for one or more stocks. It requires a symbol, a start date (`period1`) and an end date (`period2`) as Unix timestamps. An optional `interval` parameter defaults to `"1d"`.
|
|
62
61
|
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
esgScores
|
|
75
|
-
financialData
|
|
76
|
-
fundOwnership
|
|
77
|
-
incomeStatementHistory
|
|
78
|
-
incomeStatementHistoryQuarterly
|
|
79
|
-
indexTrend
|
|
80
|
-
industryTrend
|
|
81
|
-
insiderHolders
|
|
82
|
-
insiderTransactions
|
|
83
|
-
institutionOwnership
|
|
84
|
-
majorDirectHolders
|
|
85
|
-
majorHoldersBreakdown
|
|
86
|
-
netSharePurchaseActivity
|
|
87
|
-
price
|
|
88
|
-
recommendationTrend
|
|
89
|
-
secFilings
|
|
90
|
-
sectorTrend
|
|
91
|
-
summaryDetail
|
|
92
|
-
summaryProfile
|
|
93
|
-
upgradeDowngradeHistory
|
|
94
|
-
pageviews
|
|
95
|
-
quotetype
|
|
62
|
+
```ruby
|
|
63
|
+
query = BasicYahooFinance::Query.new
|
|
64
|
+
|
|
65
|
+
# Daily history for a single stock
|
|
66
|
+
data = query.history('AAPL', 1_700_000_000, 1_710_000_000)
|
|
67
|
+
|
|
68
|
+
# Weekly history
|
|
69
|
+
data = query.history('AAPL', 1_700_000_000, 1_710_000_000, '1wk')
|
|
70
|
+
|
|
71
|
+
# Multiple stocks
|
|
72
|
+
data = query.history(['AAPL', 'GOOG'], 1_700_000_000, 1_710_000_000)
|
|
96
73
|
```
|
|
97
74
|
|
|
98
75
|
## Development
|
|
@@ -101,14 +78,14 @@ After checking out the repo, run `bin/setup` to install dependencies. You can al
|
|
|
101
78
|
|
|
102
79
|
MiniTest is used as test framework and can be run using:
|
|
103
80
|
|
|
104
|
-
$ rake test
|
|
81
|
+
$ bundle exec rake test
|
|
105
82
|
|
|
106
83
|
As linter RuboCop is used and can be run using:
|
|
107
84
|
|
|
108
|
-
$ rake rubocop
|
|
85
|
+
$ bundle exec rake rubocop
|
|
109
86
|
|
|
110
|
-
This gem is being developed with Ruby 3.
|
|
87
|
+
This gem is being developed with Ruby 3.4 but should be downward compatible with at least all supported versions of Ruby.
|
|
111
88
|
|
|
112
89
|
## Contributing
|
|
113
90
|
|
|
114
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/towards/basic_yahoo_finance. If you submit a pull request please make sure to write
|
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/towards/basic_yahoo_finance. If you submit a pull request please make sure to write test cases using MiniTest covering your changes and that there are no RuboCop offenses.
|
data/lib/basic_yahoo_finance.rb
CHANGED
|
@@ -11,32 +11,71 @@ require_relative "basic_yahoo_finance/version"
|
|
|
11
11
|
module BasicYahooFinance
|
|
12
12
|
# Class to send queries to Yahoo Finance API
|
|
13
13
|
class Query
|
|
14
|
-
API_URL = "https://
|
|
14
|
+
API_URL = "https://query1.finance.yahoo.com"
|
|
15
|
+
COOKIE_URL = "https://fc.yahoo.com"
|
|
16
|
+
CRUMB_URL = "https://query1.finance.yahoo.com/v1/test/getcrumb"
|
|
17
|
+
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \
|
|
18
|
+
"Chrome/90.0.4421.0 Safari/537.36 Edg/90.0.810.1"
|
|
15
19
|
|
|
16
20
|
def initialize(cache_url = nil)
|
|
17
21
|
@cache_url = cache_url
|
|
22
|
+
@cookie = fetch_cookie
|
|
23
|
+
@crumb = fetch_crumb(@cookie)
|
|
18
24
|
end
|
|
19
25
|
|
|
20
|
-
def quotes(symbol
|
|
26
|
+
def quotes(symbol)
|
|
21
27
|
hash_result = {}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
|
|
29
|
+
with_http do |http|
|
|
30
|
+
make_symbols_array(symbol).each do |sym|
|
|
31
|
+
uri = URI("#{API_URL}/v7/finance/quote?symbols=#{sym}&crumb=#{@crumb}")
|
|
32
|
+
response = http.request(uri)
|
|
33
|
+
hash_result.store(sym, process_output(JSON.parse(response.body)))
|
|
34
|
+
rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
|
|
35
|
+
hash_result.store(sym, "HTTP Error")
|
|
36
|
+
end
|
|
31
37
|
end
|
|
32
38
|
|
|
33
|
-
|
|
39
|
+
hash_result
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def history(symbol, period1, period2, interval = "1d")
|
|
43
|
+
hash_result = {}
|
|
44
|
+
|
|
45
|
+
with_http do |http|
|
|
46
|
+
make_symbols_array(symbol).each do |sym|
|
|
47
|
+
uri = URI("#{API_URL}/v8/finance/chart/#{sym}?period1=#{period1}&period2=#{period2}&interval=#{interval}&crumb=#{@crumb}")
|
|
48
|
+
response = http.request(uri)
|
|
49
|
+
hash_result.store(sym, process_history_output(JSON.parse(response.body)))
|
|
50
|
+
rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
|
|
51
|
+
hash_result.store(sym, "HTTP Error")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
34
54
|
|
|
35
55
|
hash_result
|
|
36
56
|
end
|
|
37
57
|
|
|
38
58
|
private
|
|
39
59
|
|
|
60
|
+
def with_http
|
|
61
|
+
http = Net::HTTP::Persistent.new
|
|
62
|
+
http.override_headers["User-Agent"] = USER_AGENT
|
|
63
|
+
http.override_headers["Cookie"] = @cookie
|
|
64
|
+
yield(http)
|
|
65
|
+
http.shutdown
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def fetch_cookie
|
|
69
|
+
http = Net::HTTP.get_response(URI(COOKIE_URL), { "Keep-Session-Cookies" => "true" })
|
|
70
|
+
cookies = http.get_fields("set-cookie")
|
|
71
|
+
cookies[0].split(";")[0]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def fetch_crumb(cookie)
|
|
75
|
+
http = Net::HTTP.get_response(URI(CRUMB_URL), { "User-Agent" => USER_AGENT, "Cookie" => cookie })
|
|
76
|
+
http.read_body
|
|
77
|
+
end
|
|
78
|
+
|
|
40
79
|
def make_symbols_array(symbol)
|
|
41
80
|
if symbol.instance_of?(Array)
|
|
42
81
|
symbol
|
|
@@ -45,14 +84,22 @@ module BasicYahooFinance
|
|
|
45
84
|
end
|
|
46
85
|
end
|
|
47
86
|
|
|
48
|
-
def process_output(json
|
|
87
|
+
def process_output(json)
|
|
49
88
|
# Handle error from the API that the code isn't found
|
|
50
|
-
return json["
|
|
89
|
+
return json["error"] unless json["error"].nil?
|
|
51
90
|
|
|
52
|
-
result = json["
|
|
91
|
+
result = json["quoteResponse"]&.dig("result", 0)
|
|
53
92
|
return nil if result.nil?
|
|
54
93
|
|
|
55
|
-
result
|
|
94
|
+
result
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def process_history_output(json)
|
|
98
|
+
# Handle error from the API that the code isn't found
|
|
99
|
+
error = json.dig("chart", "error")
|
|
100
|
+
return error unless error.nil?
|
|
101
|
+
|
|
102
|
+
json
|
|
56
103
|
end
|
|
57
104
|
end
|
|
58
105
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: basic_yahoo_finance
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marc
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: net-http-persistent
|
|
@@ -30,20 +29,6 @@ dependencies:
|
|
|
30
29
|
- - ">="
|
|
31
30
|
- !ruby/object:Gem::Version
|
|
32
31
|
version: 4.0.2
|
|
33
|
-
- !ruby/object:Gem::Dependency
|
|
34
|
-
name: minitest-emoji
|
|
35
|
-
requirement: !ruby/object:Gem::Requirement
|
|
36
|
-
requirements:
|
|
37
|
-
- - "~>"
|
|
38
|
-
- !ruby/object:Gem::Version
|
|
39
|
-
version: '2.0'
|
|
40
|
-
type: :development
|
|
41
|
-
prerelease: false
|
|
42
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
-
requirements:
|
|
44
|
-
- - "~>"
|
|
45
|
-
- !ruby/object:Gem::Version
|
|
46
|
-
version: '2.0'
|
|
47
32
|
description: Very simple Ruby API for Yahoo Finance in order to query the stock market
|
|
48
33
|
email:
|
|
49
34
|
- hello@towards.ch
|
|
@@ -69,7 +54,6 @@ metadata:
|
|
|
69
54
|
homepage_uri: https://towards.ch
|
|
70
55
|
rubygems_mfa_required: 'true'
|
|
71
56
|
source_code_uri: https://github.com/towards/basic_yahoo_finance
|
|
72
|
-
post_install_message:
|
|
73
57
|
rdoc_options: []
|
|
74
58
|
require_paths:
|
|
75
59
|
- lib
|
|
@@ -77,15 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
77
61
|
requirements:
|
|
78
62
|
- - ">="
|
|
79
63
|
- !ruby/object:Gem::Version
|
|
80
|
-
version:
|
|
64
|
+
version: 3.4.0
|
|
81
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
66
|
requirements:
|
|
83
67
|
- - ">="
|
|
84
68
|
- !ruby/object:Gem::Version
|
|
85
69
|
version: '0'
|
|
86
70
|
requirements: []
|
|
87
|
-
rubygems_version: 3.
|
|
88
|
-
signing_key:
|
|
71
|
+
rubygems_version: 3.6.9
|
|
89
72
|
specification_version: 4
|
|
90
73
|
summary: Basic Yahoo Finance API to query stock prices
|
|
91
74
|
test_files: []
|