yquotes 0.1.5 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8838912fb8a6fbb4b2bb4763747ff10ed0b3064
4
- data.tar.gz: 74a817327c6e1a4ef4da6eaf569448dbf31ff263
3
+ metadata.gz: 9b08f86c5ff3fee6eee461f42329c84d35493c66
4
+ data.tar.gz: a7e15f17170b6fe991613dd67c31f659d3e4be9e
5
5
  SHA512:
6
- metadata.gz: b955472d0f609237043ac7d3a9c70b76f1d2270c12686ec36151b2f1f114b12d5b14c4f52dcffdaf1e6fa0ff88cac665f3bee3aab0caefe45d1fca674219709f
7
- data.tar.gz: c7f2a6f267546409eceae72332fd898e3365356a0c82621be4e5677f65962221c8a25042ce8dcdf10dbfee1cdee40a14d585a00a77617eab84f9ad0d5ef5e726
6
+ metadata.gz: fe8b9bd2d89823dc2a88f122f478e799e82510b752bc745a54fc2fcb95943eb1ffea4219c9ea737eb56c85ce70796cb379ffd391976f1b1feea4d415a3322632
7
+ data.tar.gz: 2697dcda93eb2c7f43798c72865b794239a3b8db3480541692d29d38a4cd1ead669b9864317e4891aacb4e006597ff6ed9d76085dac53e3bebba2ac5b8d09c39
data/README.md CHANGED
@@ -16,10 +16,12 @@ gem 'yquotes'
16
16
 
17
17
  And then execute:
18
18
 
19
+ $ bundle config build.nokogiri --use-system-libraries
19
20
  $ bundle
20
21
 
21
22
  Or install it yourself as:
22
23
 
24
+ $ gem install nokogiri -- --use-system-libraries
23
25
  $ gem install yquotes
24
26
 
25
27
  ## Usage
@@ -1,3 +1,3 @@
1
1
  module YQuotes
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.7"
3
3
  end
data/lib/yquotes/yahoo.rb CHANGED
@@ -1,18 +1,39 @@
1
1
  require 'open-uri'
2
2
  require 'csv'
3
3
  require 'date'
4
+ require 'nokogiri'
4
5
 
5
6
 
6
7
  module YQuotes
7
8
 
8
- QUOTE_ENDPOINT="https://ichart.finance.yahoo.com/table.csv?"
9
-
9
+ COOKIE_URL = 'https://finance.yahoo.com/quote/AAPL/history?p=AAPL'
10
+ CRUMB_PATTERN = /\"CrumbStore\":{\"crumb\":\"(?<crumb>[^"]+)/
11
+ QUOTE_ENDPOINT = "https://query1.finance.yahoo.com/v7/finance/download/%{symbol}?"
12
+
10
13
  class Yahoo
11
14
 
15
+ # Get cookie and crumb
16
+ def initialize
17
+
18
+ # get cookie
19
+ page = open(COOKIE_URL)
20
+ @cookie = page.meta['set-cookie'].split('; ', 2).first
21
+
22
+ # get crumb
23
+ scripts = Nokogiri::HTML(page).css('script')
24
+ scripts.each do |s|
25
+ if s.text.include? 'CrumbStore'
26
+ pattern = s.text.match(CRUMB_PATTERN)
27
+ @crumb = pattern['crumb']
28
+ break
29
+ end
30
+ end
31
+ end
32
+
12
33
  # fetch_csv: fetch historical quotes in csv format
13
34
  def fetch_csv(ticker, start_date=nil, end_date=nil, period='d')
14
- url = QUOTE_ENDPOINT + build_params(ticker, start_date, end_date, period)
15
- connection = open(url)
35
+ url = build_url(ticker, start_date, end_date, period)
36
+ connection = open(url, 'Cookie' => @cookie)
16
37
  data = CSV.parse(connection.read, :converters => :numeric)
17
38
  raise "Yahoo.fetch_csv unable to fetch data" unless data.is_a? Array
18
39
  return data
@@ -22,43 +43,38 @@ module YQuotes
22
43
  alias_method :get_data, :fetch_csv
23
44
 
24
45
  private
46
+
25
47
  # build_params: build parameters for get query
26
- def build_params(ticker, start_date=nil, end_date=nil, period='d')
48
+ def build_url(ticker, start_date=nil, end_date=nil, period='d')
49
+
50
+ url = QUOTE_ENDPOINT
51
+ url = url %{:symbol => URI.escape(ticker).upcase}
27
52
 
28
53
  params = {
29
- :s => URI.escape(ticker)
54
+ :crumb => URI.escape(@crumb),
55
+ :events => 'history',
56
+ :interval => '1d'
30
57
  }
31
58
 
32
59
  # sanitize date
33
- start_date = get_date(start_date)
34
- end_date = get_date(end_date)
35
-
36
- if start_date
37
- params[:a] = start_date.month - 1
38
- params[:b] = start_date.day
39
- params[:c] = start_date.year
40
- end
41
-
42
- if end_date
43
- params[:d] = end_date.month - 1
44
- params[:e] = end_date.day
45
- params[:f] = end_date.year
46
- end
60
+ params[:period1] = get_date(start_date).to_i unless start_date.nil?
61
+ params[:period2] = get_date(end_date).to_i unless end_date.nil?
47
62
 
48
- params[:g] = period if period == "d" or period == "m" or period == "y"
63
+ params[:interval] = "1d" if period == "d"
64
+ params[:interval] = "1mo" if period == "m"
49
65
 
50
- "#{params.map { |k,v| "#{k}=#{v}" }.join("&")}"
66
+ url + "#{params.map { |k,v| "#{k}=#{v}" }.join("&")}"
51
67
  end
52
68
 
53
69
  # get_date: get date from String
54
70
  def get_date(d)
55
- return nil unless d
56
- return d if d.is_a? Date
71
+ return nil if d.nil?
72
+ return d.to_time if d.is_a? Date
57
73
 
58
74
  if d.is_a? String
59
75
 
60
76
  begin
61
- dt = Date.parse(d)
77
+ dt = Date.parse(d).to_time
62
78
  rescue Exception => e
63
79
  raise "invalid param #{d} - date should be in yyyy-mm-dd format"
64
80
  end
data/yquotes.gemspec CHANGED
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
 
27
27
  spec.add_dependency "daru", "~> 0.1.5"
28
+ spec.add_dependency "nokogiri", "~> 1.7.2"
29
+
28
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yquotes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - P Choudhary
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2017-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.1.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.7.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.7.2
69
83
  description:
70
84
  email:
71
85
  - pankaj17n@outlook.com
@@ -106,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
120
  version: '0'
107
121
  requirements: []
108
122
  rubyforge_project:
109
- rubygems_version: 2.4.5.1
123
+ rubygems_version: 2.6.8
110
124
  signing_key:
111
125
  specification_version: 4
112
126
  summary: Get historical quotes from Yahoo Finance