Skip to content

Instantly share code, notes, and snippets.

@ajtazer
Created June 3, 2025 10:18
Show Gist options
  • Save ajtazer/b4859ac2acbcf13f453a53f54d78d99a to your computer and use it in GitHub Desktop.
Save ajtazer/b4859ac2acbcf13f453a53f54d78d99a to your computer and use it in GitHub Desktop.
Export your Instagram cookies in Netscape format using a browser extension like Cookie-Editor (choose Netscape format on export). Run the script and enter the full path to your cookie file and the Instagram username (without @) when prompted. The script fetches and downloads reels/videos from that user’s posts using your authenticated session.
#!/bin/bash
# Prompt for cookie file path (Netscape format)
read -p "Enter path to your cookies file (Netscape format): " COOKIE_FILE
if [[ ! -f "$COOKIE_FILE" ]]; then
echo "Cookie file not found! Exiting."
exit 1
fi
# Prompt for username (no @)
read -p "Enter Instagram username (without @): " USERNAME
# URL encode the username JSON part
VARIABLES=$(jq -n --arg user "$USERNAME" '
{
data: {
include_relationship_info: true,
latest_besties_reel_media: true,
latest_reel_media: true
},
username: $user,
"__relay_internal__pv__PolarisFeedShareMenurelayprovider": false
}' | jq -sRr @uri)
URL="https://www.instagram.com/graphql/query?variables=$VARIABLES&doc_id=7898261790222653&server_timestamps=true"
echo "Fetching reels for user: $USERNAME"
echo "URL: $URL"
# Extract cookies as header from Netscape file using curl --cookie
COOKIE_HEADER=""
# We'll use curl with the cookie file directly
curl -s -b "$COOKIE_FILE" -A 'Mozilla/5.0' "$URL" \
| jq -r '.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges[]
| "\(.node.taken_at) \(.node.caption.text | gsub("[^a-zA-Z0-9 _-]"; "") | gsub("\\s+"; " ") | .[0:50]) \(.node.video_versions[0].url)"' \
| while IFS= read -r line; do
timestamp=$(echo "$line" | cut -d' ' -f1)
caption=$(echo "$line" | cut -d' ' -f2- | rev | cut -d' ' -f2- | rev)
url=$(echo "$line" | awk '{print $NF}')
safe_caption=$(echo "$caption" | sed 's/[^a-zA-Z0-9._-]/_/g')
filename="${timestamp}_${safe_caption}.mp4"
echo "Downloading video to: $filename"
wget -q --show-progress -O "$filename" "$url"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment