Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Created June 20, 2012 16:41
Show Gist options
  • Save oliveiraev/2960853 to your computer and use it in GitHub Desktop.
Save oliveiraev/2960853 to your computer and use it in GitHub Desktop.
Baixa todos os tuítes de um usuário com timeline pública
#!/bin/bash
if [ ! -d './downloaded_tweets' ]; then
mkdir downloaded_tweets
if [ ! -d './downloaded_tweets' ]; then
echo "Can't create output dir." > /dev/stderr
exit 1
fi
fi
cd downloaded_tweets
while [ "$1" != "" ]; do
tweets="`curl -s https://api.twitter.com/1/users/lookup.json?screen_name=$1`"
tweets=`echo $tweets | sed -r -e 's!.*?"statuses_count":([0-9]+).*!\1!'`
pages=$(( $tweets / 200 ))
if [ $(( $tweets % 200 )) ]; then pages=$(( $pages + 1 )); fi
i=1
while [ $i -le $pages ]; do
echo "Downloading from $1: Page $i of $pages"
curl -s https://api.twitter.com/1/statuses/user_timeline.xml\?screen_name=$1\&count=200\&page=$i >> /tmp/tweets.xml
i=$(( i + 1 ))
done
sed -r -i -e's/<\?xml version="1.0" encoding="UTF-8"\?>//' -e 's/<statuses type="array">//' -e 's!</statuses>!!' /tmp/tweets.xml
echo '<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">' > $1-tweets.xml
cat /tmp/tweets.xml >> $1-tweets.xml
echo '</statuses>' >> $1-tweets.xml
rm /tmp/tweets.xml
shift
done
@oliveiraev
Copy link
Author

Usage: $ ./download_tweets.sh twitter twitterapi anotheruser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment