rendered paste body#!/usr/bin/env python
# -*- coding: utf-8 -*-
# xgirlsycups.py - See http://xkcd.com/467/
#
# Usage: $ xgirlsycups.py [ROWS] [COLUMNS] > xgirlsycups.p
# gnuplot> load 'xgirlsycups.p'
#
# Requirements: curl
#
# By Mansour Moufid <mmoufid@connect.carleton.ca>
# The author hereby dedicates any and all copyright interest in this
# code to the public domain.
import sys, os
import string
def do_help():
print "Usage: xgirlsycups.py [<ROWS> <COLUMNS>]"
print "See http://xkcd.com/467/"
sys.exit(0)
def main():
# Argument handling
if len(sys.argv) == 2:
do_help()
else:
rows = 8
columns = 8
# The matrix
matrix = [ [ 3, 375, 9, 0, 0, 0, 0, 0, ], \
[ 7, 7503, 2007, 10, 5, 3, 6, 2, ], \
[ 9, 1929000, 247, 7, 14, 13, 2, 1, ], \
[ 7, 6335, 394, 34, 3, 2, 6, 0, ], \
[ 3, 3513, 34, 0, 63, 0, 0, 0, ], \
[ 1, 9, 5, 3, 0, 0, 3, 0, ], \
[ 3, 1461, 1, 1, 1, 0, 0, 0, ], \
[ 2, 19, 4, 2, 0, 0, 0, 0, ], ]
for i in range(rows):
for j in range(columns):
try:
pout, pin = os.popen2( "curl -s -A \"Mozilla/4.0\" \"http://www.google.ca/search?&q=\\\"" + str(i) + "+girl" + (i!=1)*"s" + "+" + str(j) + "+cup" + (j!=1)*"s" + "\\\"\" | tr ['<'] ['\\n'] | grep -a -A 5 \"Results\" | tail -1 | awk -F '>' '{print $2}' | tr -d [',']" )
except IOError:
sys.exit(1)
try:
matrix[i][j] = int( pin.read() )
except ValueError:
matrix[i][j] = 0
pout.close()
pin.close()
# Generate Gnuplot script
print "set terminal png nocrop enhanced font \"/usr/share/fonts/ttf-bitstream-vera/VeraBd.ttf\" 12 size 640,480"
print "set output 'xgirlsycups.png'"
print
print "set title \"GOOGLE RESULTS FOR '<X> GIRLS <Y> CUPS'\""
print "set xlabel \"CUPS\""
print "set ylabel \"GIRLS\""
print
print "unset key"
print "set tic scale 0"
print "set palette model RGB defined (0 \"dark-red\", 50 \"dark-orange\", 75 \"orange\", 95 \"yellow\", 100 \"white\")"
print "set cbrange [0:100]"
print "unset cblabel"
print "unset cbtics"
print
print "set xrange [-0.5:" + str(rows) + ".5]"
print "set yrange [-0.5:" + str(columns) + ".5]"
print
print "set view map"
print "splot '-' matrix with image"
for i in range(rows):
for j in range(columns):
sys.stdout.write("%i " % matrix[i][j])
sys.stdout.write("\n")
print "e"
print "e"
if __name__ == '__main__':
main()