#!/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::Bing;
### http://search.cpan.org/~dhouston/Bing-Search/lib/Bing/Search.pm

use strict;
use Bing::Search;
use Bing::Search::Source::Web;

use Crawler::Config;
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 search(){
	
	my $self 	= shift;
	my $query 	= shift;
	
	my @return 	= ();
	my $basic 	= $self->{basic};
	
	my $search 	= Bing::Search->new(
      AppId => $self->{cfgValue},
      Query => $query,
    );
    
    my $source 	= Bing::Search::Source::Web->new();
    $search->add_source( $source );

    my $response = $search->search();
	
	foreach my $result ( @{$response->results} ) { 
	
		#print $result->Title, " -> ", $result->Url, "\n";
		
		if ( $basic->checkKeywords($result->Title) == 1){
			#my %c 			= ();
			#my $c;
			#$c->{title} 	= $result->Title;
			#$c->{url} 		= $result->Url;
			
			#push(@return, $c);
			push(@return, $result->Url.":".$result->Title.";\n");
		}
	}
	$self->{db_flat}->{binglinks} = @return;
	return \@return;
}

   