Created
May 17, 2017 21:45
-
-
Save Mister-Meeseeks/df985c5e3abb1be88004319f11ebe3fb to your computer and use it in GitHub Desktop.
Bash script for Yahoo Finance historical data. Works with new cookie-based authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -eu | |
# Client script to pull Yahoo Finance historical data, off of its new cookie | |
# authenticated site. Start/End Date args can be any GNU readable dates. | |
# Script requires: GNU date, curl and bash shell | |
symbol=$1 | |
startDate=$2 | |
endDate=$3 | |
startEpoch=$(date -d "$startDate" '+%s') | |
endEpoch=$(date -d "$endDate" '+%s') | |
cookieJar=$(mktemp) | |
function cleanup() { | |
rm $cookieJar | |
} | |
trap cleanup EXIT | |
function parseCrumb() { | |
sed 's+}+\n+g' \ | |
» | grep CrumbStore \ | |
» | cut -d ":" -f 3 \ | |
» | sed 's+"++g' | |
} | |
function extractCrumb() { | |
crumbUrl="https://ca.finance.yahoo.com/quote/$symbol/history?p=$symbol" | |
curl -s --cookie-jar $cookieJar $crumbUrl \ | |
» | parseCrumb | |
} | |
crumb=$(extractCrumb) | |
baseUrl="https://query1.finance.yahoo.com/v7/finance/download/" | |
args="$symbol?period1=$startEpoch&period2=$endEpoch&interval=1d&events=history" | |
crumbArg="&crumb=$crumb" | |
sheetUrl="$baseUrl$args$crumbArg" | |
curl -s --cookie $cookieJar "$sheetUrl" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment