%script to download all images returned by a google image search
%inputs:
%   Names : Cell array of strings containing names to search for with spaces
%   replaced by '_'
%   Start and finish define where on the array to run over
%outputs:
%   creates a folder images and subfolders for each name.  All images are
%   downloaded as .png files
%Usage:
%   Names{1,1}="ziggy_stardust";
%   Names{1,2}="david_bowie";
%   downloadimagesfromgoogle(Names,1,2);
%   a console output shows three integers [i j k]
%   where i is the index of the name
%   j is the google page currently in use
%   k is the image number on the page
%Requirements:
%   requires the program URL2File available at
%   http://www.chami.com/free/url2file_wincon.html

function downloadimagesfromgoogle(Names,start,finish)

for i=start:finish
    
    numi=0;
    %break name down into words
    namei='';
    words=[0];
    for j=1:numel(Names{1,i})
        if Names{1,i}(j)=='_'
            words=[words j];
            namei{1,numel(words)-1}=Names{1,i}(words(numel(words)-1)+1:words(numel(words))-1);
        end
    end
    namei{1,numel(words)}=Names{1,i}(words(numel(words))+1:end);
    outdir=Names{1,i};
    mkdir('images',outdir);
    %create google query
    %start
    querya=strcat('"http://images.google.com/images?q=',namei{1,1});
    %add names
    for k=2:numel(namei)
        querya=strcat(querya,'+',namei{1,k},'&safe=off');
    end
    
    %cycle through pages
    j=0;
    run=1;
    while run==1
        %finish query
        query=strcat(querya,'&start=',num2str(j*20),'"');
        delete tempfile.txt;
        eval(['!URL2file ' query ' tempfile.txt']);
        try
            t=textread('tempfile.txt','%s');
            for k=1:numel(t)
                if numel(t{k,1})>21
                    if strcmp('href=/imgres?imgurl=',t{k,1}(1:20));
                        %image is located in this line
                        numi=numi+1;
                        imagename=t{k,1}(21:end);
                        finish=findstr(imagename,'&');
                        imagename=imagename(1:finish(1)-1);
                        problem=findstr(imagename,'%2520');
                        if numel(problem)~=0
                            for u=numel(problem):-1:1
                                imagename=strcat(imagename(1:problem(u)-1),'%20',imagename(problem(u)+5:end));
                            end
                        end
                        [i j numi]
                        try
                        imwrite(imread(imagename),strcat('images\',outdir,'\pic.',num2str(i),'.',num2str(numi),'.png'),'png');
                        end
                    end
                end
                if k>3
                    if strcmp(t(k-3),'similar') && strcmp(t(k-2),'to') && strcmp(t(k-1),'the')
                        run=0;
                    end
                end
            end
        catch
        end
        j=j+1;
    end
end
        
    