#!/usr/bin/perl

###########################
### Autor: Sebastian Enger / B.Sc. 
### Copyright: Sebastian Enger
### Licence: Commercial
### Version: 1.1.1  - 2013-09-18
### Contact: sebastian.enger@gmail.com
###########################

package Crawler::Google;
# http://search.cpan.org/~rokr/Google-Search-0.028/lib/Google/Search.pm

use strict;
use Google::Search;

use Crawler::Config;
use Crawler::MemStore;
use Crawler::Basic;

# Konstruktor
sub new {
	
	my ($class) 	= @_;
	
	my $config 		= Crawler::Config->new();
	my $MemStore	= Crawler::MemStore->new();
	my $basic  		= Crawler::Basic->new();
	
	my $self = {
		'db_flat'	=> $MemStore,
		'config' => $config,
		'basic' => $basic,
	};
	bless $self, $class;
  return $self;
}

sub news(){

}

sub patent(){

}

sub search(){
	
	my $self = shift;
	my $query = shift;
	
	my @return = ();
	my $basic = $self->{basic};
	
	my $search = Google::Search->Web(q => $query, hl => 'de' );
	my $result = $search->first;
	
	while ($result) {
	#	print $result->number, " ", $result->uri, "\n";
	#	$result = $result->next;
	#	$result->title
		
		if ( $basic->checkKeywords($result->title) == 1){
			#my %c 			= ();
			#$c->{url} 		= $result->uri;
			#$c->{title} 	= $result->title;
			#push(@return, $c);
			push(@return, $result->uri.":".$result->title.";\n");
		}
	}
	
	$self->{db_flat}->{googlelinks} = @return;
	
	return \@return;
}