Skip to content

Instantly share code, notes, and snippets.

@ulfandersson
Created June 28, 2018 19:54
Show Gist options
  • Save ulfandersson/089cda423d08bd26b3e08068d4d5c32b to your computer and use it in GitHub Desktop.
Save ulfandersson/089cda423d08bd26b3e08068d4d5c32b to your computer and use it in GitHub Desktop.
Download Excel data for a company from borsdata.se. NOTE! REQUIRES BÖRSDATA SUBSCRIPTION.
#!/bin/bash
#
# Copyright 2018 Ulf Andersson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
if [[ -z "$BORSDATA_COOKIE" ]]; then
echo -e "\033[0;31mNeed environment variable BORSDATA_COOKIE\033[0m"
echo "Step 1. Login to https://borsdata.se with Chrome."
echo "Step 2. Navigate to chrome://settings/cookies/detail?site=borsdata.se, find"
echo " the cookie with the name 'borsdata' and copy it's content."
echo "Step 3. Run export BORSDATA_COOKIE=<copied value from step 2>"
echo "Step 4. Run this script again with a company name as argument"
exit -1
fi
SNAPSHOT_FILENAME="borsdataCompanyDataSnapshot.json"
if [ ! -f "$SNAPSHOT_FILENAME" ] || [ `stat --format=%Y $SNAPSHOT_FILENAME` -le $(( `date +%s` - 86400 )) ]; then
curl 'https://borsdata.se/complist/GetCompanies' -H 'origin: https://borsdata.se' -H "cookie: borsdata=$BORSDATA_COOKIE;"\
-H 'content-type: application/json;charset=UTF-8' -H 'accept: application/json, text/plain, */*' -H 'authority: borsdata.se'\
--data-binary '{"filter":{"KpiFilter":[{"CategoryId":9,"AdditionalId":151,"KpiId":151,"CalculationType":2,"Calculation":20,"CalcTime":1}]}}' >$SNAPSHOT_FILENAME
fi
if [[ -z "$1" ]]; then
echo "Please enter a company name! (use quotes if multiple words)"
exit -1
fi
COMPANY=$1
COMPANY_URL=$(jq -r ".data[] | select(.Name==\"$COMPANY\") | .CountryUrlName" $SNAPSHOT_FILENAME)
if [[ -z "$COMPANY_URL" ]]; then
echo "Company $COMPANY not found. (Note that the search is case sensitive)."
exit -1
fi
echo "Found Börsdata page for $COMPANY (https://borsdata.se/$COMPANY_URL/analys)."
echo "Downloading excel file to \"$PWD/$COMPANY.xls\"."
curl -sS 'https://borsdata.se/analysis/excel' -G --data-urlencode "companyUrl=$COMPANY_URL" -H "cookie: borsdata=$BORSDATA_COOKIE;"\
-H 'accept: */*' -H 'authority: borsdata.se' -H 'accept-encoding: gzip, deflate, br' --compressed >"$1.xls"
file "$1.xls" | grep "Composite Document File" >/dev/null || echo "WARNING! Downloaded file seems corrupt."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment