Skip to content

Instantly share code, notes, and snippets.

@timgreen
Last active May 18, 2017 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timgreen/9647ca29f74df29d3a0c to your computer and use it in GitHub Desktop.
Save timgreen/9647ca29f74df29d3a0c to your computer and use it in GitHub Desktop.
#!/bin/bash
extract_icon() {
gawk 'match($0, /<img\s+class="cover-image"\s+src="([^"]+)"/, m) {print m[1]}' | head -n 1
}
format_icon_url() {
url=$(cat | sed "s/=.*$//" | sed "s/^https://")
echo -n "https:${url}=h32"
}
get_app_icon() {
app_url="$1"
curl "$app_url" 2> /dev/null | extract_icon | format_icon_url
}
extract_app_url() {
echo "$1" | sed 's/^.*(//' | sed 's/).*$//'
}
GIST_ID=9647ca29f74df29d3a0c
fetch_gist_repo() {
if [ -d $GIST_ID/ ]; then
cd $GIST_ID
git pull
cd ..
else
git clone https://gist.github.com/${GIST_ID}.git
fi
}
FILE="$GIST_ID/My Preferred Apps.md"
TMP_FILE="$FILE.tmp"
process_file() {
IFS=''
while read line; do
if echo "$line" | grep "\[app icon\]" > /dev/null; then
app_url=$(extract_app_url "$line")
echo "$app_url" > /dev/stderr
app_icon=$(get_app_icon "$app_url")
echo "$app_icon" > /dev/stderr
echo "$line" | sed 's|\[app icon\]([^)]*)|[app icon]('"$app_icon"')|'
else
echo "$line";
fi
done < "$FILE" > "$TMP_FILE"
}
main() {
fetch_gist_repo
process_file
mv -f "$TMP_FILE" "$FILE"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment