Skip to content

Instantly share code, notes, and snippets.

@gorango
Last active May 25, 2018 00:12
Show Gist options
  • Save gorango/b4e9509c455f13af04fb2aa056889329 to your computer and use it in GitHub Desktop.
Save gorango/b4e9509c455f13af04fb2aa056889329 to your computer and use it in GitHub Desktop.
Download any Google Font with its corresponding CSS rules.
#!/bin/bash
FONT_NAME="$1"
FILE_NAME=$(echo $FONT_NAME | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
URL_NAME=$(echo $FONT_NAME | tr ' ' '+')
FILE=$FILE_NAME"-font.css"
FONT_FOLDER="web-font"
AGENT_WOFF="Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"
AGENT_WOFF2="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like $
# download css as IE11 for .woff
wget https://fonts.googleapis.com/css?family=$URL_NAME:100,200,300,400,500,600,700,800,900 -O$
sed "s/local('.*'), //" > $FILE
# get links dirname
URL=$(cat $FILE | tr '()' \\n | grep https\*:// | head -n 1)
DIRNAME=$(dirname $URL)
rm -rf $FONT_FOLDER
mkdir $FONT_FOLDER
# download all http links
cat $FILE | tr '()' \\n | grep https\*:// | parallel --gnu "wget {} -P $FONT_FOLDER"
# replace links to local
sed -e "s*"$DIRNAME"*\./web-font*" $FILE > $FILE".tmp" && mv $FILE".tmp" $FILE
@gorango
Copy link
Author

gorango commented May 25, 2018

chmod +x get-google-fonts.sh
./get-google-fonts "Open Sans"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment