Advertisement
Guest User

Mention Constellation Generator

a guest
Jan 3rd, 2012
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. MENTION_COUNT=10000
  4. COMPONENTS="0-1000"
  5. OUTPUT_SIZE=100
  6. STREAM_URL=https://stream.twitter.com/1/statuses/sample.json
  7. CURL_OPTIONS=
  8. MENTIONS=`mktemp -t mentions`
  9. SCRIPT=`mktemp -t script`
  10.  
  11. function Usage {
  12.     echo Usage: `basename $0` -u username [-p password] [-n num] [-s size] [-q] [-v] [-o]
  13.     exit 1
  14. }
  15.  
  16. function Dependencies {
  17.     echo `basename $0` requires that Graphviz and Python be installed and on your PATH
  18.     exit 2
  19. }
  20.  
  21. test `which python` || Dependencies
  22. test `which sfdp` || Dependencies
  23.  
  24. while getopts ":n:u:p:s:qvo" opt; do
  25.     case $opt in
  26.         n)
  27.             MENTION_COUNT=$OPTARG
  28.             ;;
  29.         o)
  30.             OPEN=yes
  31.             ;;
  32.         u)
  33.             TWITTER_USERNAME=$OPTARG
  34.             ;;
  35.         p)
  36.             TWITTER_PASSWORD=$OPTARG
  37.             ;;
  38.         q)
  39.             QUIET=yes
  40.             ;;
  41.         s)
  42.             OUTPUT_SIZE=$OPTARG
  43.             ;;
  44.         v)
  45.             PROGRESS=-v
  46.             ;;
  47.         \?)
  48.             Usage
  49.             ;;
  50.         :)
  51.             Usage
  52.             ;;
  53.     esac
  54. done
  55.  
  56. if [ "${TWITTER_USERNAME}" == "" ]; then Usage; fi
  57.  
  58. if [ "${TWITTER_PASSWORD}" == "" ]; then
  59.     stty -echo
  60.     read -p "Twitter password:" TWITTER_PASSWORD; echo
  61.     stty echo
  62. fi
  63.  
  64. test $QUIET || echo Collecting ~${MENTION_COUNT} mentions from ${STREAM_URL} at ${MENTIONS}
  65.  
  66. cat > ${SCRIPT} <<EOF
  67. import os
  68. import sys
  69. import json
  70. import time
  71. import base64
  72. import urllib2
  73. from optparse import OptionParser
  74.  
  75. parser = OptionParser()
  76. parser.add_option('-u', action='store', dest='username')
  77. parser.add_option('-p', action='store', dest='password')
  78. parser.add_option('-n', action='store', dest='target', default=1000, type=int)
  79. parser.add_option('-v', action='store_true', dest='verbose', default=False)
  80. (options, args) = parser.parse_args()
  81.  
  82. request = urllib2.Request("https://stream.twitter.com/1/statuses/sample.json")
  83. request.add_header("Authorization", "Basic %s" % base64.encodestring('%s:%s' % (options.username, options.password)).replace('\n',''))
  84. stream = urllib2.urlopen(request)
  85.  
  86. count=0
  87. start=time.time()
  88.  
  89. for line in stream:
  90.     if len(line) == 0:
  91.         continue
  92.        
  93.     message = json.loads(line)
  94.     if (message.get("entities") == None):
  95.         continue
  96.     user = message["user"]["screen_name"]
  97.     for mentionee in message.get("entities").get("user_mentions"):
  98.         print "\"%s\" -> \"%s\"" % (user.lower(), mentionee["screen_name"].lower())
  99.         count += 1
  100.         if (options.verbose) and (time.time() - start > 5):
  101.             start = time.time()
  102.             print >> sys.stderr, "\r%s mentions collected" % count,
  103.     if (count > options.target):
  104.         print >> sys.stderr, "\r%s mentions collected" % count
  105.         break
  106. EOF
  107.  
  108. python -u ${SCRIPT} -u ${TWITTER_USERNAME} -p ${TWITTER_PASSWORD} -n ${MENTION_COUNT} ${PROGRESS} > ${MENTIONS}
  109.  
  110. test $QUIET || echo Done collecting mentions
  111.  
  112. test $QUIET || echo Rendering graph
  113.  
  114. sort ${MENTIONS} \
  115.     | uniq \
  116.     | cat <(echo "digraph mentions {") - <(echo "}") \
  117.     | ccomps -zX#${COMPONENTS} \
  118.     | grep "-" \
  119.     | cat <(echo "digraph mentions {") - <(echo "}") \
  120.     | tee $0.gv \
  121.     | sfdp \
  122.         -Gbgcolor=black \
  123.         -Ncolor=white \
  124.         -Ecolor=white \
  125.         -Nwidth=0.02 \
  126.         -Nheight=0.02 \
  127.         -Nfixedsize=true \
  128.         -Nlabel='' \
  129.         -Earrowsize=0.4 \
  130.         -Gsize=${OUTPUT_SIZE} \
  131.         -Gratio=fill \
  132.     | neato -s -n2 -Nlabel='' -Tpng \
  133.     > $0.png
  134.    
  135. test $OPEN && open $0.png
  136.  
  137. test $QUIET || echo Done
  138.  
  139. rm ${SCRIPT}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement