Skip to content

Commit

Permalink
Add results parsing and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
maikgreubel committed Aug 20, 2017
1 parent 58e1a3f commit 56d6f2c
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 146 deletions.
8 changes: 8 additions & 0 deletions src/ApiException.php
@@ -0,0 +1,8 @@
<?php
namespace Nkey\DDG\API;

use Generics\GenericsException;

class ApiException extends GenericsException
{
}
83 changes: 70 additions & 13 deletions src/JsonProvider.php
Expand Up @@ -3,47 +3,94 @@

use Generics\Client\HttpClient;
use Generics\Socket\Url;
use Exception;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* This class provides means of fetching data from duckduckgo api
*
* @author Maik Greubel <greubel@nkey.de>
* @license Apache 2.0
*/
class JsonProvider
class JsonProvider implements LoggerAwareInterface
{
/**
* Logger instance
*
* @var LoggerInterface
*/
private $logger;

/**
* Timeout in seconds
*
* @var int
*/
private $timeout = 5;

/**
*
* @var HttpClient
*/
private $httpClient;

public function __construct()
{
$this->logger = new NullLogger();
}

/**
* Set timeout in seconds
*
* @param int $timeout
*/
public function setTimeout(int $timeout)
{
$this->timeout = $timeout;
}

/**
* Query the duck duck go api
*
* @param string $query
* Keywords to start a query for
* @throws ApiException
* @return string The payload
*/
public function query(string $query): string
{
$this->httpClient = new HttpClient($this->provideUrl($query));
$this->httpClient->setHeader('User-Agent', '*');
$this->httpClient->setHeader('Accept-Encoding', 'identity'); // Currently there is a bug in HttpClient for passing buf to gzdecode()
$this->httpClient->request('GET');
$response = "";

while ($this->httpClient->getPayload()->ready()) {
$response .= $this->httpClient->getPayload()->read($this->httpClient->getPayload()
->count());
try {

$this->httpClient = new HttpClient($this->provideUrl($query));
$this->httpClient->setTimeout($this->timeout);
$this->httpClient->setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36');
$this->httpClient->setHeader('Accept-Encoding', 'identity'); // Currently there is a bug in HttpClient for passing buf to gzdecode()
$this->httpClient->setHeader('Connection', 'close');
$this->httpClient->request('GET');
$response = "";

while ($this->httpClient->getPayload()->ready()) {
$response .= $this->httpClient->getPayload()->read($this->httpClient->getPayload()
->count());
}

$response = strstr($response, "{");
$response = substr($response, 0, strrpos($response, "}") + 1);
return $response;
} catch (Exception $ex) {
$this->logger->error($ex->getMessage());
$this->logger->debug($ex->getTraceAsString());
throw new ApiException("Could not retrieve query response: {cause}", array(
'cause' => $ex->getMessage()
), $ex->getCode(), $ex);
}

return $response;
}

/**
* Provide an URL object out of given duckduckgo query
*
* @param string $query
* @return Url
*/
Expand All @@ -53,4 +100,14 @@ private function provideUrl(string $query): Url

return $url;
}

/**
* Set the logger
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

}
8 changes: 4 additions & 4 deletions src/Model/QueryResult.php
Expand Up @@ -221,7 +221,7 @@ public function setEntity(string $entity): QueryResult
*/
public function getMeta(): Metadata
{
return $this->meta;
return $this->meta == null ? new Metadata() : $this->meta;
}

/**
Expand Down Expand Up @@ -530,7 +530,7 @@ public function getRelatedTopics(): array
*/
public function getResults(): array
{
return $this->results;
return $this->results == null ? array() : $this->results;
}

/**
Expand All @@ -546,10 +546,10 @@ public function addResult(Result $result): QueryResult

/**
*
* @param RelatedTopic $topic
* @param Result $topic
* @return QueryResult
*/
public function addRelatedTopic(RelatedTopic $topic): QueryResult
public function addRelatedTopic(Result $topic): QueryResult
{
$this->relatedTopics[] = $topic;
return $this;
Expand Down
115 changes: 0 additions & 115 deletions src/Model/RelatedTopic.php

This file was deleted.

110 changes: 109 additions & 1 deletion src/Model/Result.php
@@ -1,7 +1,115 @@
<?php
namespace Nkey\DDG\API\Model;

/**
* Model for RelatedTopics property
*
* @author Maik Greubel <greubel@nkey.de>
*/
class Result
{


/**
*
* @var string
*/
private $result;

/**
*
* @var Icon
*/
private $icon;

/**
*
* @var string
*/
private $firstURL;

/**
*
* @var string
*/
private $text;

/**
*
* @return string
*/
public function getResult(): string
{
return $this->result;
}

/**
*
* @param string $result
* @return Result
*/
public function setResult(string $result)
{
$this->result = $result;
return $this;
}

/**
*
* @return Icon
*/
public function getIcon(): Icon
{
return $this->icon;
}

/**
*
* @param Icon $icon
* @return Result
*/
public function setIcon(Icon $icon)
{
$this->icon = $icon;
return $this;
}

/**
*
* @return string
*/
public function getFirstURL(): string
{
return $this->firstURL;
}

/**
*
* @param string $firstURL
* @return Result
*/
public function setFirstURL(string $firstURL)
{
$this->firstURL = $firstURL;
return $this;
}

/**
*
* @return string
*/
public function getText(): string
{
return $this->text;
}

/**
*
* @param string $text
* @return Result
*/
public function setText(string $text)
{
$this->text = $text;
return $this;
}
}

0 comments on commit 56d6f2c

Please sign in to comment.