-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock-parser.rb
56 lines (48 loc) · 1.27 KB
/
stock-parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'csv'
require 'dotenv'
require 'sequel'
require 'net/http'
require 'uri'
Dotenv.load
DB = Sequel.connect("mysql2://#{ENV['DB_USER']}:#{ENV['DB_PASSWORD']}@localhost/financial")
def create_db
DB.execute('CREATE DATABASE IF NOT EXISTS financial')
end
def create_stocks_table
# index on code, date
DB.create_table :stocks do
String :code
Date :date
Decimal :open, size: [10, 2]
Decimal :high, size: [10, 2]
Decimal :low, size: [10, 2]
Decimal :close, size: [10, 2]
Integer :volume
index :code
unique [:code, :date]
end
end
def delete_stocks_table
DB.execute('DROP TABLE stocks')
end
def download(date:)
uri = URI.parse("http://float.com.au/download/#{date}.csv")
Net::HTTP.get_response(uri).response.body
end
def import(date: nil)
stocks = DB[:stocks]
stocks_csv = download(date: date) # offline: CSV.read('data/20170619.csv')
CSV.parse(stocks_csv).each do |row|
stocks.insert(code: row[0],
date: row[1],
open: row[2],
high: row[3],
low: row[4],
close: row[5],
volume: row[6])
end
end
puts "importing #{ARGV[0]}"
# check if date is weekend or public holiday don't do it
import(date: ARGV[0])
puts "importing finished"