Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # bash twitter client using json api
- #
- txtred=$(tput setaf 1) # Red
- txtgrn=$(tput setaf 2) # Green
- txtylw=$(tput setaf 3) # Yellow
- txtblu=$(tput setaf 4) # Blue
- txtpur=$(tput setaf 5) # Purple
- txtcyn=$(tput setaf 6) # Cyan
- txtwht=$(tput setaf 7) # White
- txtrst=$(tput sgr0)
- i=1
- COLOR=0
- unique=`date +"%Y-%m-%d-%T"`
- AUTHKEY=`cat ~/twitter.key|head -1`
- print_tweets(){
- cat /tmp/twitter.$unique.json|jq ".statuses[].text" >/tmp/twitter.$unique.out
- while read line ;
- do
- date=`date +"%Y-%m-%d-%T"`
- if [ $COLOR -eq 0 ] ; then
- # echo "${txtred} -- $i --"
- echo
- echo "${txtwht} $date ${txtred}$line"
- else
- # echo "${txtred} -- $i --"
- echo
- echo "${txtwht} $date ${txtgrn}$line"
- fi
- let i+=1
- done </tmp/twitter.$unique.out
- if [ $COLOR -eq 0 ] ; then
- COLOR=1
- else
- COLOR=0
- fi
- }
- if [ $# -eq 0 ] ; then
- echo
- echo "usage: twitter <searchstring> [<lang>]"
- echo
- exit 0
- fi
- if [ $# -gt 1 ] ; then
- LANG="&lang=$2"
- else
- LANG=""
- fi
- SEARCH=`echo $1|sed "s/#/%23/g"`
- SEARCH=`echo $SEARCH|sed "s/@/%40/g"`
- SEARCH=`echo $SEARCH|sed "s/ /%20/g"`
- SEARCH=`echo $SEARCH|sed "s/or/OR/g"`
- SEARCH=`echo $SEARCH|sed "s/and/AND/g"`
- echo $SEARCH
- curl -s -H"Authorization: Bearer $AUTHKEY" "https://api.twitter.com/1.1/search/tweets.json?q="$SEARCH"$LANG&count=20&result_type=mixed" >/tmp/twitter.$unique.json
- print_tweets
- LASTID=`cat /tmp/twitter.$unique.json|jq ".statuses[].id_str"|head -1|cut -d"\"" -f2`
- while [ true ] ; do
- curl -s -H"Authorization: Bearer $AUTHKEY" "https://api.twitter.com/1.1/search/tweets.json?q="$SEARCH"$LANG&count=10&result_type=mixed&since_id=$LASTID" >/tmp/twitter.$unique.json
- COUNT=`cat /tmp/twitter.$unique.json|jq ".statuses[].id_str"|wc -l`
- if [ "$COUNT" != "0" ] ; then
- LASTID=`cat /tmp/twitter.$unique.json|jq ".statuses[].id_str"|head -1|cut -d"\"" -f2`
- print_tweets
- fi
- sleep 3
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement