Download a Twitter User’s Profile Image

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 cr…


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman

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:

  1. Fetch the avatar image for any twitter username
  2. Add the correct file extension (png or jpg)
  3. Optimize the image using jpegtran or pngcrush (some of them were not optimized ?‍♂️)

Here’s an example:

$ ./fetch-twitter-image.sh zachleat
Created zachleat.jpg

(Don’t type the $ there)

And here’s the script. Save the following content as fetch-twitter-image.sh:

wget --quiet -O $1.jpg https://twitter.com/$1/profile_image?size=bigger

file="$1.jpg"
type=$(file-type $file)

if [[ $type == *"image/jpeg"* ]]
then
jpegtran "$file" > "$file_"
mv "$file_" "$file"
echo "Created $1.jpg"
elif [[ $type == *"image/png"* ]]
then
pngcrush -brute "$file"
rm $1.jpg
mv pngout.png $1.png
echo "Created $1.png"
fi

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
  • file-type-cli npm install -g file-type-cli
  • jpegtran npm install -g jpegtran-bin
  • pngcrush 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:

[{
"twitterUsername": "zachleat"
},{
"twitterUsername": "filamentgroup"
}]

Iterate over data.json using jq and fetch all the images.

for handle in $(cat data.json | jq -r '.[] | .twitterUsername'); do
./fetch-twitter-image.sh $handle
done


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman


Print Share Comment Cite Upload Translate
APA
Zach Leatherman | Sciencx (2023-01-31T12:57:24+00:00) » Download a Twitter User’s Profile Image. Retrieved from https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/.
MLA
" » Download a Twitter User’s Profile Image." Zach Leatherman | Sciencx - Wednesday January 16, 2019, https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/
HARVARD
Zach Leatherman | Sciencx Wednesday January 16, 2019 » Download a Twitter User’s Profile Image., viewed 2023-01-31T12:57:24+00:00,<https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/>
VANCOUVER
Zach Leatherman | Sciencx - » Download a Twitter User’s Profile Image. [Internet]. [Accessed 2023-01-31T12:57:24+00:00]. Available from: https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/
CHICAGO
" » Download a Twitter User’s Profile Image." Zach Leatherman | Sciencx - Accessed 2023-01-31T12:57:24+00:00. https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/
IEEE
" » Download a Twitter User’s Profile Image." Zach Leatherman | Sciencx [Online]. Available: https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/. [Accessed: 2023-01-31T12:57:24+00:00]
rf:citation
» Download a Twitter User’s Profile Image | Zach Leatherman | Sciencx | https://www.scien.cx/2019/01/16/download-a-twitter-users-profile-image/ | 2023-01-31T12:57:24+00:00
https://github.com/addpipe/simple-recorderjs-demo