Download images or videos from an X (Twitter) link to ~/Downloads.
Dependencies
yt-dlp(installed)
Execution flow
1. Parse input
Extract the X/Twitter URL from the user input. Supported formats:
https://x.com/user/status/123456https://twitter.com/user/status/123456https://mobile.twitter.com/user/status/123456- URL with query parameters (yt-dlp automatically handles
?s=20and other tracking parameters)
Shortened links (t.co) are resolved first:curl -Ls -o /dev/null -w '%{url_effective}' "SHORT_URL"
If the user does not provide a URL, use AskUserQuestion to request one.
2. Try downloading directly (video first)
Download directly with yt-dlp without probing first:
yt-dlp -o "~/Downloads/%(uploader)s_%(id)s.%(ext)s" "URL"
If successful (video tweet), done. Skip to step 4 to report the result.
3. When video download fails, extract images
yt-dlp may error on image-only tweets. In that case use --dump-json to extract image URLs:
yt-dlp --dump-json "URL" 2>&1
Interpret the result:
- JSON contains a
thumbnailsarray → extract image URLs - JSON is empty or errors with
no video→ The tweet has no media; inform the user "This tweet does not contain downloadable images or videos" - error contains
login/authentication→ login required (see Troubleshooting) - Other errors → report the specific error information
Image download:
From the JSON's thumbnails array, extract all image URLs, replace name=small or name=medium with name=orig to get the original image, then download one by one:
curl -L -o ~/Downloads/tweet_ID_1.jpg "https://pbs.twimg.com/media/xxx?format=jpg&name=orig"
curl -L -o ~/Downloads/tweet_ID_2.jpg "https://pbs.twimg.com/media/yyy?format=jpg&name=orig"
The extension follows the format parameter in the URL (jpg/png/webp).
4. Report the result
After downloading, use ls -lh to list the downloaded files: filename, size, path.
Troubleshooting
Login required
When yt-dlp errors with login / Sign in / age-restricted , add --cookies-from-browser chrome:
yt-dlp --cookies-from-browser chrome -o "~/Downloads/%(uploader)s_%(id)s.%(ext)s" "URL"
Tweet has no media
Text-only tweets have no downloadable media. Just tell the user.