tweeirc - localbin - leot's localbin (~/bin)
 (HTM) hg clone https://bitbucket.org/iamleot/localbin
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       weeirc
       ---
            1 #!/bin/sh
            2 
            3 #
            4 # Given an IRC server, port and channel via TWEEIRC_* environment
            5 # variables and a list of users as arguments fetch all Twitter user
            6 # timelines, parse them via tscrape and share each tweet on the IRC
            7 # channel with the proper nick.
            8 #
            9 
           10 
           11 tweeirc_dir=${TWEEIRC_DIR:="${HOME}/.tweeirc"}
           12 tweeirc_channel=${TWEEIRC_CHANNEL:="#twitter"}
           13 tweeirc_server=${TWEEIRC_SERVER:="localhost"}
           14 tweeirc_port=${TWEEIRC_PORT:="6667"}
           15 
           16 base="https://twitter.com"
           17 
           18 
           19 #
           20 # Print usage message and exit
           21 #
           22 usage()
           23 {
           24 
           25         echo "usage: tweeirc [-o] username ..."
           26         exit 1
           27 }
           28 
           29 
           30 #
           31 # Given a username print to standard output all tweets newer than the id stored
           32 # in fileid.
           33 #
           34 tweets()
           35 {
           36         username=$1
           37 
           38         curl -m 10 -H 'Accept-Language: en' -gs "${base}/${username}" | tscrape | sort -n |
           39         awk -F '\t' \
           40             -v fileid="${tweeirc_dir}/${username}" '
           41         BEGIN {
           42                 getline min_id < fileid
           43                 min_id = int(min_id)
           44                 max_id = 0
           45         }
           46 
           47         {
           48                 timestamp = $1
           49                 username = $2
           50                 text = $4
           51                 item_id = $5
           52                 item_username = $6
           53                 item_retweetid = $8
           54                 item_pinned = $9
           55                 id = item_retweetid ? item_retweetid : item_id
           56                 id = int(id)
           57                 max_id = id > max_id ? id : max_id
           58         }
           59 
           60         (id > min_id) {
           61                 if (username != item_username)
           62                         printf("RT @%s ", item_username)
           63                 printf("%s\n", text)
           64         }
           65 
           66         END {
           67                 if (max_id > min_id)
           68                         print max_id > fileid
           69         }
           70         '
           71 }
           72 
           73 
           74 #
           75 # Given a username, pipe to the IRC channel
           76 #
           77 irc()
           78 {
           79         username=$1
           80 
           81         awk -v nc="nc ${tweeirc_server} ${tweeirc_port} > /dev/null" \
           82             -v channel="${tweeirc_channel}" \
           83             -v username="${username}" '
           84         BEGIN {
           85                 print "NICK " username | nc
           86                 print "USER " username " * * *" | nc
           87                 fflush(nc)
           88         }
           89 
           90         {
           91                 print "PRIVMSG " channel " :" $0 | nc
           92                 fflush(nc)
           93         }
           94 
           95         END {
           96                 print "QUIT" | nc
           97                 close(nc)
           98         }
           99         '
          100 }
          101 
          102 
          103 while getopts o f; do
          104         case $f in
          105                 o) irc() { awk -v u="$1" '{ printf("<%s> %s\n", u, $0) }'; } ;;
          106                 \?) usage ;;
          107         esac
          108 done
          109 shift $((OPTIND - 1))
          110 if [ $# -lt 1 ]; then
          111         usage
          112 fi
          113 
          114 mkdir -p "${tweeirc_dir}" || exit 1
          115 
          116 for u in "$@"; do
          117         tweets "$u" | irc "$u"
          118 done