Commit b4c37dfc authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Loading of Admitad Affiliates is working now

parent 6751648b
......@@ -93,11 +93,16 @@ class MetaGerSearch extends Controller
# Ergebnisse der Suchmaschinen kombinieren:
$metager->prepareResults($timings);
$admitad = null;
$admitad = [];
if(!$metager->isApiAuthorized() && !$metager->isDummy()){
$admitad = new \App\Models\Admitad($metager);
$newAdmitad = new \App\Models\Admitad($metager);
if(!empty($newAdmitad->hash)){
$admitad[] = $newAdmitad;
}
}
$metager->parseAdmitad($admitad);
$finished = true;
foreach ($metager->getEngines() as $engine) {
if ($engine->loaded) {
......@@ -204,6 +209,7 @@ class MetaGerSearch extends Controller
$metager->setAdgoalHash($adgoal["adgoalHash"]);
$metager->parseFormData($request, false);
$metager->setJsEnabled(true);
# Nach Spezialsuchen überprüfen:
$metager->checkSpecialSearches($request);
$metager->restoreEngines($engines);
......@@ -215,7 +221,15 @@ class MetaGerSearch extends Controller
$metager->rankAll();
$metager->prepareResults();
$metager->parseAdmitad($admitad);
if(!$metager->isApiAuthorized() && !$metager->isDummy()){
$newAdmitad = new \App\Models\Admitad($metager);
if(!empty($newAdmitad->hash)){
$admitad[] = $newAdmitad;
}
}
$admitadFinished = $metager->parseAdmitad($admitad);
$result = [
'finished' => true,
......@@ -263,7 +277,7 @@ class MetaGerSearch extends Controller
}
}
if (!$metager->isAdgoalLoaded()) {
if (!$metager->isAdgoalLoaded() || !$admitadFinished) {
$finished = false;
}
......@@ -280,6 +294,7 @@ class MetaGerSearch extends Controller
"metager" => [
"apiAuthorized" => $metager->isApiAuthorized(),
],
"admitad" => $admitad,
"adgoal" => [
"loaded" => $metager->isAdgoalLoaded(),
"adgoalHash" => $metager->getAdgoalHash(),
......
......@@ -48,6 +48,7 @@ class MetaGer
protected $availableFoki = [];
protected $startCount = 0;
protected $canCache = false;
protected $javascript = false;
# Daten über die Abfrage$
protected $ip;
protected $useragent;
......@@ -435,8 +436,25 @@ class MetaGer
}
}
public function parseAdmitad(\App\Models\Admitad &$admitad){
$admitad->parseAffiliates($this->results);
/**
* @param \App\Models\Admitad[] $admitads
* @param Boolean $wait Wait for Results?
* @return Boolean whether or not all Admitad Objects are finished
*/
public function parseAdmitad(&$admitads){
$wait = false;
$finished = true;
if(!$this->javascript){
$wait = true;
}
foreach ($admitads as $admitad) {
$admitad->fetchAffiliates($wait);
$admitad->parseAffiliates($this->results);
if(!$admitad->finished){
$finished = false;
}
}
return $finished;
}
public function humanVerification(&$results)
......@@ -1940,6 +1958,13 @@ class MetaGer
return $this->dummy;
}
public function jsEnabled() {
return $this->javascript;
}
public function setJsEnabled(bool $bool){
$this->javascript = $bool;
}
/**
* Used by JS result loader to restore MetaGer Object of previous request
*/
......
......@@ -14,7 +14,9 @@ class Admitad
"en"
];
private $hash;
public $hash;
public $finished = false; // Is true when the Request was sent to and read from Admitad App
private $affiliates = [];
/**
* Creates a new Admitad object which will start a request for affiliate links
......@@ -29,7 +31,13 @@ class Admitad
// Generate a list of URLs
$resultLinks = [];
foreach($results as $result){
$resultLinks[] = $result->originalLink;
if ($result->new) {
$resultLinks[] = $result->originalLink;
}
}
if(empty($resultLinks)){
return;
}
$lang = LaravelLocalization::getCurrentLocale();
......@@ -69,19 +77,51 @@ class Admitad
}
/**
* Parses the response from Admitad server and converts all Affiliate Links.
*
* @param \App\Models\Result[] $results
* Fetches the Admitad Response from Redis
* @param Boolean $wait Whether or not to wait for a response
*/
public function parseAffiliates(&$results){
$answer = Cache::get($this->hash);
public function fetchAffiliates($wait = false) {
if($this->finished){
return;
}
$answer = null;
$startTime = microtime(true);
if($wait){
while (microtime(true) - $startTime < 5) {
$answer = Cache::get($this->hash);
if ($answer === null) {
usleep(50 * 1000);
} else {
break;
}
}
}else{
$answer = Cache::get($this->hash);
}
$answer = json_decode($answer, true);
// If the fetcher had an Error
if($answer === "no-result"){
$this->finished = true;
return;
}
if(empty($answer) || !isset($answer["error"]) || $answer["error"] || !is_array($answer["result"])){
return;
}
foreach($answer["result"] as $linkResult){
$this->affiliates = $answer["result"];
$this->finished = true;
}
/**
* Converts all Affiliate Links.
*
* @param \App\Models\Result[] $results
*/
public function parseAffiliates(&$results){
foreach($this->affiliates as $linkResult){
$originalUrl = $linkResult["originalUrl"];
$redirUrl = $linkResult["redirUrl"];
$image = $linkResult["image"];
......@@ -94,7 +134,7 @@ class Admitad
foreach ($results as $result) {
if ($result->originalLink === $originalUrl) {
# Ein Advertiser gefunden
if ($result->image !== "") {
if ($result->image !== "" && !$result->partnershop) {
$result->logo = $image;
} else {
$result->image = $image;
......@@ -107,7 +147,5 @@ class Admitad
}
}
}
Log::info("tzewst");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment