Skip to content

Instantly share code, notes, and snippets.

@Mister-Meeseeks
Created May 17, 2017 21:45
Show Gist options
  • Save Mister-Meeseeks/df985c5e3abb1be88004319f11ebe3fb to your computer and use it in GitHub Desktop.
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
#!/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