Posts Tagged ‘google’

Google images game: high score

Monday, October 26th, 2009

I will try to implement a current high score or high scores feature to the Google images game, but in the mean time I will post screenshots here. Here’s the first known “Hard” solution found for the “Nouns” game:


Google images game first hard solution


Try to beat this score!

Leave a comment below or contact me with a screen shot of your high score.

Google images game

Sunday, October 25th, 2009


google images game screen shot

I finished coding a rather simple but addictive and challenging puzzle game, I’m calling the Google images game. The player is presented with the image results of a Google images search for some search word or phrase. The player’s objective is to guess and figure out what that search phrase was. Play it now!

The game is an amalgamation of php, javascript and ruby. The ajax javascript for the game is rather complicated by now to handle all of the gaming options, but it’s all visible in the game’s page source. I use a little ruby program to grab the initial google images thumbnails:


#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'cgi'

SAFE_VALUES = ["on","moderate","off"]
cgi = CGI.new("html3")
safe = "moderate"
safe = cgi["safe"] if(!cgi["safe"].empty? and SAFE_VALUES.include?(cgi["safe"]))
target = ""
target = cgi["target"] if(!cgi["target"].empty?)

if(!cgi["query"].empty?)
  address = "http://images.google.com/images?q="+
    cgi["query"].gsub(/ /,"+")+"&safe="+safe
  uri = URI(address)
  http = Net::HTTP.new(uri.host, uri.port)
  headers = {
    "User-Agent" => "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)"
    }

  code = http.head(address, headers).code.to_i
  if (code >= 200 && code < 300) then

      #the data is available...
      response = ""
      http.get(address, headers) do |chunk|
        response = response + chunk
      end
      images = []
      response.scan(
        /imgurl=(.+?)&imgrefurl=(.+?)&[^<]+<img src=(.+?) /
        ) do |img, ref, thumb|
        images<<[img,ref, thumb]
      end
      images.collect! do |e|
        "<a title='"+e[1]+" 'href='"+
        CGI.unescape(e[1]).gsub(/'/,"%27").gsub(/ /,"%20")+
        "' target='"+target+"'><img src='"+e[2]+"' height='100px'></a>"
      end
      cgi.out{
        images.join(" ")
      }
  end
end

I use a little php program to return a random line (initial search phrase/word) from a given file:


  $difficulty = 'easy';
  if (isset($_GET['difficulty'])&&strlen($_GET['difficulty'])>0) {
      $difficulty= $_GET['difficulty'];
  }
  $word_list = 'nouns.txt';
  $possible_word_lists = array('nouns.txt','wiki.txt');
  if (isset($_GET['word_list'])&&strlen($_GET['word_list'])>0) {
      if(in_array($_GET['word_list'],$possible_word_lists)){
        $word_list = $_GET['word_list'];
      }
  }


  $query = "";
  $word_count = 1;
  if(strcmp($word_list,'nouns.txt')==0){
    if(strcmp($difficulty,"medium")==0){
      $word_count = 2;
    }else if(strcmp($difficulty,"hard")==0){
      $word_count = 3;
    }
  }
  for($i=0; $i<$word_count; $i=$i+1){
    if(strlen($query)>0){
      $query= $query."+";
    }
    $query = $query.`ruby -e "a=File.readlines('$word_list').collect{|line| line.strip.gsub(' ','+').gsub('\'','%27')};puts a[rand*a.leng
    $query = trim($query);
  }
  print trim($query);

As you can see, I gave up on php half way through and cheated by calling a ruby one-liner. Someday I'll fix this up...

Foiling google.com’s pay-per-click advertising program

Thursday, July 30th, 2009

For educational purposes only:
Here is a sketch for a script that works with Safari and shell to “click” on all the ads you see while surfing the web. These visits to the advertisers pages (after first being rerouted by the pay-per-click counter, in this case google.com) happen behind the scene. So to the advertiser and the host of the ads it seems as though you clicked on the ad you saw, but to you your surfing went undisturbed.

Save the following in a file called clickads.sh:


#!/bin/bash
SOURCE=`cat -`
ADS=`echo $SOURCE | grep -o "/aclk?[^\"]*" | \
sed s/amp\;//g | sed s/^/http:\\\/\\\/google.com/g`
for x in $ADS
do
  `/sw/bin/wget -q -erobots=off -t2 -O - -U "Mozilla/5.0 \
(Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/530.17 \
(KHTML, like Gecko) Version/4.0 Safari/530.17" "$x" 2>&1 | \
cat > log.clickads`
done

Note: change your absolute path to wget accordingly (find what it is by issuing which wget in a terminal).

clickads.sh will accept the html source of a google search, strip out all of the side bar ads and “click” each one. Remove the log and change your -U User agent accordingly (remember this is not for use and for educational purposes only).

Now, so that the background clicker only “clicks” on ads you actually see, save and run this as an Applescript:


repeat
	try
		tell application "Safari"
			set old_current_source to ""
			set current_source to ""
			repeat until old_current_source is equal to current_source and current_source is not equal to ""
				set old_current_source to source of front document as string
				delay 1.0
				set current_source to source of front document as string
			end repeat
			set current_url to URL of front document as string
		end tell
		if current_url starts with "http://www.google.com" then
			set newFileName to ".behindthescenesadclick.temp"
			set newFile to open for access newFileName with write permission
			set eof of newFile to 0
			write current_source to newFile
			close access newFile
			do shell script "cat /.behindthescenesadclick.temp | bash ~/Bash/clickads.sh"
		else
			delay 1
		end if
	end try
end repeat

The path to the dump temp file ".behindthescenesadclick.temp" has to be in Applescript format not UNIX. And remember to change the path to ~/Bash/clickads.sh to point to your file.

The above code could be easily modified to target pay-per-click hosts other than google.com and there was no reason other than availability that I chose that site here.

Help google images find your images

Tuesday, July 21st, 2009

I’ve been using a javascript turned perl turned ruby gallery script to make hosting images as easy and sleek as possible. But I’ve found that google (and of course if there are other search engines then those, too) has not indexed the images shown in my gallery. All other images on my site that are included on a static html page eventually show up. I don’t want to give up my gallery script (hopefully I’ll post that code soon). So, I drew up some quick bash code to find all the images on my site and throw them into a static page, which I then hide links to all over my site.
Here’s the bash code that can be run periodically (it’s not necessary to do it all the time since search engines aren’t caching the site all the time…but obviously more often is better). Here’s my script, named make_all.sh:


#!/bin/bash
# Generate an html page with links to all images in this dir
# Author: Alec Jacobson (alecjacobson[strudel]gmail.com)
#
# To write links to all images in dir/ to index.html run:
# $ ./make_all.sh dir/ index.html 
#
echo "<html><body>" > $2
links=`find $1 -iregex ".*.jpe?g$" \
  | sed "s/\(^.*$\)/<a href=\"\1\" ><img alt=\'\[ideal query]\' src=\"\1\"\/><\/a>/"`
echo $links >> $2
echo "</body></html>" >> $2

Change ".*.jpe?g$" as you see fit (I’m only worrying about jpegs). Since I want my images to come when my name is searched I put my name in the alt field for [ideal query]. I also add a little text to the page to be sure it isn’t ignored for lack of content.

Next, since I don’t intend for human users to see this page but I still want the crawlers to find it I hide links all over my site using:


<a href="[path to your static index of images.html]" style="text-decoration:none;">&nbsp;</a>

You could even get really carried away hiding it and move it off the visible pages using position:absolute; top:-1;.

Here’s my static page of all my images, and between these parenthesis is an example of a hidden link ( ).

I will report back with whether this has worked successfully for me.