Download a Twitter User’s Profile ImageThis is a note written on 14 Jan 2019. It is short. It is terse. In total I’ve written 4 notes. Subscribe to the Notes RSS feed.
The 11ty.io web site uses avatar images for its Testimonial and Sites Using Eleventy features. I wrote a script to automatically download those avatar images based on usernames stored in data files in the 11ty.io repository.
In that same vein, I created a simpler utility borne from that script. It will:
- Fetch the avatar image for any twitter username
- Add the correct file extension (
- Optimize the image using
pngcrush(some of them were not optimized 🤷♂️)
Here’s an example:
$ ./fetch-twitter-image.sh zachleat
(Don’t type the
And here’s the script. Save the following content as
wget --quiet -O $1.jpg https://twitter.com/$1/profile_image?size=bigger
if [[ $type == *"image/jpeg"* ]]
jpegtran "$file" > "$file_"
mv "$file_" "$file"
echo "Created $1.jpg"
elif [[ $type == *"image/png"* ]]
pngcrush -brute "$file"
mv pngout.png $1.png
echo "Created $1.png"
Don’t forget to add execute permissions to this file:
chmod +x fetch-twitter-image.sh
Install the Dependencies
- wget: You probably already have this
npm install -g file-type-cli
npm install -g jpegtran-bin
npm install -g pngcrush-bin
(This script should be its own module on npm, huh?)
Bonus tip: Iterate over a Data File
Given this arbitrary
data.json JSON file:
jq and fetch all the images.
for handle in $(cat data.json | jq -r '. | .twitterUsername'); do
Zach is a Web Developer with the award winning Filament Group. He’s currently fixated on web fonts and static site generators. His public speaking résumé includes talks in seven different countries at events like Smashing Conference, CSSConf, and The White House. He also helps herd NEJS CONF and the NebraskaJS meetup. Read more about Zach » Follow @zachleat