SHARE
TWEET

bash twitter client

x1622sec Nov 14th, 2015 (edited) 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # bash twitter client using json api
  4. #
  5. txtred=$(tput setaf 1) # Red
  6. txtgrn=$(tput setaf 2) # Green
  7. txtylw=$(tput setaf 3) # Yellow
  8. txtblu=$(tput setaf 4) # Blue
  9. txtpur=$(tput setaf 5) # Purple
  10. txtcyn=$(tput setaf 6) # Cyan
  11. txtwht=$(tput setaf 7) # White
  12. txtrst=$(tput sgr0)
  13. i=1
  14. COLOR=0
  15. unique=`date +"%Y-%m-%d-%T"`
  16. AUTHKEY=`cat ~/twitter.key|head -1`
  17.  
  18. print_tweets(){
  19.  
  20.   cat /tmp/twitter.$unique.json|jq ".statuses[].text" >/tmp/twitter.$unique.out
  21.  
  22.   while read line ;
  23.   do
  24.     date=`date +"%Y-%m-%d-%T"`
  25.     if [ $COLOR -eq 0 ] ; then
  26.        # echo "${txtred} -- $i --"
  27.        echo
  28.        echo "${txtwht} $date ${txtred}$line"
  29.     else
  30.        # echo "${txtred} -- $i --"
  31.        echo
  32.        echo "${txtwht} $date ${txtgrn}$line"
  33.     fi
  34.     let i+=1
  35.   done </tmp/twitter.$unique.out
  36.  
  37.   if [ $COLOR -eq 0 ] ; then
  38.     COLOR=1
  39.   else
  40.     COLOR=0
  41.   fi
  42.  
  43. }
  44.  
  45. if [ $# -eq 0 ] ; then
  46.   echo
  47.   echo "usage: twitter <searchstring> [<lang>]"
  48.   echo
  49.   exit 0
  50. fi
  51.  
  52. if [ $# -gt 1 ] ; then
  53.   LANG="&lang=$2"
  54. else
  55.   LANG=""
  56. fi
  57.  
  58. SEARCH=`echo $1|sed "s/#/%23/g"`
  59. SEARCH=`echo $SEARCH|sed "s/@/%40/g"`
  60. SEARCH=`echo $SEARCH|sed "s/ /%20/g"`
  61. SEARCH=`echo $SEARCH|sed "s/or/OR/g"`
  62. SEARCH=`echo $SEARCH|sed "s/and/AND/g"`
  63.  
  64. echo $SEARCH
  65. 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
  66.  
  67. print_tweets
  68.  
  69. LASTID=`cat /tmp/twitter.$unique.json|jq ".statuses[].id_str"|head -1|cut -d"\"" -f2`
  70.  
  71. while [ true ] ; do
  72.   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
  73.   COUNT=`cat /tmp/twitter.$unique.json|jq ".statuses[].id_str"|wc -l`
  74.   if [ "$COUNT" != "0" ] ; then
  75.       LASTID=`cat /tmp/twitter.$unique.json|jq ".statuses[].id_str"|head -1|cut -d"\"" -f2`
  76.       print_tweets    
  77.   fi
  78.  
  79.   sleep 3
  80. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top