Merge pull request #4 from captn3m0/v2
Diff
.gitignore | 3 ++-
.ruby-version | 2 +-
Gemfile | 1 +
Gemfile.lock | 4 ++++
Makefile | 8 --------
README.md | 5 +++--
_config.yml | 2 ++
netlify.toml | 6 ++++++
_plugins/amfi.rb | 46 ++++++++++++++++++++++++++++++++++++----------
9 files changed, 48 insertions(+), 29 deletions(-)
@@ -1,6 +1,7 @@
_site
.sass-cache
.jekyll-cache
.jekyll-metadata
vendor
nav/IN*
nav/IN*
funds.db*
@@ -1,1 +1,1 @@
3.1.2
3.0.5
@@ -1,6 +1,7 @@
source "https://rubygems.org"
gem "jekyll", "~> 4.2.0"
gem "jekyll-theme-modernist", "~> 0.2"
gem "sqlite3", "~> 1.6.2", force_ruby_platform: true
@@ -47,6 +47,7 @@
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.4.0)
mini_portile2 (2.8.1)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (5.0.1)
@@ -58,6 +59,8 @@
safe_yaml (1.0.5)
sassc (2.4.0)
ffi (~> 1.9)
sqlite3 (1.6.2)
mini_portile2 (~> 2.8.0)
terminal-table (2.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
unicode-display_width (1.8.0)
@@ -70,6 +73,7 @@
DEPENDENCIES
jekyll (~> 4.2.0)
jekyll-theme-modernist (~> 0.2)
sqlite3 (~> 1.6.2)
tzinfo (~> 1.2)
tzinfo-data
wdm (~> 0.1.1)
@@ -1,8 +1,0 @@
amfi:
curl --retry 10 \
--connect-timeout 30 \
--retry-max-time 100 \
--silent "https://www.amfiindia.com/spages/NAVAll.txt" | \
grep ';' > "_data/nav.csv";
all: amfi
@@ -17,12 +17,13 @@
This service uses data from 2 sources:
- Kuvera
- AMFI
- AMFI, via [Historical Mutual Fund Database](https://github.com/captn3m0/historical-mf-data)
## how
- A mapping between Kuvera Mutual Fund IDs and ISIN data is fetched from [kuvera-mutual-funds-lookup](https://github.com/captn3m0/kuvera-mutual-funds-lookup) as an automatically updating submodule.
- NAV data from AMFI is updated daily and saved in [_data/nav.csv](https://github.com/captn3m0/mf.captnemo.in/blob/main/_data/nav.csv)
- NAV data from AMFI is updated daily and published as a SQLite Database by https://github.com/captn3m0/historical-mf-data.
- This website takes the above, and publishes it via Netlify.
## license
@@ -8,6 +8,8 @@
github:
repository_url: https://github.com/captn3m0/mf.captnemo.in
exclude:
- funds.db
- funds.db.zst
- .sass-cache/
- .jekyll-cache/
- gemfiles/
@@ -1,5 +1,11 @@
[build]
command = """
wget https://github.com/captn3m0/historical-mf-data/releases/latest/download/funds.db.zst
unzstd funds.db.zst
echo 'CREATE INDEX "date" ON "nav" ("date","scheme_code")' | sqlite3 funds.db
echo 'CREATE INDEX "nav-scheme" ON "nav" ("scheme_code")' | sqlite3 funds.db
echo 'CREATE INDEX "securities-scheme" ON "securities" ("scheme_code")' | sqlite3 funds.db
echo 'CREATE INDEX "securities-isin" ON "securities" ("isin")' | sqlite3 funds.db
jekyll build
"""
publish = "_site"
@@ -1,25 +1,37 @@
require 'csv'
require 'json'
require 'sqlite3'
DB = SQLite3::Database.new('funds.db', :readonly => true)
def get_historical_nav(isin)
DB.execute("SELECT date, nav from nav_by_isin WHERE isin='#{isin}' ORDER BY date")
end
def get_latest_nav(isin)
DB.execute("SELECT nav,date from nav_by_isin WHERE isin='#{isin}' ORDER BY date DESC LIMIT 1")[0]
end
def get_mutual_fund_schemes
DB.execute("SELECT isin,scheme_name as name,S1.scheme_code FROM securities S1
LEFT JOIN schemes S2 ON S1.scheme_code = S2.scheme_code")
end
Jekyll::Hooks.register :site, :after_init do |site|
data = CSV.read('_data/nav.csv', col_sep: ';', headers: ['code', 'isin', 'reinvestment_isin', 'name', 'nav', 'date'])[1..]
data.each do |nav_data|
date = Date.parse(nav_data['date']).strftime('%Y-%m-%d')
get_mutual_fund_schemes.each do |isin, scheme_name|
puts "[+] #{isin}, #{scheme_name}"
nav = get_latest_nav(isin)
data = {
'ISIN'=> nav_data['isin'],
'name'=> nav_data['name'],
'nav' => nav_data['nav'].to_f,
'date'=> date
'ISIN'=> isin,
'name'=> scheme_name,
}
File.write("nav/#{data['ISIN']}", data.to_json) if data['ISIN'].length == 12
if nav_data['reinvestment_isin'].length == 12
data = {
'ISIN'=> nav_data['reinvestment_isin'],
'name'=> nav_data['name'] + " (Reinvestment)",
'nav' => nav_data['nav'],
'date'=> date
}
File.write("nav/#{data['ISIN']}", data.to_json)
if nav
data['nav'] = nav[0]
data['date']= nav[1]
end
data['historical_nav'] = get_historical_nav(data['ISIN'])
File.write("nav/#{data['ISIN']}", data.to_json)
end
end